react-redux-form icon indicating copy to clipboard operation
react-redux-form copied to clipboard

Checkbox in LocalForm updating twice (once with invalid value)

Open valoricDe opened this issue 7 years ago • 5 comments

The Problem

The checkbox component in LocalForm triggers validator once with "on" and once with true.

Steps to Reproduce

Open https://codepen.io/anon/pen/XZpMwq , open console and trigger checkbox change.

Expected Behavior

Only true or false are given to validators.

valoricDe avatar Feb 08 '18 17:02 valoricDe

I changed my get-value.js file to:

function getEventValue(event) {
  ...

  if (target.type === 'checkbox') {
    return target.checked;
  }

  ...
}
...
function getCheckboxValue(value, props) {
  var controlProps = props.controlProps;

  if ((0, _isMulti2.default)(props.model)) {
    return controlProps.value;
  }

  debugger;
  return getValue(value)
}

So at least I get correct truthy values. But it is still triggering somewhere another change where it sets "on" as value. (props.controProps.modelValue by the way is always undefined)

valoricDe avatar Feb 09 '18 09:02 valoricDe

Could it be tha in control-component-factory.js -> createEventHandler on handleUpdate

if (this.props.isToggle) {
          return compose(
            dispatchBatchActions,
            persistEventWithCallback(controlEventHandler || identity)
          )(event);
        }

is doing something unexpected?

valoricDe avatar Feb 09 '18 10:02 valoricDe

It might actually be a combination of things in control-props-map.js and control-component-factory with changeAction. 🔍

davidkpiano avatar Feb 09 '18 14:02 davidkpiano

When I log [model].concat(args) in trackable (in track.js) I get following on one click of the checkbox

grafik

valoricDe avatar Feb 12 '18 09:02 valoricDe

It's not just checkboxes updating necessary often. When I create a textfield and give it a parser and formatter my parser gets two times a string value and the last time the parsed value :( In control-component-factory -> componentWillReceiveProps it does:

      value: function componentWillReceiveProps(_ref) {
        var modelValue = _ref.modelValue;

        if (modelValue !== this.props.modelValue) {
          this.setViewValue(modelValue);
        }
      }

so viewValue actually receives the parsed value right? But in setViewValue it agains parses the modelValue.

      value: function setViewValue(viewValue) {
        if (!this.props.isToggle) {
          if (this.props.formatter) {
            var parsedValue = this.parse(viewValue);
            this.setState({ viewValue: this.format(parsedValue) });
          } else {
            this.setState({ viewValue: this.parse(viewValue) });
          }
        }
      }

valoricDe avatar Feb 16 '18 15:02 valoricDe