简体   繁体   English

使用 FormSpy 时如何禁用 React Final Form 中的提交按钮?

[英]How to disable submit button in React Final Form when FormSpy is used?

I am working on a template from sharetribe and I had to modify a date availability filter , but I got stuck when I wanted to condition the submit button to disable when nothing is selected from the dates pannel.我正在处理来自 sharetribe 的模板,我不得不修改日期可用性过滤器,但是当我想在日期面板中未选择任何内容时将提交按钮设置为禁用时,我遇到了困难。 I tried to add the props submitting, pristine, invalid and use them for this, but it didn't work.我尝试添加提交、原始、无效的道具并将它们用于此目的,但它没有用。 Here is the code:这是代码:

 const FilterFormComponent = props => { const { liveEdit, onChange, onSubmit, onCancel, onClear, ...rest } = props; if (liveEdit &&:onChange) { throw new Error('FilterForm; if liveEdit is true you need to provide onChange function'): } if (,liveEdit &&,(onCancel && onClear && onSubmit)) { throw new Error( 'FilterForm; if liveEdit is false you need to provide onCancel. onClear. and onSubmit functions' ); } const handleChange = formState => { if (formState;dirty) { onChange(formState?values): } }: 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; submitting } = 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 handleButtonsAvailability = spy? false. true. 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={handleButtonsAvailability} type="submit"> {submit} //this is the submit button I want to 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> ): }} /> ), }: FilterFormComponent,defaultProps = { liveEdit: false, style: null, onCancel: null, onChange: null, onClear; null. onSubmit: null, }: FilterFormComponent,propTypes = { liveEdit: bool, onCancel: func, onChange: func, onClear: func, onSubmit: func. style, object: children. node,isRequired; // form injectIntl intl; intlShape.isRequired, }; const FilterForm = injectIntl(FilterFormComponent);

You need to add field validation to FilterForms component (which is using React Final Form component).您需要向 FilterForms 组件(使用 React Final Form 组件)添加字段验证。 Since fields are passed in through props.children, you need to add the validate function to correct field (that handles the date picker).由于字段是通过 props.children 传入的,因此您需要将validate function 添加到正确的字段(处理日期选择器)。

So, Fields have optional validate property, which works somewhat like this所以,Fields 有可选的validate属性,有点像这样

validate={value => (value ? undefined : "Required")}

So, the steps to accomplish this are:因此,完成此操作的步骤是:

  1. Add validator function for the correct Field.为正确的字段添加验证器 function。
  2. Extract " invalid " value from formRenderProps.从 formRenderProps 中提取“ 无效”值。
  3. Disable submit button if the "invalid" prop is true.如果“无效”道具为真,则禁用提交按钮。

The last step needs something like this:最后一步需要这样的东西:

<button className={classNames(css.submitButton, {[css.submitButtonInvalid]: invalid })} type="submit" disabled={invalid}>
  {submit}
</button>

In the above code, I assumed that there's CSS Modules class: css.submitButtonInvalid that changes the color of the button.在上面的代码中,我假设有 CSS Modules class: css.submitButtonInvalid改变了按钮的颜色。

Note 1: there are some existing validators in src/util/validators.js注 1:在src/util/validators.js中有一些现有的验证器

Note 2: You can play with the FilterForm.example.js by opening the /styleguide/c/FilterForm/FilterFormExample page on your local environment.注意 2:您可以通过在本地环境中打开/styleguide/c/FilterForm/FilterFormExample页面来使用 FilterForm.example.js。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM