简体   繁体   中英

How to make input field editable on a button click separately in React?

I have some data coming from database which I have shown on the UI. But I want to make those field editable on a button click. I have added images below for better understanding.

Default View图片

After Clicking The Edit Button图片

As you can see from the pictures I want something like this: When I click on the edit button the corresponding field get editable and the edit button change to save button. And when clicking the save button data will be submitted to an API, and the view will be change as before. How Can I do that.

This is my simple Code

 <div className="col-sm-10 d-flex align-items-center">
    <input
    className="form-control mx-1"
    value={item.name}
    disabled
    />
    <button className="text-end">
        <i
        style={{ color: "blue" }}
        className="fas fa-edit d-block"
        ></i>
    </button>
</div>

You can use state of boolean value that will triger the disable part in the input.

 const [disableButton,setDisableButton] = useState(false)
 
<div className="col-sm-10 d-flex align-items-center">
    <input
    className="form-control mx-1"
    value={item.name}
    disabled={disableButton}
    />
    <button className="text-end" onClick={() => setDisableButton(!disableButton)}>
        <i
        style={{ color: "blue" }}
        className="fas fa-edit d-block"
        ></i>
    </button>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM