I have two input fields which are basically password fields. The disabling of these two fields are controlled by two external flags. if already set flag is true, then make this disabled and show the text as "already set". Also , there is a button next to this input field that can make this input enable and then user should be able to enter the text as password. I am clearing "Already set" text whenever I click on this edit. In my case the content is cleared, but I am unable to edit.
Can some one help me here.
Sandbox:https://codesandbox.io/s/react-functional-component-forked-8nrbb1?file=/src/index.js:0-1419
import React, { useState } from "react";
import { render } from "react-dom";
import * as data from "./messages.json";
const App = () => {
return <Test {...{ alreadySetVal1: true, alreadySetVal2: true }} />;
};
const Test = ({ alreadySetVal1, alreadySetVal2 }) => {
const [input1, setInput1] = useState(alreadySetVal1 ? "Already Set" : "");
const [input2, setInput2] = useState(alreadySetVal2 ? "Already Set" : "");
const [config, setConfig] = useState({ config1: false, config2: false });
return (
<>
<div>
<input
disabled={alreadySetVal1}
value={input1}
type={alreadySetVal1 ? "text" : "password"}
onChange={(e) => setInput1(e.target.value)}
/>
<span
onClick={() => {
setInput1("");
setConfig({ ...config, config1: true });
}}
>
Enable Input 1
</span>
</div>
<div>
<input
disabled={alreadySetVal2}
value={input2}
type={alreadySetVal2 ? "text" : "password"}
onChange={(e) => setInput2(e.target.value)}
/>
<span
onClick={() => {
setInput2("");
setConfig({ ...config, config2: true });
}}
>
Enable Input 2
</span>
</div>
</>
);
};
render(<App messages={data.messages} />, document.getElementById("root"));
