React checkbox and button control - html

I am trying to implement a check box for the full consent of membership registration.
const [agree1, setAgree1] = useState(false); //agree1
const [agree2, setAgree2] = useState(false); //agree2
const [agree3, setAgree3] = useState(false); //agree3
const [total, settotal] = useState(false); // all agree
const buttonState = useCallback(() => {
if((agree1===true)&&(agree2===true)&&(agree3===true)){
settotal(true);
setDisabled('');
}
else {
setDisabled('disabled');
}
},[agree1,agree2,agree3,total]);
const totalchange = () => {
if(total ===true) { //전체동의가 true라면 다시 클릭 했을때 전부 unchecked
settotal(!total);
setAgree1(!agree1);
setAgree2(!agree2);
setAgree3(!agree3);
} else{ //그외(하나만 체크되 있거나 아무것도 없다면) 전부 checked로 만듬
settotal(true);
setAgree1(true);
setAgree2(true);
setAgree3(true);
// setDisabled('');
}
}
<div>
<div>
<input type="checkbox" name="total_agree" value="total_agree" checked={total} onChange={totalchange} />
<label>전체 동의</label>
</div>
<div>
<input type="checkbox" name="agree1" value="agree1" checked={agree1} onChange={changeState1} />
<label>회원 약관</label>
<span>전체보기</span>
</div>
<div>
<input type="checkbox" name="agree2" value="agree2" checked={agree2} onChange={changeState2} />
<label>개인정보 수집 및 이용</label>
<span>전체보기</span>
</div>
<div>
<input type="checkbox" name="agree3" value="agree3" checked={agree3} onChange={changeState3} />
<label>위치 정보 이용 동의</label>
<span>전체보기</span>
</div>
</div>
<br/>
입력해주신 이메일 정보는 회원가입 시 이메일로 설정됩니다.
<br/>
<div>
<label>이메일</label>
<input type = "email" onChange = {EmailHandler}/>
<button name="button" onClick = {clickFunction}>인증코드 발송</button>
</div>
If you activate the full terms and conditions, all the checkboxes below will be activated.
If you deactivate all terms and conditions, all the checkboxes below will also be deactivated.
Individually, all checkboxes should be enabled.
If you activate all checkboxes individually, the entire terms and conditions will also be activated.
If any of the checkboxes below are deactivated, the entire terms and conditions are deactivated.
When the entire terms and conditions are activated, the member sign-up button is activated.
I tried several methods, but failed to activate the button.
How can I implement it?
Please help...

Essentially what you need to do here is remove the states for total and disabled as they can be derived from the state of the other checkboxes.
The checkbox for "total" is checked if and only if all three are checked. When it is checked or unchecked, we handle that by setting the state of the other three.
Code Sandbox Demo
const Form = ({ onSubmit }) => {
const [agree1, setAgree1] = useState(false);
const [agree2, setAgree2] = useState(false);
const [agree3, setAgree3] = useState(false);
const isAgreedAll = agree1 && agree2 && agree3;
const handleCheckAll = (e) => {
setAgree1(e.target.checked);
setAgree2(e.target.checked);
setAgree3(e.target.checked);
};
const [email, setEmail] = useState("");
// placeholder so that you can implement your own validation
const emailLooksValid = email.length > 5;
const isDisabled = !isAgreedAll || !emailLooksValid;
return (
<>
<div>
<div>
<input
type="checkbox"
name="total_agree"
value="total_agree"
checked={isAgreedAll}
onChange={handleCheckAll}
/>
<label>전체 동의</label>
</div>
<div>
<input
type="checkbox"
name="agree1"
value="agree1"
checked={agree1}
onChange={(e) => setAgree1(e.target.checked)}
/>
<label>회원 약관</label>
<span>전체보기</span>
</div>
<div>
<input
type="checkbox"
name="agree2"
value="agree2"
checked={agree2}
onChange={(e) => setAgree2(e.target.checked)}
/>
<label>개인정보 수집 및 이용</label>
<span>전체보기</span>
</div>
<div>
<input
type="checkbox"
name="agree3"
value="agree3"
checked={agree3}
onChange={(e) => setAgree3(e.target.checked)}
/>
<label>위치 정보 이용 동의</label>
<span>전체보기</span>
</div>
</div>
<br />
입력해주신 이메일 정보는 회원가입 시 이메일로 설정됩니다.
<br />
<div>
<label>이메일</label>
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<button
disabled={isDisabled}
name="button"
onClick={onSubmit(email)}
>
인증코드 발송
</button>
</div>
</>
);
};
export default Form;
If you want to, you could combine some of the logic for the three checkboxes into a renderCheckbox function.

