antd-form-builder icon indicating copy to clipboard operation
antd-form-builder copied to clipboard

Cannot pass formatter to Input

Open sklinov opened this issue 5 years ago • 4 comments

I would like to use formatter and parser function to <Input> or <InputNumber /> components, but can't figure out how to pass those functions. I've tried adding them to widgetProps, or getValueProps but that didn't seem to work. Thanks!

sklinov avatar Jul 01 '20 12:07 sklinov

For InputNumber you should be able to pass formatter or parser to the component by widgetProps. I don't see them as the API of Input from antd's docs.

supnate avatar Jul 02 '20 04:07 supnate

Thanks for prompt answer. Yeah, you're right, there's no formatter or parser for regular Input. I should probably try to use getValueFromEvent and pass it using fieldProps

sklinov avatar Jul 02 '20 04:07 sklinov

You can wrap Input to a new custom component with formatter supported. Like:

const MyInput = props => {
  const { value, onChange, formatter, parser } = props;
  const newValue = formatter(value);
  const newOnChange = value => onChange(parser(value));
  const newProps = _.omit(props, ['value', 'onChange', 'formatter', 'parser']);
  return <Input {...newProps} value={newValue} onChange={newOnChange} />
}

supnate avatar Jul 02 '20 04:07 supnate

Great, thanks for the support! I'll give it a try

sklinov avatar Jul 02 '20 04:07 sklinov