final-form / react-final-form

šŸ High performance subscription-based form state management for React
https://final-form.org/react
MIT License
7.38k stars 480 forks source link

useField sets `type` to `undefined` when not provided via FieldRenderProps #624

Open quicksnap opened 5 years ago

quicksnap commented 5 years ago

Oh hey @erikras! Long time no chat. =P

Finally am getting to use this!

Are you submitting a bug report or a feature request?

Bug!

What is the current behavior?

useField() returns input: { ...stuff, type: undefined }

What is the expected behavior?

Instead, the key should not be deifined

Sandbox Link

https://codesandbox.io/s/react-final-form-simple-example-p35vk

What's your environment?

react-final-form@6.3.0

Other information

The reason this is problematic is that, when spreading into something, it will override type: 'something' with type: undefined since the key's value is actually set to undefined. This is due to a bad optimization in the compiled output:


function useField(name, _temp) {
  var _ref = _temp === void 0 ? {} : _temp,
      afterSubmit = _ref.afterSubmit,
      allowNull = _ref.allowNull,
      beforeSubmit = _ref.beforeSubmit,
      component = _ref.component,
      defaultValue = _ref.defaultValue,
      _ref$format = _ref.format,
      format = _ref$format === void 0 ? defaultFormat : _ref$format,
      formatOnBlur = _ref.formatOnBlur,
      initialValue = _ref.initialValue,
      isEqual = _ref.isEqual,
      multiple = _ref.multiple,
      _ref$parse = _ref.parse,
      parse = _ref$parse === void 0 ? defaultParse : _ref$parse,
      _ref$subscription = _ref.subscription,
      subscription = _ref$subscription === void 0 ? all$1 : _ref$subscription,
      type = _ref.type,
      validate = _ref.validate,
      validateFields = _ref.validateFields,
      _value = _ref.value;

In the uncompiled code, there is a check for undefined before assigning to the key. In compiled code, that check is not there, and it explicitly sets type to _ref.type, which is undefined by default.

I haven't looked at this build on why this is.

chrisamador commented 4 years ago

Ran into this today with the <Field /> component. The type prop is turning into an undefined value. This is an issue for us since we have been using that type prop to pass down the input type to the <input/> inside of the <Field />.

The current workaround is reverting back to v4.1.0. We checked all the versions and that was the last working version with the type prop. Or, of course, changing the name of the prop to something other than type