If you don't want/need button visible prior to being active I like to use && for display. ex:
( isFlag1 && isFlag2 && isFlag3 && <button>Active Now</button>)

Related

How to customize file input in react.js

This is the image I need as a output can anybody let me know any solution to this. Here I want the file path to display in place of "set output path" I have already given the css to the button and its uploading the file but I need the path or file name at the place of "set output path"
Function DataExtractor(){
const uploadFiles=()=>{
document.getElementById('selectFile').click()
}
return(
<div>
<label>Set Output Path:</label>
<input type="text" placeholder="Set output path">
</div>
<div>
<button className="btn-1" onClick={uploadFiles}>Browse path</button>
<input type="file" id="selectFile" style={{display:"none"}}/>
</div>)}
)}
export default DataExtractor
You can access the file name as e.target.files[0] in onChange function. Something like this:
function DataExtractor() {
const [selectedFile, setSelectedFile] = useState('')
const uploadFiles = () => {
document.getElementById('selectFile').click()
}
const handleFileChange = (e) => {
setSelectedFile(e.target.files[0])
}
return (
<>
<div>
<label>Set Output Path:</label>
<input value={selectedFile} type="text" placeholder="Set output path">
</div>
<div>
<button className="btn-1" onClick={uploadFiles}>Browse path</button>
<input type="file" id="selectFile" style={{display:"none"}} onChange={handleFileChange} />
</div>
</>
)
}

Using form labels for multiple elements

My page has many js-generated form elements which can exist simultaneously but need to be created from the same block of code (jsx, react). My problem is that I need to give labels to all these form elements for semantics and seo, but I can't give them all the same id (becuase html ids are unique). Is the only option to dynamically generate unique html ids? Is there any way to do this with html classes?
My code would look something like this:
function cardEditor() {
return (
<form>
<label for="card-title">Title: </label>
<input type="text" id="card-title">
<button type="submit">Save</button>
</form>
);
}
You can pass id as props to your component
const Input = ({ id }) => {
return <>
<label for={id}>Title: </label>
<input type="text" id={id}>
</>
}
function cardEditor() {
return (
<form>
<Input id="card-title-1" />
<Input id="card-title-2" />
<Input id="card-title-3" />
<button type="submit">Save</button>
</form>
);
}
or generate unique ids for your form elements
const uuid = () => Math.random().toString(36).slice(-6);
const Input = () => {
const id = uuid();
return <>
<label for={id}>Title: </label>
<input type="text" id={id}>
</>
}
function cardEditor() {
return (
<form>
<Input/>
<Input/>
<Input/>
<button type="submit">Save</button>
</form>
);
}

Input text no longer editable when using register with react-hook-form

I'm trying to set up react-fook-form to validate my forms, but when I use the register function on a text input (in my case the username input), this input is no longer editable, I can't type anything inside.
const {register, handleSubmit, formState: { errors }} = useForm();
const [username, setUsername] = useState('');
.....
<form onSubmit={handleSubmit(onSubmit)}>
<label htmlFor="username">Username : </label>
<input type="text" value={username}
onChange={e => setUsername(e.target.value)}
{...register('username', { required: 'Please, type in your username' })}
/>
{errors.username && <span style={{color: 'red'}}><br/>{errors.username.message}</span>}
<br/>
<label htmlFor="password">Password : </label>
<input type="password" value={password} onChange={e => setPassword(e.target.value)}/>
<br/>
<button type="submit">Login</button>
</form>
Ok I finally found the solution myself.
Since the version 7 of react-hook-form library, we have to place "onChange" inside the register function like so :
<input type="text" value={username} {...register('username', {
required: 'Please, type in your username',
onChange: e => setUsername(e.target.value)
})}/>

CheckBox with variable default value

What's the best way to create a checkbox that gets a default status but can still be modified.
<div className="master">
<input className="tgl tgl-skewed" id={id} type="checkbox" onChange={onChange} name={flag} checked={lock}/>
<label className="slave tgl-btn" data-tg-off={flag + " is OFF"} data-tg-on={flag + " is ON"} htmlFor={id}></label>
</div>
In this example above I get a default status (lock) that can be "true" or "false" this changes the checkbox from "checked" to "unchecked". Unfortunately this also makes it impossible to change this status by clicking on the relevant checkbox.
Any ideas ?
You can use defaultChecked props for the input tag in react.
<input className="tgl tgl-skewed" id={id} type="checkbox" onChange={onChange} name={flag} defaultChecked={lock}/>
Put the lock into state, and have the change handler toggle the boolean:
const App = ({ lock }) => {
const [checked, setChecked] = React.useState(lock);
const onChange = () => setChecked(!checked);
const id = 'foo';
return (
<div className="master">
<input className="tgl tgl-skewed" id={id} type="checkbox" onChange={onChange} checked={checked}/>
<label className="slave tgl-btn" htmlFor={id}></label>
</div>
);
};
ReactDOM.render(<App lock={true} />, document.querySelector('.react'));
<script crossorigin src="https://unpkg.com/react#16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom#16/umd/react-dom.development.js"></script>
<div class="react"></div>

