React Fragment isn't working for Input component - javascript

Is there any other way I can write the following code without resorting to React.Fragment because that's not working.
<FormGroup>
<Input type="select" name="selectMultiPrefs" id="MultiPrefs" multiple>
{this.state.arrayForCategs.map(function (CategName, index) {
return (
<React.Fragment key={keyNum++}>
<option key={keyNum++} value={CategName} disabled>{CategName}</option>
{that.props.general.adprefrec.map(function (AdSingle, index) {
<option key={keyNum++} value={AdSingle.prefid}>{AdSingle.name}</option>
})}
</React.Fragment>
)
})}
</Input>
</FormGroup>

This isn't working because there is no such thing as <Input type="select">.
You want to use <select>.
See https://reactjs.org/docs/forms.html#the-select-tag

Related

What is the difference between these two jsx syntax in React?

I have this form that draws some fields dynamically and set the state of the form, the code is working flawlessly check the code here,
this is how the return statement looks like
return (
<div className="patientForm">
<section className="form">
<form onSubmit={onSubmit}>
{patientFormFields.map(({ title, type, values }, index) => (
<div className="form-group w-50" key={index}>
<label htmlFor={title}>{capitalize(title)}</label>
{type === "select" ? (
<select
name={title}
id={title}
value={formValue[title]}
onChange={(e) => handleChange(e, title)}
>
{values.map(({ value, text }, index) => (
<option key={index} value={value}>
{text}
</option>
))}
</select>
) : (
<input
type={type}
name={title}
id={title}
placeholder={capitalize(title)}
value={formValue[title]}
onChange={(e) => handleChange(e, title)}
/>
)}
</div>
))}
</form>
</section>
</div>
);
My problem is I don't like having my return statement be filled with logic so I tried to refactor the logic part into a separate component, check the code here,
this is my return statement now
return (
<div className="patientForm">
<section className="form">
<form onSubmit={onSubmit}>
{patientFormFields.map(({ title, type, values }, index) => (
<div className="form-group w-50" key={index}>
<label htmlFor={title}>{capitalize(title)}</label>
<FormFields type={type} title={title} values={values} />
</div>
))}
</form>
</section>
</div>
);
but it didn't work as expected, the input kept losing focus when I type, I looked up the problem and it seems that when I update my state, the component rerenders, react can't compare the two components/functions and that causes a creating a new component, I guess?! when I tried using a regular function (line-53) that returns JSX it worked correctly, check the code here.
this is my return statement now
return (
<div className="patientForm">
<section className="form">
<form onSubmit={onSubmit}>
{patientFormFields.map(({ title, type, values }, index) => (
<div className="form-group w-50" key={index}>
<label htmlFor={title}>{capitalize(title)}</label>
{FormFields(type, title, values)}
</div>
))}
</form>
</section>
</div>
);
};
The explanation sort of make sense but I don't understand why did my code work when I converted the JSX call in the component return to a regular function, aren't they both functions? and shouldn't my second code also be buggy since functions are reference type, and react shouldn't be able to compare them, I feel there is something missing that I don't understand. Also, what would be a better way to refactor that logic away from the component return?
edit: add code snippet to the problem

react hook form, A component is changing a controlled input of type hidden to be uncontrolled

i have a problem with use onChange method inside a Controller tag from react-hook-form. I need capture when the user select a field to render a new one downside, with information relationship in the last one.
Im using react-hook-form with MUI
<FormControl>
<InputLabel>Universidad</InputLabel>
<Controller
as={
<Select placeholder="Universidad">
{universities.map((item, i) => {
return (
<MenuItem value={item.value} key={i}>
{item.label}
</MenuItem>
);
})}
</Select>
}
name="university"
control={control}
onChange={([event]) => {
console.log(event.target.value);
}}
defaultValue=""
/>
</FormControl>

How to populate dropdown menu in React with entries from an excel file?

So currently, this is what I have to populate my dropdown menu. I use React Hook Form to create my dropdown. Currently, the options are hard-coded, but I want to read data from an excel or cvs file and then populate my options from that data. I have googled but most show up as populate from json file. I want to populate from excel file.
Thank you!
<h5 for="secondaryControls">Secondary controls-other </h5>
<div className="float-left">
{fields.map((field, idx) => {
return (
<div key={`${field}-${idx}`}>
<select name="primaryControls" ref={register}>
<option value="Component3">->Link: Lift /Transfer Seat </option>
<option value="Component4">->ASENTO – XL-SEAT: Lift /Transfer Board</option>
value={field.value}
onChange={e => ChangeItem(idx, e)}
</select>
<input
type="text"
style={{width: "370px"}}
value={field.value}
onChange={e => ChangeItem(idx, e)}
/>
<button type="button" onClick={() => RemoveDropDown(idx)}>
X
</button>
<button type="button" onClick={() => NewDropDown()}>
+
</button>
<br /><br />
</div>
);
})}
</div>
You need to first convert your excel file to a JSON. There are multiple libraries out there which will help you do that, one of them is convert-excel-to-json.
Once your file is converted to JSON, then you can easily loop over the object to create dropdown options.

