I have a large set of FIELDS(which includes “text”, “select”, “radio”) and place them on our page.
I’m iterating the FIELDS objects and rendering them in the same order. Below case,
If the customer press “Yes” the comment section will be displayed If “No” we need to remove that.
In this case alone I wanted to place the input box next to the Yes option after the customer chooses “Yes”.
Like below
const FIELDS = {
'wishToAdd': {
name: 'wishToAdd',
id: 'wishToAdd',
type: 'radio',
required: true,
options: {
yes: {
displayName: 'Yes!!!',
id: 'yes',
value: 'Yes',
},
no: {
displayName: "No!!!",
id: "no",
value: 'No',
}
},
},
'comment': {
name: 'comment',
id: 'txtComment',
dataRequired: true,
value: '',
displayName: 'Comment',
type: 'text',
required: true,
maxlength: 100,
autoFocus: true,
},
}
class TextField extends React.Component {
constructor(props) {
super(props);
}
render() {
const field = this.props.field;
const values = this.props.values;
return (
<div className="input-group">
<input
type={field.type}
name={field.name}
data-required={field.dataRequired}
id={field.id}
value={values[field.name].value}
maxLength={field.maxlength}
className="form-control flashcard"
autoFocus={field.autoFocus}
/>
</div>
)
}
}
class RadioField extends React.Component {
constructor(props) {
super(props);
}
render() {
const field = this.props.field;
const options = this.props.field.options;
const values = this.props.values;
return (
<div className="check-group">
{
Object.keys(options).map((opt) => (
<div>
<label className="orgRadio" key={options[opt].id}>
<input
type={field.type}
name={field.name}
id={field.id}
value={options[opt].value}
className="form-control flashcard radio"
onChange={e => this.props.handleRadioChange(field, e)}
autoFocus={field.autoFocus}
checked={values[field.name].value === options[opt].value}
/>
<span className="lbl text-center">{options[opt].displayName}</span>
</label><br/>
</div>
))
}
</div>
)
}
}
class Fieldset extends React.Component {
constructor(props) {
super(props);
}
showField = () => {
let add = this.props.values.wishToAdd.value;
if (this.props.field.name === 'comment') {
return add === 'Yes' ? true : false;
} else {
return true
}
}
render() {
const field = this.props.field;
const values = this.props.values;
return (
<div>
{this.showField() &&
<div className='form-group org-new'>
<label htmlFor={field.id}>{field.displayName}</label>
<div className="row">
<div className="col-sm-12">
<div className="has-feedback">
{(field.type === 'text' || field.type === 'email') &&
<TextField
field={field}
values={values}
handleChange={this.props.handleChange}>
</TextField>
} {field.type === 'radio' &&
<RadioField
field={field}
values={values}
handleRadioChange={this.props.handleRadioChange}>
</RadioField>
}
</div>
</div>
</div>
</div>
}
</div>
);
}
}
class Form extends React.Component {
constructor(props) {
super(props);
}
render() {
const fields = this.props.fields;
const values = this.props.values;
return (
<div>
{
fields.map((field) => (
<div key={field.id}>
<Fieldset
field={field}
values={values}
handleChange={this.props.handleChange}
handleRadioChange={this.props.handleRadioChange}>
</Fieldset>
</div>
))
}
</div>
);
}
}
class Content extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<form method='POST'>
<h1 className='font-xl'>{this.props.header}</h1>
<Form
prevStep={this.props.prevStep || false}
nextStep={this.props.nextStep}
handleChange={this.props.handleChange}
handleRadioChange={this.props.handleRadioChange}
values={this.props.values}
fields={this.props.fields}>
</Form>
</form>
)
}
}
class Page extends React.Component {
constructor(props) {
super(props);
this.state = {
wishToAdd: {
value: '',
valid: null
},
comment: {
value: '',
valid: null,
},
}
}
handleRadioChange = (field, e) => {
this.setState({
[field.name]: {
value: e.target.value,
valid: true,
},
});
}
render() {
const { wishToAdd, comment } = this.state;
const values = { wishToAdd, comment }
let fields = [FIELDS.wishToAdd, FIELDS.comment];
return (
<div>
<Content
handleChange={this.handleChange}
handleRadioChange={this.handleRadioChange}
values={values}
fields={fields}
buttonText='Add'
header='Wish to add comment?'
/>
</div>
)
}
}
/*
* Render the above component into the div#app
*/
ReactDOM.render(
<Page />,
document.getElementById('app')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="app"></app>