React - Deselecting the checkbox doesn't make div to dissapear

I'm currently messing around with Reactjs and don't know where exactly my problem lays. I wan't to render an input field if a certain chekbox is selected (requiredRoleResponsible in this case). If i check it first time to true it works fine, but if I deselect it, it won't disappear. Maybe i missing something?
class ProccessForm extends React.Component {
constructor(props) {
super(props)
this.state = { name: '', description: '', requireSupervisor: false, requireRoleResponsible: false, roleResponsible: '' }
this.handleNameChange = this.handleNameChange.bind(this);
this.handleDescriptionChange = this.handleDescriptionChange.bind(this);
this.handleSupervisorCheckbox = this.handleSupervisorCheckbox.bind(this);
this.handleRoleResponsibleCheckbox = this.handleRoleResponsibleCheckbox.bind(this);
this.handleRoleResponsibleChange = this.handleRoleResponsibleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(e, data) {
e.preventDefault();
const name = this.state.name.trim();
const description = this.state.description.trim();
if (!name || !description)
return;
this.props.onProcessSubmit({ name: name, description: description });
this.setState({ name: '', description: '' });
}
handleSupervisorCheckbox(e) {
this.setState({requireSupervisor: Boolean(e.target.value)});
}
handleRoleResponsibleCheckbox(e) {
this.setState({requireRoleResponsible: Boolean(e.target.value)});
}
handleRoleResponsibleChange(e) {
this.setState({roleResponsible: e.target.value});
}
handleNameChange(e) {
this.setState({ name: e.target.value })
}
handleDescriptionChange(e) {
this.setState({ description: e.target.value })
}
render() {
return (
<div className="panel-body">
<form className="processForm" onSubmit={this.handleSubmit}>
<div className="form-group">
<label htmlFor="inputName">Name</label>
<input className="form-control" id="inputName" type="text" placeholder="Prozessname" value={this.state.name} onChange={this.handleNameChange} />
</div>
<div className="form-group">
<label htmlFor="inputDescription">Beschreibung</label>
<textarea className="form-control" id="inputDescription" type="text" placeholder="Prozessbeschreibung" value={this.state.description} onChange={this.handleDescriptionChange} />
</div>
<span>Welche Genehmigungen werden benötigt?</span>
<div className="checkbox">
<label><input type="checkbox" value={this.state.requireSupervisor} onChange={this.handleRoleResponsibleCheckbox}/>Vorgesetzer</label>
</div>
<div className="checkbox">
<label><input type="checkbox" value={this.state.requireRoleResponsible} onChange={this.handleRoleResponsibleCheckbox}/>Rollenverantwortlicher</label>
</div>
{this.state.requireRoleResponsible ?
<div className="form-group">
<label htmlFor="inputRoleResp">Name</label>
<input className="form-control" id="inputRoleResp" type="text" placeholder="Rollenverantwortlicher" value={this.state.roleResponsible} onChange={this.handleRoleResponsibleChange} />
</div> : null}
<div>
<input type="submit" value="erstellen" />
</div>
</form>
</div>
);
}
}
I tried different approaches I seen online so far. Making the second part of the if to null like currently in my code. Just to use this.state.requireRoleResponsible && the to render part and in the handler to make explicitely a boolean out of the value
Thanks for your help
For checkbox you need to use checked instead of value.
Checked will determined if the checkbox is checked or not. Value is just a property to hold.
In this case you need to change:
<input type="checkbox" value={this.state.requireRoleResponsible}
onChange={this.handleRoleResponsibleCheckbox}/>Rollenverantwortlicher</label>
To:
<input type="checkbox" checked={this.state.requireRoleResponsible}
onChange={this.handleRoleResponsibleCheckbox}/>Rollenverantwortlicher</label>
And change the handler to:
handleRoleResponsibleCheckbox(e) {
this.setState({requireRoleResponsible: e.target.checked});
}
With checkbox you should use checked instead of value ( should use value attr for type: text , select , ... )
And in 2 function handler for your checkbox , i think you can use like this :
handleRoleResponsibleCheckbox(e) {
//this.setState({requireRoleResponsible: Boolean(e.target.value)});
this.setState((prevState)=>{
return {
requireRoleResponsible: !prevState.requireRoleResponsible
}
})
}
with prevState is current state before you update