Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onBlur event #431

Merged

Conversation

jbalboni
Copy link
Contributor

@jbalboni jbalboni commented Dec 22, 2016

Reasons for making this change

We want to be able to show validation errors only after a user has touched a field or tried to submit. We can do the submit part easily through the onError hook and formContext, but we need a way to track blur events. This adds that, similar to the onChange hook.

Should close #364

Checklist

  • I'm updating documentation
    • I've checked the rendering of the Markdown text I've added
    • If I'm adding a new section, I've updated the Table of Content
  • I'm adding or updating code
    • I've added and/or updated tests
    • I've updated docs if needed
  • I'm adding a new feature
    • I've updated the playground with an example use of the feature

@n1k0
Copy link
Collaborator

n1k0 commented Jan 23, 2017

I like the approach of this patch better than the proposed implementation in #444. I think it's worth having the field unique id passed as the handler first argument as it's the most obvious way to know which field has triggered the event.

Could you please rebase your patch against latest master so we can land this?

@@ -439,6 +447,7 @@ if (process.env.NODE_ENV !== "production") {
idSchema: PropTypes.object,
errorSchema: PropTypes.object,
onChange: PropTypes.func.isRequired,
onBlur: PropTypes.func.isRequired,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably leave this optional and test for its presence instead.

Copy link
Contributor Author

@jbalboni jbalboni Jan 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I can just remove the isRequired part here. There's no actual use of onBlur in this component, it's just passed through.

@jbalboni
Copy link
Contributor Author

@n1k0 I'm assuming that there's always an onBlur function passed down to the widget components, and I have the conditional check for a user specified onBlur function in the Form component. I could add a conditional in front of the use of onBlur in the widget components instead (I think the other PR did this). Do you have a preference?

@n1k0
Copy link
Collaborator

n1k0 commented Jan 23, 2017

Do you have a preference?

Actually I don't have a strong opinion on this. While the onChange fn seems mandatory, the onBlur one is more optional to me, so I'd probably mark it that way.

Copy link
Collaborator

@n1k0 n1k0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few last nits and we're good to go. Thanks for the great work!

@@ -24,7 +25,8 @@ function BaseInput(props) {
readOnly={readonly}
autoFocus={autofocus}
value={typeof value === "undefined" ? "" : value}
onChange={_onChange} />
onChange={_onChange}
onBlur={onBlur ? (event) => onBlur(inputProps.id, event.target.value) : undefined}/>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize we could just do onBlur && (event) => onBlur(inputProps.id, event.target.value) here, but that may be harder to read. Opinions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's better, actually.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go for it then!

}

return newValue;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's take the opportunity to improve readability here:

function getValue(event, multiple) {
  if (multiple) {
    return [].slice.call(event.target.options)
      .filter(o => o.selected)
      .map(o => o.value);
  } else {
    return event.target.value;
  }
}

@n1k0
Copy link
Collaborator

n1k0 commented Jan 24, 2017

Great work, thank you.

@n1k0 n1k0 merged commit befe596 into rjsf-team:master Jan 24, 2017
n1k0 added a commit that referenced this pull request Jan 28, 2017
Breaking changes

When a text input is emptied by the user, it's value is now reset to `undefined` instead of being set to `""` (empty string) as previously. This better matches traditional HTML forms behavior.

New features

* Add an array field template component (#437)
* Wrap radio and checkbox labels into span to enable styling. (#428)
* Reset text inputs value when emptied (#442)
* Add transform hook for changing errors from json schema validation (#432)
* Add a noHtml5Validate prop (#448)
* Add support for a onBlur event handler (#431)
* Allow empty option for enum fields (#451)

Bugfixes

* Fix #452: Support recursively referenced definitions. (#453)
* Fix #454: Document what master actually is, suffix its version with -dev.
@n1k0
Copy link
Collaborator

n1k0 commented Jan 28, 2017

Released in v0.42.0.

@jedmonsanto jedmonsanto mentioned this pull request Mar 8, 2020
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

onBlur event
2 participants