我想访问我的React-Select元素值并将其作为值传递给OnSubmit函数,并添加到从formik收集的值中。
我在Formik中创建了一个多元素表单。我在模态中访问此表格。在表单中,我可以很好地访问表单元素username, email, password
的值。 Code Below
:
handleSubmit = (values,) => {
const { onSuccess } = this.props;
console.log(values);
onSuccess(values);
};
我还有一个React-Select组件,我用label
和value
对加载一些数据。
我想将所选值添加到来自fomrik的值列表中。下面的响应仅为我提供了此信息。
selectOnChangeCallback = response => {
// eslint-disable-next-line no-console
console.log('selectOnChangeCallback', response);
};
我的React选择组件:
<ReactSelect
isMulti={false}
options={selectData}
onChangeCallback={this.selectOnChangeCallback}
/>
我可以使用以下功能在Modal内部访问它:
closeCreateUserModal = response => {
this.setState({ isCreateUserModalOpen: false });
// eslint-disable-next-line
this.props.notify({
message: `User ${response.username} was added to group `,
status: STATUS.success,
});
console.log(response)
return this.sampleGet();
};
我的表单组件:
const UserValidationSchema = Yup.object().shape({
username: Yup.string('Provide a Username').required('Username is Required'),
email: Yup.string().email('Provide a Valid email Address'),
password: Yup.string('Provide a Password').required('Password s required'),
confirmPassword: Yup.string('Provide your password again')
.required('Password Confirmation is Required')
.oneOf([Yup.ref('password')], 'Passwords do not match')
});
const INITAIL_VALUES = {
username: '',
email: '',
password: '',
confirmPassword: '',
group: {}
};
class ViewUser extends Component {
static propTypes = {
...formPropTypes,
username: PropTypes.string,
email: PropTypes.string,
password: PropTypes.string,
confirmPassword: PropTypes.string,
groupSelect: PropTypes.func,
onSuccess: PropTypes.func
};
static defaultProps = {
email: ''
};
state = {
type: 'password',
groups: []
};
componentDidMount() {
this.fetchListGroups();
}
fetchListGroups = () => {
listGroups().then(({ data }) => {
this.setState({ groups: data });
});
};
mapListGroupToSelect = () => {
const { groups } = this.state;
return groups.map(group => ({
label: group.name,
value: group.name
}));
};
selectOnChangeCallback = response => {
// eslint-disable-next-line no-console
console.log('selectOnChangeCallback', response);
};
handleSubmit = (values,) => {
const { onSuccess } = this.props;
// same shape as initial values
console.log(values);
onSuccess(values);
};
render() {
const { type, groups } = this.state;
const selectData = this.mapListGroupToSelect();
const togglePassword = type === 'password' ? 'fa fa-eye fa-lg' : 'fa fa-eye-slash fa-lg';
const classes = `${togglePassword}`;
return (
<Fragment>
<Formik
initialValues={INITAIL_VALUES}
validationSchema={UserValidationSchema}
onSubmit={this.handleSubmit}
>
{({ errors, touched }) => (
<Form>
<div className="col-7">
<div className="my-3">
<label>
<span className="font-weight-bold">Username</span>
<span className="text-danger">*</span>
</label>
<Field name="username" type="text" className="form-control rounded-0" />
{errors.username && touched.username ? (
<div className="text-danger">{errors.username}</div>
) : null}
</div>
<div className="my-3">
<label>
<span className="font-weight-bold">Email</span>
</label>
<Field name="email" type="email" className="form-control rounded-0" />
{errors.email && touched.email ? (
<div className="text-danger">{errors.email}</div>
) : null}
</div>
<div className="my-3">
<label>
<span className="font-weight-bold">Password</span>
<span className="text-danger">*</span>
</label>
<div className="d-flex align-items-center">
<Field type={type} name="password" className="form-control rounded-0 mr-2" />
<span className={classes} onClick={this.togglePasswordMask} />
</div>
{errors.password && touched.password ? (
<div className="text-danger">{errors.password}</div>
) : null}
</div>
<div className="my-3">
<label>
<span className="font-weight-bold">Confirm Password</span>
<span className="text-danger">*</span>
</label>
<Field name="confirmPassword" type={type} className="form-control rounded-0" />
{errors.confirmPassword && touched.confirmPassword ? (
<div className="text-danger">{errors.confirmPassword}</div>
) : null}
</div>
<div className="my-3">
<label>
<span className="font-weight-bold">Select Group</span>
<span className="text-danger">*</span>
</label>
<ReactSelect
isMulti={false}
options={selectData}
onChangeCallback={this.selectOnChangeCallback}
/>
</div>
</div>
<button type="submit" className="btn btn-primary rounded-0 float-right mt-5 b-2">
<span className="mx-2">Create User</span>
</button>
</Form>
)}
</Formik>
</Fragment>
);
}
}
export default ViewUser;
当前,我只能访问FORMIK中的值,但是我想将react select的信息添加到这些值中,因此每次创建用户时,我都可以显示一条带有适当信息的漂亮消息。
答案 0 :(得分:2)
我尝试类似的事情:
<Field
component={({field, form}) =>
<ReactSelect
isMulti={false}
options={selectData}
value={selectData ? selectData.find(option => option.value === field.value) : ''}
onChange{(option) => form.setFieldValue(field.name, option.value)}
onBlur={field.onBlur}
/>}
/>
还没有测试(如果可行,应该有更清洁的方法)
一旦我有足够的时间进行测试,我就会更新答案