import 'bootstrap-icons/font/bootstrap-icons.css'; import { useState, useEffect, useRef…
- User
import 'bootstrap-icons/font/bootstrap-icons.css'; import { useState, useEffect, useRef } from 'react'; import { Col, ListGroup, Button, Form, Accordion, Card, ButtonGroup } from 'react-bootstrap'; import { useNavigate } from 'react-router-dom'; import API from '../API'; import dayjs from 'dayjs'; import '../style.css';
//Can't get it from database because it is not saved. //This number is used only for triggering the re-render of the content of the Forms of each block showed at video. (ex when handleUp or handleDown) function generatelocalId() { return Math.random().toString(36).substring(2) + Date.now().toString(36); }
function BlockList(props) {
codefunction headerBlock() { this.blockType = "Header"; this.content = ""; } function paragraphBlock() { this.blockType = "Paragraph"; this.content = ""; } function imageBlock() { this.blockType = "Image"; this.content = ""; } const addToList = (x) => { props.setList((oldList) => [...oldList, x]); }; return ( <> <h5 style={{ textAlign: "center" }}>Components</h5> <p></p> <Card> <ListGroup> <ListGroup.Item > Header <Button className="float-end addbutton" variant="outline-success" onClick={() => { addToList(new headerBlock()); }}> <i className="bi bi-plus"></i> </Button> </ListGroup.Item> <ListGroup.Item > Paragraph <Button className="float-end addbutton" variant="outline-success" onClick={() => { addToList(new paragraphBlock()); }}> <i className="bi bi-plus"></i> </Button> </ListGroup.Item> <ListGroup.Item > Image <Button className="float-end addbutton" variant="outline-success" onClick={() => { addToList(new imageBlock()); }}> <i className="bi bi-plus"></i> </Button> </ListGroup.Item> </ListGroup> </Card> </> ); - Assistant
It seems that the focus is being set to the input element inside the
MyBlockcomponent whenever a re-render occurs, due to theuseEffecthook in that component. This hook re-focuses the input element whenever there is a change in the value of any input field (including the title form field) and causes the input element to gain focus. To prevent this refocusing issue, you can update the dependency array for theuseEffecthook in theMyBlockcomponent.Change the following piece of code in the
MyBlockcomponent:javascriptuseEffect(() => { inputEl.current?.focus(); }, [inputEl.current?.value]);to:
javascriptuseEffect(() => { inputEl.current?.focus(); }, []);By setting the dependency array to an empty array (
[]), theuseEffecthook will only execute once, when the component mounts. This will make sure that the input element gains focus only when the component is initially mounted and not whenever the value of any input field changes, preventing the issue you're facing. - User
I need to focus whenever the value of the input field changes because the onchange triggers a re-render of it so if someone is writing inside whould keep writing. Anyway if you're writing in the title it gains focus after the frst character pressed on the accordion that is opened at the time.
- Assistant
I see, in that case, you can add a local state variable for the
MyBlockcomponent to track if the input field within that component should receive focus. When the inner content of the component is changed, we'll set the state variable to true, and use this value to conditionally apply the focus in theuseEffecthook. Afterwards, set the state variable back to false to persist the focus.Here's how you can modify the
MyBlockcomponent:javascript - User
where can I put this? !(ref.current.contains(event.target))
- Assistant
You can use this condition inside a custom event handler that listens for an outside click event. This event handler will be responsible for checking if the event target element is contained within the specific input element or not.
You'll want to register and unregister this event handler with the
useEffecthook. Here's an example of how you can implement this behavior:javascript - User
- Assistant
To change the color of the form to green, you can update the
colorstate variable when the check button is clicked. Then, use a conditional class name based on thecolorstate in theForm.Controlcomponent.Here's how you can modify the check button's click event and apply the CSS class conditionally to change the color of the form:
- First, create a new CSS class in your
style.cssfile:
css.green-form { background-color: lightgreen; }- Modify the check button's click event in the
MyBlockcomponent:
javascript<Button as='div' size="sm" variant="outline-success" onClick={(ev) => { ev.stopPropagation(); handleChange(); setColor("green-form"); // Add this line to set the 'color' state variable }}> <i className="bi bi-check-lg"></i> </Button> - First, create a new CSS class in your
- User
No because handleChange triggers a re-render of all the blocks
- Assistant
You're right. As the
handleChangeupdates the content of the block, it triggers a re-render of the block. In order to avoid this, you can set a new state variable, namedupdateColor, to determine when the component should update its color.Here's how to modify the
MyBlockcomponent:- Add a new state variable
updateColor:
javascriptconst [updateColor, setUpdateColor] = useState(false);- Update the check button's onClick handler to set
updateColorto true:
javascript<Button as='div' size="sm" variant="outline-success" onClick={(ev) => { ev.stopPropagation(); handleChange(); setUpdateColor(true); // Set updateColor to true when the button is clicked }}> <i className="bi bi-check-lg"></i> </Button> - Add a new state variable