field-form icon indicating copy to clipboard operation
field-form copied to clipboard

make form.submit return a promise

Open hvsy opened this issue 5 years ago • 3 comments

参考以下示例,当form 的 submit 方法返回一个 promise 对象的时候. 用户可以很方便的实现以下需求

  1. 防止重复提交

  2. 显示表单正在提交中


const AsyncButton = (props)=>{
    const {onClick,children} = props;
    const [ing,setIng] = useState(false);
    return <a onClick={async()=>{
            if(ing) return;
            setIng(true);
            try{
                await onClick();
            }finally{
                setIng(false);
            }
    }}>
        {children}
    </a>
};
const TestComponent = ()=>{
    const form = useForm();
    return <div>
        <Form onFinish={(values)=>{
            return axios.post("...",values);
        }}>
            ...
        </Form>
        <AsyncButton
        onClick={()=>{
            return form.submit();
        }}
        >
                submit
        </AsyncButton>
    </div>
};

hvsy avatar Aug 28 '20 07:08 hvsy

Codecov Report

Merging #187 into master will increase coverage by 0.00%. The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #187   +/-   ##
=======================================
  Coverage   99.89%   99.89%           
=======================================
  Files          13       13           
  Lines         912      915    +3     
  Branches      208      207    -1     
=======================================
+ Hits          911      914    +3     
  Misses          1        1           
Impacted Files Coverage Δ
src/Form.tsx 100.00% <100.00%> (ø)
src/useForm.ts 99.71% <100.00%> (+<0.01%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update e72849a...79b60df. Read the comment docs.

codecov[bot] avatar Aug 28 '20 07:08 codecov[bot]

https://github.com/react-component/field-form/pull/108#issuecomment-608241087

shaodahong avatar Sep 01 '20 03:09 shaodahong

急需一个submitting状态。。

HHHui avatar Sep 01 '20 07:09 HHHui