当前位置:网站首页>Add get disabled for RC form

Add get disabled for RC form

2022-07-07 22:18:00 Belden wind

background

In the use of ant Form time , When we need to pass the verification , Button is disabled , however Form Did not provide us with onDisable Methods , What shall I do? ?
 Insert picture description here
 Insert picture description here

terms of settlement

adopt onValuesChange When an validate


 const [disabled, setDisabled] = useState(false);// Default inspection failed 
 const fieldDisabledRef = useRef({
    });
 const required = ['languages'];//  All fields to be verified 
 const valueChange = changeValue => {
    
        if (!formRef.current) {
    
            return;
        }
        const values = formRef.current.getFieldsValue();
        localStorage.setItem(`${
      ID}_skill_info`, JSON.stringify(values));

        const changeValueKeys = Object.keys(changeValue);
        formRef.current.validateFields(changeValueKeys, (v, error) => {
    
            changeValueKeys.forEach(key => {
    
                if (error && error[key]) {
    
                    fieldDisabledRef.current[key] = true;
                } else {
    
                    fieldDisabledRef.current[key] = false;
                }
            });
            const keys = Object.keys(values);
            let disabled = false;
            keys.forEach(key => {
    
                const disabledData = fieldDisabledRef.current[key];
                if (disabledData === true || disabledData === undefined) {
    
                    disabled = true;
                }
            });
            setDisabled(disabled);
        });
    };
    useEffect(() => {
    
        if (formRef.current) {
    
            const data = Object.entries(initObj).reduce((result, [key, value]) => {
    
                if (Array.isArray(value)) {
    
                    if (value.length || !required.includes(key)) {
    
                        result[key] = value;
                    }
                    return result;
                }
                if (value || !required.includes(key)) {
    
                    result[key] = value;
                }
            }, {
    });
            formRef.current.setFieldsValue(data);
        }
    }, [initObj]);
      <Form
            ref={
    formRef}
            layout="vertical"
            onValuesChange={
    valueChange}
            initialValues={
    ...}
        >
        ...
        </Form>

That's it , Sometimes it's so simple to change your mind ...

原网站

版权声明
本文为[Belden wind]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130607022374.html