首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据表单的脏属性更改提交按钮的可用性?

如何根据表单的脏属性更改提交按钮的可用性?
EN

Stack Overflow用户
提问于 2021-01-06 21:42:51
回答 2查看 44关注 0票数 0

我有一个使用react final form的日期过滤器,当没有任何东西被点击时,或者当formState.dirty为" true“时,我想禁用submit按钮,当我单击表单中的某个东西时,该按钮变为true,但我不知道如何链接它们。我的代码如下

代码语言:javascript
复制
let handleButtonDisable = true;      //here I created a variable which I want to use for handling submit button availability

  const handleChange = formState => {      
    if (formState.dirty) {                       
      onChange(formState.values);
      console.log(formState.dirty);
      handleButtonDisable = !formState.dirty;    //here is where I tried to link the dirty property to the handler
    }
  };

  const formCallbacks = liveEdit ? { onSubmit: () => null } : { onSubmit, onCancel, onClear };
  return (
    <FinalForm
      {...rest}
      {...formCallbacks}
      mutators={{ ...arrayMutators }}
      render={formRenderProps => {
        const {
          id,
          form,
          handleSubmit,
          onClear,
          onCancel,
          style,
          paddingClasses,
          intl,
          children,
        } = formRenderProps;

        const handleCancel = () => {
          // reset the final form to initialValues
          form.reset();
          onCancel();
        };

        const clear = intl.formatMessage({ id: 'FilterForm.clear' });
        const cancel = intl.formatMessage({ id: 'FilterForm.cancel' });
        const submit = intl.formatMessage({ id: 'FilterForm.submit' });

        const classes = classNames(css.root);

        const spy =
          liveEdit || onChange ? (
            <FormSpy onChange={handleChange} subscription={{ values: true, dirty: true }} />
          ) : null;

        const buttons = !liveEdit ? (
          <div className={css.buttonsWrapper}>
            <button className={css.clearButton} type="button" onClick={onClear}>
              {clear}
            </button>
            {/* <button className={css.cancelButton} type="button" onClick={handleCancel}>
              {cancel}
            </button> */}
            <button className={css.submitButton} disabled={handleButtonDisable} type="submit">
              {submit}                  //here is the submit button I want to conditionally disable
            </button>
          </div>
        ) : null;

        return (
          <Form
            id={id}
            className={classes}
            onSubmit={handleSubmit}
            tabIndex="0"
            style={{ ...style }}
          >
            <div className={classNames(paddingClasses || css.contentWrapper)}>{children}</div>
            {spy}
            {buttons}
          </Form>
        );
      }}
    />
  );
};

EN

回答 2

Stack Overflow用户

发布于 2021-01-06 22:07:13

试试这个:

代码语言:javascript
复制
<button className={css.submitButton} disabled={formState.dirty?true:false} type="submit">
票数 0
EN

Stack Overflow用户

发布于 2021-01-06 22:42:05

只需将formState.dirty传递给按钮disabled属性即可。

代码语言:javascript
复制
<button 
 className={css.submitButton} 
 disabled={formState.dirty} 
 type="submit"
>
  {submit}                 
</button>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65596899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档