简体   繁体   English

Ant 设计表单 - onAfterChange 处理程序

[英]Ant Design Form - onAfterChange handler

I've created a form with antd that outputs the form values to the console whenever a field has changed by using the onValuesChange function on a Form .我已经使用 antd 创建了一个表单,只要使用Form上的onValuesChange函数更改字段,就会将表单值输出到控制台。

My issue is that Slider components call this onValuesChange function whilst dragging the slider and I'd like it to instead be at onmouseup .我的问题是Slider组件在拖动Slider调用这个onValuesChange函数,我希望它改为onmouseup

I'm aware that the onAfterChange event of a slider only fires onmouseup but I'm not sure how to make onValuesChange use onAfterChange instead of onChange .我知道滑块的onAfterChange事件只会触发onmouseup但我不确定如何让onValuesChange使用onAfterChange而不是onChange Can anyone offer advice on this?任何人都可以提供这方面的建议吗?

 import React, { Component } from 'react'; import { Form, Slider } from 'antd'; const FormItem = Form.Item; class UnwrappedMyForm extends Component { render() { const { getFieldDecorator } = this.props.form; return ( <Form> <FormItem> {getFieldDecorator(`field1`, {})( <Slider range defaultValue={[0, 100]} /> )} </FormItem> <FormItem> {getFieldDecorator(`field2`, {})( <Slider range defaultValue={[0, 100]} /> )} </FormItem> </Form> ); } } const MyForm = Form.create({ onValuesChange(props, changedValues, allValues) { console.log(allValues); } })(UnwrappedMyForm); export default MyForm;

Working example: https://codesandbox.io/s/z2ppnop8x工作示例: https : //codesandbox.io/s/z2ppnop8x

you should never do it! 你不应该这样做! if I will know the purpose I can help you better, 如果我能知道我可以帮助您更好的目的,

but this will work for you: 但这将为您工作:

render() {
    const { form, errorMessage } = this.props;
    const { getFieldDecorator, setFieldsValue } = form;

    return (
      <Form>
        <FormItem>
          {getFieldDecorator(`slider`, {})(
            <div>
              <Slider onAfterChange={wantedValues => setFieldsValue({ slider: wantedValues })} range />
            </div>,
          )}
        </FormItem>
      </Form>
    );
  }
}
export default Form.create({
  onValuesChange(props, changedValues, allValues) {
    console.log('on change', allValues);
  },

The value of the slider and the value of the slider field is separate and you are updating the field value manualy 滑块的值和“滑块”字段的值是分开的,您需要手动更新字段值

I'm facing the same issue and maybe this is a simpler approach:我面临同样的问题,也许这是一种更简单的方法:

The keypoint is to maintain an internal state for this slider component and send the wanted value through onChange during onAfterChange .关键点是为这个滑块组件维护一个内部状态,并在onAfterChange期间通过onChange发送想要的值。

 const MouseUpSlider = ({ value, onChange, ...rest }) => { const [val, setVal] = useState(value) return ( <Slider value={val} onAfterChange={(value) => onChange(value)} onChange={(value) => setVal(value)} {...rest} /> ) }

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

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