React - Show/Hide multiple divs from different check boxes

I am new in react, I have certain domain in JS, I imagine this is a simple question but I have been searching for 2 hours and I haven't found a satisfactory answer :(
I have the check boxes:
<Col md="4">
<FormGroup>
<Label>Have Children</Label>
<CustomInput type="radio" name="customRadio" label="Yes" value="yes" />
<CustomInput type="radio" name="customRadio" label="No" value="no" />
</FormGroup>
</Col>
<Col md="4">
<FormGroup>
<Label>Have Spouse</Label>
...(same as children input)
</FormGroup>
</Col>
<Col md="4">
<FormGroup>
<Label>Have Family Members</Label>
...(same as children input)
</FormGroup>
</Col>
If you click on "have children"
I want that some divs after, related to children, to be displayed. If not, they should be hidden. And the same for the other options.
I want to know what is the cleanest way to do this.
The checkbox should toggle a boolean in state.
You then conditionally render the children if the value in state is true.
So attach a checked value and change handler to your groups:
<FormGroup>
<Label>Have Children</Label>
<CustomInput type="radio" name="customRadio" label="Yes" value="yes" onChange={() => this.handleChange} checked={this.state.visibility} />
<CustomInput type="radio" name="customRadio" label="No" value="no" onChange={() => this.handleChange} checked={!this.state.visibility}/>
</FormGroup>
The create the state value in your class and toggle it with the handleChange method
state = {visibility: false}
handleChange = () => this.setState({visibility: !this.state.visibility})
Then you can conditionally render your thing based on the visibility boolean, so...
<div>{this.state.visibility && <p>Now You see me</p>}</div>
Just think of the data structure beforehand.
From your question, I assume you simply want something like this:
If children checkbox is checked, show elements related to children
If spouse checkbox is checked, show elements related to spouse.
etc.
The way I see it, you need an array of checkboxes with each of their statuses (checked or not). You can store this information in the component state.
// in the constructor
this.state = {
checkboxes: [
{ id: 1, label: 'children', checked: false },
{ id: 2, label: 'spouse', checked: false },
],
}
Then, in your render() function, you just need to loop through the array like this:
const { checkboxes } = this.state;
return (
<div>
{checkboxes.map((checkbox, index) => (
<div>
<label>{checkbox.label}</label>
<input type="checkbox" checked={checkbox.checked} onClick={() => this.toggleCheckBox(index)} />
{checkbox.checked && <div>show this if the checkbox is checked</div>}
</div>
))}
</div>
);
Just remember to implement this.toggleCheckBox method in the class. If you are unsure of how to do it, here's a codesandbox for you to check out.

why label is display inside select box?

why my select box label display inside the select box .Take a example i am not using react -material-validator .it show like this
https://codesandbox.io/s/5vr4xp8854
when i tried to validate my select box using react-material-ui-form-validator plugin my label come inside the select bx why
here is my code
plugin:
https://www.npmjs.com/package/react-material-ui-form-validator
https://codesandbox.io/s/38x8q8zpm5
Secondly when I submit my label is not display in red color why ?
function App() {
return (
<div className="App">
<ValidatorForm onSubmit={() => {}} className="" autoComplete="off">
<FormControl>
<InputLabel shrink={true} htmlFor="age-simple">
Age
</InputLabel>
<SelectValidator
required
value=""
name="name"
displayEmpty
validators={["required"]}
errorMessages={["this field is required", "email is not valid"]}
inputProps={{
name: "age",
id: "age-simple",
shrink:true
}}
SelectProps={{
displayEmpty: true,
shrink:true
}}
input={<Input id="age-simple" />}
className=""
>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</SelectValidator>
</FormControl>
<Button type="submit"> submit</Button>
</ValidatorForm>
</div>
);
}
Looks like Material UI FormValidator package simply takes a label property. You should remove
<InputLabel htmlFor="age-simple">
Age
</InputLabel>
and add the label and InputLabelProps with shrink: true (as discussed in the limitations Here ) properties for your SelectValidator, for example:
<SelectValidator
required
label="Age"
InputLabelProps={{ shrink: true }}
value=""
name="name"
.......
This will also fix your label not appearing in red when the user hits submit without a selection.

Categories