【发布时间】:2021-08-21 14:29:50
【问题描述】:
我将 Formik 用于我的 React 表单,我也很新,我有一个带有两个复选框和一个默认禁用的提交按钮的表单。我需要找到一种方法来检查复选框是否已被选中。如果已选中所有两个复选框,则启用提交按钮,如果未选中任何复选框,则提交按钮保持禁用状态。
我的代码:
FormikWithRef:
import React, { useEffect, forwardRef, useImperativeHandle } from 'react';
import { Formik } from 'formik';
function FormikWithRef(props, ref) {
let formikPropObj = {};
useImperativeHandle(ref, () => formikPropObj);
useEffect(() => {
if (props.refSetter && props.invokable)
props.refSetter({
ref: ref.current,
invokableFunction: props.invokable,
});
}, []);
return (
<Formik {...props}>
{(formikProps) => {
formikPropObj = formikProps;
if (typeof props.children === 'function') {
return props.children(formikProps);
}
return props.children;
}}
</Formik>
);
}
export default forwardRef(FormikWithRef);
注册表格:
import React from 'react';
import PropTypes from 'prop-types';
import FormikWithRef from './FormikWithRef';
const RegisterForm = ({
formRef,
internalRef,
children,
initialValues,
validationSchema,
onSubmit,
enableReinitialize,
validateOnMount,
initialTouched,
invokable,
refSetter,
}) => {
return (
<FormikWithRef
enableReinitialize={enableReinitialize}
validateOnMount={validateOnMount}
initialTouched={initialTouched}
validateOnChange
validateOnBlur
initialValues={initialValues}
validationSchema={validationSchema}
onSubmit={onSubmit}
ref={formRef}
internalRef={internalRef}
invokable={invokable}
refSetter={refSetter}
>
{(props) => (
<form ref={internalRef} onSubmit={props.handleSubmit}>
{children}
</form>
)}
</FormikWithRef>
);
};
RegisterForm.propTypes = {
formRef: PropTypes.object,
children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.node), PropTypes.node]),
initialValues: PropTypes.object,
validationSchema: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
onSubmit: PropTypes.func,
enableReinitialize: PropTypes.bool,
validateOnMount: PropTypes.bool,
initialTouched: PropTypes.object,
internalRef: PropTypes.func,
handleSubmit: PropTypes.func,
invokable: PropTypes.func,
refSetter: PropTypes.func,
};
RegisterForm.defaultProps = {
formRef: null,
children: null,
initialValues: {},
validationSchema: null,
onSubmit: () => {},
enableReinitialize: false,
validateOnMount: false,
initialTouched: {},
internalRef: null,
handleSubmit: null,
invokable: null,
refSetter: null,
};
export default RegisterForm;
注册组件:
import React, { useState, useRef } from 'react';
import Registerform from './RegisterForm';
import useUserApi from './useUserApi';
const RegisterForm = () => {
const [submitting, setSubmitting] = useState(false);
const [isSubmitted, setIsSubmitted] = useState(false);
const [disableSubmit, setDisableSubmit] = useState(true);
const formRef = useRef();
const initialValues = {
cookies: false,
terms: false,
};
const {register} = useUserApi();
const submitRegisterForm = async (values) => {
const body = {
terms: values?.terms || null,
cookies: values?.cookies || null,
};
const res = await register(body);
return res;
};
const onFormSubmit = async (values, { setErrors }) => {
setSubmitting(true);
const result = await submitRegisterForm(values);
if (result && !(await checkErrors(result))) {
setSubmitting(false);
setIsSubmitted(true);
return true;
}
if (result.status === 500)
setErrors('A problem occurred submitting form.');
setIsSubmitted(false);
setSubmitting(false);
return false;
};
useEffect(() => {
if (isSubmitted) {
const historyScreen = reverse(`${routes.users}`);
history.push(historyScreen);
}
}, [isSubmitted]);
return initialValues ? (
<FormWrapper>
<RegisterForm
initialValues={initialValues}
onSubmit={onFormSubmit}
formRef={formRef}
>
<InputWrapper width="50%" verticalAlign="top">
<CheckBox
label={
<LabelText>I have agreed to terms</LabelText>
}
name="terms"
id="terms"
/>
</InputWrapper>
<InputWrapper width="50%" verticalAlign="top">
<CheckBox
label={
<LabelText>I agree to cookies.</LabelText>
}
name="cookies"
id="cookies"
/>
</InputWrapper>
<ButtonWrapper>
<SaveButton
type="submit"
buttonText="Register"
disabled={disableSubmit}
/>
</ButtonWrapper>
</Form>
</FormWrapper>
) : null;
}
export default RegisterForm;
【问题讨论】:
-
您也可以为 RegisterForm 添加您的代码吗?如果您使用 Formik 组件,这可以轻松完成,但看起来您在 Formik 顶部有一个包装器。
-
是的,我可以做到
-
我刚刚添加了@Shyam
标签: javascript reactjs forms formik