function MyBlock(props) { const x = props.x; const [content, setContent] =…
- User
function MyBlock(props) {
const x = props.x; const [content, setContent] = useState(x.content); const images = [ //Server checks they're correct when submitting a request { url: 'http://localhost:3000/images/rome.jpg', alt: 'Image' }, { url: 'http://localhost:3000/images/brasil.jpg', alt: 'Image' }, ]; const removeFromList = (x) => { props.setList((oldlist) => oldlist.filter((item) => item != x)); }; const handleUp = (x) => { props.setList(oldList => { const index = oldList.indexOf(x); const newList = [...oldList]; [newList[index - 1], newList[index]] = [newList[index], newList[index - 1]]; return newList; }); }; const handleDown = (x) => { props.setList(oldList => { const index = oldList.indexOf(x); const newList = [...oldList]; [newList[index], newList[index + 1]] = [newList[index + 1], newList[index]]; return newList; }); }; return ( <Accordion.Item eventKey={props.list.indexOf(x)}> <Accordion.Header > <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", width: "100%" }}> {x.blockType} <ButtonGroup style={{ paddingRight: "2%" }} > {props.list.indexOf(x) != 0 && ( //button as div avoid nested buttons error validateDOMnesting - since accordion header is a button itself! <Button as='div' style={{ fontSize: "0.8rem", padding: "0.25rem 0.5rem" }} variant="outline-secondary" onClick={(ev) => { ev.stopPropagation(); handleUp(x) }}> <i className="bi bi-arrow-up"></i> </Button>)} {props.list.indexOf(x) != (props.list.length - 1) && ( <Button as='div' style={{ fontSize: "0.8rem", padding: "0.25rem 0.5rem" }} variant="outline-secondary" onClick={(ev) => { ev.stopPropagation(); handleDown(x) }}> <i className="bi bi-arrow-down"></i> </Button>)} <Button as='div' size="sm" variant="outline-danger" onClick={(ev) => { ev.stopPropagation(); removeFromList(x) }}> <i className="bi bi-x-lg"></i> </Button> </ButtonGroup> </div> </Accordion.Header> <Accordion.Body> {x.blockType != "Image" ? ( <Form.Control as="textarea" value={content} onChange={(ev) => { x.content = ev.target.value; setContent(ev.target.value); }}></Form.Control> ) : ( <div> {images.map((image) => (<ImageForm image={image} key={generatelocalId()}/> ))} </div> )} </Accordion.Body> </Accordion.Item> ); function ImageForm(props) { return ( <label key={generatelocalId()} style={{ paddingRight: "20px" }}> <input value={props.image.url} onChange={(ev) => { x.content = ev.target.value; setContent(ev.target.value); }} checked={x.content == props.image.url} type="radio" name="image" /> <img style={{ borderRadius: "5%", paddingLeft: "5px", width: '256px', height: '144px' }} src={props.image.url} alt={props.image.alt} /> </label> ); }}
When I click an accordion and expoand the lower box and select an image all is ok. Then when I open another accordion and select another image everything seems fine. If I check back the first accordion I see x.content is ok but a video I don't see the radiobutton clicked.
- Assistant
It seems like the "checked" property of the input element isn't updating as expected when you switch between accordions. To fix this issue, you can use React's useState hook to track the selected image for each accordion item individually. Here's a modified version of your code that should work:
function MyBlock(props) { // ... Existing code ... return ( <Accordion.Item eventKey={props.list.indexOf(x)}> {/* ... Existing code ... */} <Accordion.Body> {x.blockType != "Image" ? ( <Form.Control as="textarea" value={content} onChange={(ev) => { x.content = ev.target.value; setContent(ev.target.value); }}></Form.Control> ) : ( <div> {images.map((image) => ( <ImageForm image={image} key={generatelocalId()} selectedImage={x.content} setSelectedImage={(url) => { x.content = url; setContent(url); }} /> ))} </div> )} </Accordion.Body> </Accordion.Item> ); } function ImageForm(props) { const { image, selectedImage, setSelectedImage } = props; return ( <label key={generatelocalId()} style={{ paddingRight: "20px" }}> <input value={image.url} onChange={() => setSelectedImage(image.url)} checked={selectedImage === image.url} type="radio" name="image" /> <img style={{ borderRadius: "5%", paddingLeft: "5px", width: "256px", height: "144px", }} src={image.url} alt={image.alt} /> </label> ); }In this modified version, the
ImageFormcomponent now takes an additionalselectedImageprop, which is the value of the currently selected image, and asetSelectedImagefunction, to update the selected image when a radio button is clicked. This should make sure that the selected radio buttons are correctly displayed when you switch between accordions. - User
where is the declaration of this useState??
- Assistant
Apologies for the confusion. It seems I missed adding the
useStatefor tracking the selected images. You can track the selected image in theMyBlockcomponent instead of passing it from the parent component. Here's a corrected version of theMyBlockcomponent with theuseStatefor the selected image:function MyBlock(props) { const x = props.x; const [content, setContent] = useState(x.content); const [selectedImage, setSelectedImage] = useState(x.content); // Add this line for tracking the selected image // ... Rest of the existing code ... return ( <Accordion.Item eventKey={props.list.indexOf(x)}> {/* ... Existing code ... */} <Accordion.Body> {x.blockType != "Image" ? ( <Form.Control as="textarea" value={content} onChange={(ev) => { x.content = ev.target.value; setContent(ev.target.value); }}></Form.Control> ) : ( <div> {images.map((image) => ( <ImageForm image={image} key={generatelocalId()} selectedImage={selectedImage} // Pass the useState value here setSelectedImage={(url) => { x.content = url; setContent(url); setSelectedImage(url); // Update the useState value }} /> ))} </div> )} </Accordion.Body> </Accordion.Item> ); } // ... Rest of the existing code ...Now, the
MyBlockcomponent tracks the selected image with its own local state using theselectedImageandsetSelectedImagestate variables, which are passed to theImageFormcomponent. This should fix the issue with radio buttons not being displayed correctly when you switch between accordions.