我想访问我的阵营,选择元素值,将它作为价值在了OnSubmit功能,并添加到从formik收集的值。
我在Formik创建一个多元素的形式。我访问一个模式内的这种形式。在表单中,我访问表单元素的值username, email, password
就好了。 Code Below
:
handleSubmit = (values,) => {
const { onSuccess } = this.props;
console.log(values);
onSuccess(values);
};
我也有一个阵营 - 选择部件,即我加载一些数据,具有label
和value
对。
我想选择的值添加到从fomrik值列表。下面的反应,让我眼前这个信息。
selectOnChangeCallback = response => {
// eslint-disable-next-line no-console
console.log('selectOnChangeCallback', response);
};
我的阵营选择组件:
<ReactSelect
isMulti={false}
options={selectData}
onChangeCallback={this.selectOnChangeCallback}
/>
我访问它的模态里面,使用此功能:
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只能访问值,但我想补充的反应选择的值的信息,这样我就可以用适当的信息各创建一个用户时显示一个不错的消息。
我试着这么做:
<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}
/>}
/>
没有测试它(应该有这样做的更清洁的方式 - 如果它的工作原理)
一旦我有足够的时间来测试反正我会更新的答案