question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[blur event] Why does handleBlur updates field value?

See original GitHub issue

What is the current behavior?

onBlur and onFocus callbacks are required to mark field as touched to display per-field validation. onBlur and onFocus assume DOM event is passed and current field value corresponds to value of the underlying DOM element.

That behavior is not working for custom components. If custom component is used in place of input the value property of DOM element that originates event can not be equal to that of a component itself.

For example, we use CheckBoxGroup which has value property defined as arrayOf(string). This component still emits onFocus and onBlur events when individual checkboxes are checked/unchecked and passes DOM event through. This DOM event is used by blur, value is extracted from it and this value is no longer an array but string true, since this value is read from checked property of a checkbox.

What is the expected behavior?

onBlur event handler shouldn’t update the value of the field and shouldn’t assume that event object passed in is actually a DOM event. onBlur handler should update touched property of the field so that validation is still displayed.

Is there any reason why onBlur event updates form state? Can we use onChange events exclusively to update form state?

What’s your environment?

All environments

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:44
  • Comments:29 (3 by maintainers)

github_iconTop GitHub Comments

19reactions
pvjonescommented, Dec 12, 2017

Using event.preventDefault() was not working for me, since input.touched was then not set to true (this is needed in my case for displaying input error hints). I think this is related to what @An6r mentioned above. It’s not pretty, but I ended up passing down a callback that invokes input.onBlur without the event (this also satisfies my TypeScript compilation). If anyone has a better solution I’d love to hear it.

const renderChipInput: SFC<MergedProps> = ({
  input,
  meta: { error, touched },
  ...rest,
}) => {
  const value = input.value || List()

  const onRequestAdd = (chip: any): void => {
    if (invalidChip(chip)) { return }
    input.onChange(input.value.push(chip))
  }
  const onRequestDelete = (chip: any): void => {
    if (value.indexOf(chip) !== -1) {
      input.onChange(input.value.delete(index))
    }
  }

  return (
      <ChipInput
        value={value}
        onRequestAdd={onRequestAdd}
        onRequestDelete={onRequestDelete}
        helperText={touched && error}
        onBlur={e => input.onBlur(undefined)}
        {...rest}
      />
  )
}

const FormChipInput: SFC<BaseFieldProps<MergedComponentProps> & FormChipInputProps> =
  ({ name, componentProps, ...rest }) => {
    return (
      <Field
        name={String(name)}
        component={renderChipInput}
        props={componentProps}
        {...rest}
      />
    )
  }
16reactions
mlarchercommented, Jun 26, 2017

Fwiw, it seems to me that a onBlur={e => { e.preventDefault(); } on the Field element brings us the expected behaviour with no downside. Gotta confirm there is no edge case though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Element: blur event - Web APIs | MDN
The blur event fires when an element has lost focus. The event does not bubble, but the related focusout event that follows does...
Read more >
Updating a React Input text field with the value on onBlur event
On blur, the function calls a service to update the input value to the server, and once that's complete, it updates the input...
Read more >
onblur Event - W3Schools
The onblur event occurs when an element loses focus. The onblur event is often used on input fields. The onblur event is often...
Read more >
The difference between onBlur vs onChange for React text ...
Every time you get out of focus from the input field, the event will trigger. Here's how it looks like in React function...
Read more >
.blur() | jQuery API Documentation
The blur event is sent to an element when it loses focus. Originally, this event was only applicable to ... <input id="target" type="text"...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found