I have a strange problem with my form. I can add and remove fields with two several buttons, but the remove button has stopped working. I can see that on clicking still the action "##redux-form/ARRAY_REMOVE" gets fired, but the field does not disappear. I don't really know why it stopped working, I tried to undo all the code I wrote afterwards, but wasn't able to track the error down. Would appreciate any input. Here is my code:
import React from 'react';
import TextField from 'material-ui/TextField';
import { Field, FieldArray, reduxForm} from 'redux-form';
import validate from './validate';
import injectTapEventPlugin from 'react-tap-event-plugin';
injectTapEventPlugin(); //Needed, otherwise an error message is shown in the console
const renderTextField = ({input, label, meta: {touched, error}, ...custom}) => (
<TextField
hintText={label}
floatingLabelText={label}
errorText={touched && error}
{...input}
{...custom}
/>
);
const renderUsers = ({fields, meta: { touched, error }}) => (
<div>
<div>
<button className="btn btn-primary"
type="button" onClick={() => fields.push({})}>
<span className="glyphicon glyphicon-plus-sign"/> Add User
</button>
{touched && error && <span>{error}</span>}
</div>
{fields.map((index) =>
<div key={index}>
<Field name={`${index}.firstName`} component={renderTextField} label="First Name"/>
<Field name={`${index}.lastName`} component={renderTextField} label="Last Name"/>
<Field name={`${index}.position`} component={renderTextField} label="Position"/>
<Field name={`${index}.email`} component={renderTextField} label="Email"/>
<button className="btn btn-xs btn-danger"
type="button"
title="Remove User"
onClick={() => fields.remove(index)}>
<span className="glyphicon glyphicon-minus-sign"/>
</button>
</div>
)}
</div>
);
const UserCreation = (props) => {
const { handleSubmit, pristine, reset, submitting} = props;
return (
<form onSubmit={handleSubmit}>
<FieldArray name="users" component={renderUsers}/>
<div>
<button className="btn btn-primary btn-success"
type="submit"
disabled={pristine || submitting}>
<span className="glyphicon glyphicon-send" />
Submit
</button>
{' '}
<button type="button"
className="btn btn-primary btn-danger"
disabled={ pristine || submitting}
onClick={reset}>
Cancel
</button>
</div>
</form>
);
}
export default reduxForm({
form: 'UserCreationForm',
validate
})(UserCreation);
Here is a snapshot from my console when I try to remove the field:
console.log of action
I fixed it. I added a second key to the map function:
{fields.map((user, index) =>
<div key={index}>
<Field name={`${user}.firstName`} component={renderTextField} label="First Name"/>
<Field name={`${user}.lastName`} component={renderTextField} label="Last Name"/>
<Field name={`${user}.position`} component={renderTextField} label="Position"/>
<Field name={`${user}.email`} component={renderTextField} label="Email"/>
Related
I have a React frontendand I have styled it using Material UI design.I added the Date and Time component styling to my form and now the form is not able to take up the "name" value and further process with the posting.The prob is only happening in the Date and time input feilds here is the error "TypeError: Cannot read properties of undefined (reading 'name')"....
Pls check out my code thanks.
function ReservationForm(){
const navigate = useNavigate();
const params = useParams();
const {user}=useContext(Cont)
const[reservationData,setReservationData]=useState({
name:"",
date:"",
time:"",
num:"",
contact:"",
occasion:"",
});
function handleReservationChange(event){
setReservationData({
...reservationData,
[event.target.name]: event.target.value,
})
}
function handleReservationSubmit(event){
event.preventDefault();
const newReservation={
...reservationData,
restaurant_id: params.id,
user_id: user.id,
};
fetch(`/reservations`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(newReservation),
})
.then((r) => r.json())
.then(
setReservationData({
name:"",
date:"",
time:"",
num:"",
contact:"",
occasion:"",
})
);
navigate("/myreservations");
}
return(
<>
<Box
component="form"
sx={{
"& > :not(style)": { m: 1 },
}}
noValidate
autoComplete="off"
onSubmit={handleReservationSubmit}
>
<h1 className="editheadings">Reserve 🍽️</h1>
<FormControl className="inputstyle">
<InputLabel htmlFor="component-outlined">Name</InputLabel>
<OutlinedInput
type="text"
name="name"
id="name"
value={reservationData.name}
onChange={handleReservationChange}
label="Name"
/>
</FormControl>
<br />
<FormControl>
<LocalizationProvider name="date" dateAdapter={AdapterDayjs} fullWidth>
<DatePicker
name="date"
label="Date"
value={reservationData.date}
onChange={handleReservationChange}
renderInput={(params) => <TextField {...params} />}
/>
</LocalizationProvider>
</FormControl>
<FormControl>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<TimePicker
name="time" label="Time"
value={reservationData.time} onChange={handleReservationChange}
renderInput={(params) => <TextField {...params} />}
/>
</LocalizationProvider>
</FormControl>
<br />
<FormControl className="inputstyle">
<InputLabel htmlFor="component-outlined">No. of Guests</InputLabel>
<OutlinedInput
type="number"
name="num"
value={reservationData.num} onChange={handleReservationChange}
/>
</FormControl>
<br />
<FormControl className="inputstyle">
<InputLabel htmlFor="component-outlined">Contact</InputLabel>
<OutlinedInput
type="tel"
name="contact"
value={reservationData.contact} onChange={handleReservationChange}
placeholder="contact"
/>
</FormControl>
<br />
<FormControl className="inputstyle">
<InputLabel htmlFor="component-outlined">Occasion</InputLabel>
<OutlinedInput
type="text"
name="occasion"
value={reservationData.occasion} onChange={handleReservationChange}
/>
</FormControl>
<Stack paddingLeft={15} direction="row" id="loginbutton">
<ColorButton variant="contained" type="submit">
{" "}
Update Reservation
</ColorButton>
</Stack>
</Box>
</>
)
}
export default ReservationForm;
Since DatePicker and TimePicker onChange events send the new value instead of event, you need to add new handleChange function for DatePicker and TimePicker components like:
function handleReservationChangeWithNameAndValue(name, newValue) {
setReservationData({
...reservationData,
[name]: newValue
});
}
and pass it in DatePicker and TimePicker components accordingly:
<DatePicker
name="date"
label="Date"
value={reservationData.date}
onChange={(newVal) =>
handleReservationChangeWithNameAndValue("date", newVal)
}
renderInput={(params) => <TextField {...params} />}
/>
<TimePicker
name="time"
label="Time"
value={reservationData.time}
onChange={(newVal) =>
handleReservationChangeWithNameAndValue("time", newVal)
}
renderInput={(params) => <TextField {...params} />}
/>
You can take a look at this sandbox for a live working example of your form with this function.
Better to check this function.
function handleReservationChange(event){
setReservationData({
...reservationData,
[event.target.name]: event.target.value,
})
}
Based on this function, every field should have name property. But as you can see some of them have not that property.
I have successfully selected a radio button. My problem is I want to include the selection with the label also. The labels/words should be clickable also.
Here's my codesandbox
CLICK HERE
<RadioButton
label="Food"
name="radio"
value="optionA"
checked={select === "optionA"}
handleChange={(event) => handleSelectChange(event)}
/>
<RadioButton
label="Water"
name="radio"
value="optionB"
checked={select === "optionB"}
handleChange={(event) => handleSelectChange(event)}
/>
Just custome like this: https://codesandbox.io/s/react-styled-components-radio-button-forked-efxzd?file=/src/Radio.js:1554-1613
In the App:
const handleSelectChange = (value) => {
setSelect(value);
};
<RadioButton
handleChange={handleSelectChange}
/>
And in the RadioButton:
const handleChecked = () => {
handleChange(value);
};
<Item onClick={handleChecked}>
just include the radio button inside <label> tag
<label>
<RadioButton
label="Food"
name="radio"
value="optionA"
checked={select === "optionA"}
handleChange={(event) => handleSelectChange(event)}
/>
</label>
<label>
<RadioButton
label="Water"
name="radio"
value="optionB"
checked={select === "optionB"}
handleChange={(event) => handleSelectChange(event)}
/>
</label>
I have a form which needs its css Display to be set to block when I click on a certain button. When I click on the Add button id="add" it should set the css style block for id="modalContent" div.
I've just started react and am completely new to it. I read something about ref but couldn't completely understand how to go through with it.
AddFormMod.tsx
import React from 'react';
import './App.css';
function AddForm(){
return (
<div id="modalContent" className="modal-content">
<h1 id="headerAdd">ADD NEW CONTACT</h1>
<form action="#" id="myForm">
<label className="required label" ><b>Name: </b></label><br />
<input className="form-fields type1" type="text" id="name" name="name" required></input><br/><br/>
<label className="required label" ><b>Email:</b> </label><br/>
<input className="form-fields type1 " type="email" id="email" name="mail" required></input><br/><br/>
<label className="required label" ><b>Mobile:</b> </label>
<label className="label" id="landlinelabel" ><b>Landline:</b></label><br/>
<input className="form-fields" type="tel" id="mobile" name="mobile" pattern="^\d{10}$" required></input>
<input className="form-fields" type="tel" id="landline" name="landline" ></input><br/><br/>
<label className="label" ><b>Website</b></label><br/>
<input className="form-fields type1" type="text" id="website" name="website" ></input><br/><br/>
<label className="label"><b>Address:</b> </label><br/>
<textarea className="addressstyle form-fields" id="address1" name="address1" rows={9} cols={74}></textarea>
<input className = "buttonstyle" type="submit" value="Add" id="adddetails" ></input>
<input className = "buttonstyle" type="button" value="Cancel" id="candetails"></input>
</form>
</div>
);
}
export default AddForm;
App.tsx
import React from 'react';
import './App.css';
import AddForm from './AddFormMod';
function App() {
return (
<p id="title"> Address Book </p>
);
}
function AddHome(){
return (
<div>
<button id="home" >HOME</button>
<button id="add" onClick={} >+ADD</button>
</div>
);
}
function ContactBar(){
return (
<div>
<p id="contacts">CONTACTS</p>
</div>
);
}
export { App , AddHome, ContactBar };
One approach to achieve the result you want, is to utilize conditional rendering. For example, when you click the "add"-button in your AddHome component, you can set a state variable to render the AddForm-component:
function AddHome(){
const [shouldRenderForm, setShouldRenderForm] = useState(false);
return (
<div>
<button id="home" >HOME</button>
<button id="add" onClick={() => setShouldRenderForm(true)} >+ADD</button>
{shouldRenderForm && <AddForm />}
</div>
);
}
I'm also guessing you want to "close the form" after submit or via a close button inside the AddForm-component. To achieve this, simply pass a prop to the component, for the AddForm-component to call to close the form:
// ... in the add AddHome component:
{shouldRenderForm && <AddForm closeForm={() => setShouldRenderForm(false)} />}
// ... in AddForm component:
type AddFormProps = { closeForm: () => void };
function AddForm({ closeForm }: AddFormProps) {
// ... i.e. on a button to close the form
<button type="button" onClick={() => closeForm()}>Cancel</button>
}
Checkout a working example in this sandbox.
I've created a redux-form and i want to add className to each Field to customize them with css.
The code for each field is:
<Form onSubmit={handleSubmit(requestAccountsFilter)}>
<FormGroup row>
<Field
id="symbol"
name="symbol"
type="text"
component={inputField}
placeholder="Enter Product Here"
/>
<Field id="side" name="side" component={inputField} type="select">
<option value={null}>Any</option>
<option value="Buy">Buy</option>
<option value="Sell">Sell</option>
</Field>
<Field id="status" name="status" component={inputField} type="select">
<option value={null}>Any</option>
<option value="Working">Working</option>
<option value="Completed">Completed</option>
</Field>
<Button name="submit-btn" className="filter-submit-btn" color="danger" type="submit">
Submit
</Button>
</FormGroup>
</Form>
I've added a className tag but i see that neither the placeholder i've added is shown nor the className. How can i customize each field?
<Field
type="text"
className="myClass"
component={InputField}
placeholder="Type here..."
/>
and your custom InputField should be something like
(I've taken this example from http://redux-form.com/6.5.0/examples/submitValidation/)
export const InputField = ({ input, type, placeholder, className, meta: { touched, error } }) => (
<div>
<input {...input} placeholder={placeholder} type={type} className={className}/>
{meta.touched && meta.error && <span>{meta.error}</span>}
</div>
)
or a better approach, if too many props are there, You can use object destructuring
export const InputField = (field) => (
<div>
<input {...field.input} {...field} />
{field.meta.touched && field.meta.error && <span className="error">{field.meta.error}</span>}
</div>
)
{...field} will extract all props that you have passed in Field.
You can take a look at this official redux-form example: http://redux-form.com/6.5.0/examples/react-widgets/ to get more idea.
Hope it helps :)
You can use object destructuring method to set className.
<Field
type="text"
input={{className:'red'}}
component={InputField}
placeholder="Type here..."
/>
I realize that you are using a custom renderer by saying component={InputField}, but for others coming here (since I can't find it in the docs): if you are using one of the built-in renderers like component="input" or component="select", you can just add className and the renderer will apply it, e.g.:
<Field name="foo" component="select" className="form-control">
</Field>
By definition, whatever you pass to the Field component should be passed to the InputField component as input prop. So, your InputField Component should look something like this:
<div>
<InputField {...field.input} > {field.children} </InputField>
{field.meta.touched && field.meta.error && <span className="error">
{field.meta.error}</span>}
</div>
I have a component that successfully uses redux-form onSubmit to call an action creator. The creator performs an ajax call but the the action is never dispatched to save it to the store. I must have something messed up in the wiring of react-redux and redux-form, possibly in the binding of the action creator. I have read everything that I have found on Google and still can't find the problem. Any ideas? ( I have included redux-promise to handle the request promise, but it never makes it that far )
import React from 'react';
import { Field, reduxForm } from 'redux-form';
import validate from '../utils/add_person_validation';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { addPersonResponseAction } from '../actions/index';
import renderField from '../components/render_input_field';
// call the action creator - this part succeeds
const doSubmit = function(values) {
addPersonResponseAction(values);
};
let AddPersonContainer = (props) => {
const {
handleSubmit,
pristine,
reset,
submitting
} = props;
return (
<div className="row">
<form onSubmit={handleSubmit(doSubmit)} >
<div className="col-sm-6">
<fieldset>
<legend>Person Info</legend>
<div className="form-group">
<Field name="personFirstName" component={renderField} type="text" label="First Name" className="form-control" />
<Field name="personLastName" component={renderField} type="text" label="Last Name" className="form-control" />
<Field name="birthday" component={renderField} type="date" label="Birthday" className="form-control" />
<Field name="group" component={renderField} type="text" label="Group" className="form-control" />
</div>
</fieldset>
</div>
<div className="form-buttons-container">
<button className="btn btn-default form-button" type="submit" disabled={pristine || submitting}>Submit</button>
<button className="btn btn-default form-button" type="button" disabled={pristine || submitting} onClick={reset}>Clear Values</button>
</div>
</form>
</div>
);
};
const mapStateToProps = function({ addPersonResponse }) {
return { addPersonResponse };
};
const mapDispatchToProps = function(dispatch) {
return bindActionCreators( {addPersonResponseAction}, dispatch);
};
const form = reduxForm({ form: 'addPerson', validate: validate });
AddPersonContainer = connect(mapStateToProps, mapDispatchToProps)(form(AddPersonContainer));
export default AddPersonContainer;
/********************************************
* Action creator
**********************************************/
import axios from 'axios';
export const ADD_PERSON_RESPONSE = 'ADD_PERSON_RESPONSE';
export const addPersonResponseAction = (data) => {
const postURL = 'http://some-url/addperson';
const request = axios.post(postURL, { data });
return {
type: ADD_PERSON_RESPONSE,
payload: request
};
};
Redux wraps actions using mapDispatchToProps - but you are calling the unwrapped version by using the imported method.
// call the action creator - this part succeeds
const doSubmit = function(values) {
addPersonResponseAction(values); <------ Redux does not know anything about this
};
Try:
let AddPersonContainer = (props) => {
const {
handleSubmit,
pristine,
reset,
submitting
} = props;
const doSubmit = function(values) {
props.addPersonResponseAction(values); <----- Try this
}
return (
<div className="row">
<form onSubmit={handleSubmit(doSubmit)} >
<div className="col-sm-6">
<fieldset>
<legend>Person Info</legend>
<div className="form-group">
<Field name="personFirstName" component={renderField} type="text" label="First Name" className="form-control" />
<Field name="personLastName" component={renderField} type="text" label="Last Name" className="form-control" />
<Field name="birthday" component={renderField} type="date" label="Birthday" className="form-control" />
<Field name="group" component={renderField} type="text" label="Group" className="form-control" />
</div>
</fieldset>
</div>
<div className="form-buttons-container">
<button className="btn btn-default form-button" type="submit" disabled={pristine || submitting}>Submit</button>
<button className="btn btn-default form-button" type="button" disabled={pristine || submitting} onClick={reset}>Clear Values</button>
</div>
</form>
</div>
);
};
Because the function you are defining does not have access to props this gives a bit of a twist, so try refactoring it into the component definition.
This is a source of confusion because it is required to import the function so mapDispatchToProps can wrap it...but there is temptation to forget that the real action is in props, not the function itself. So I'm sure you are seeing results in the actual action function, but redux does not know because it is not wrapped in dispatch.