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.

[Proposal] Change handler consistency

See original GitHub issue

Here’s a quick snapshot of our change handlers:

Checkbox: onChange(evt) { console.log(evt.target.checked); } Input: onChange(evt) { console.log(evt.target.value); } Menu: onItemSelect(selectedItem: object) Modal: onClose(closeSource: string) Pagination: onPageChange?: (nextPage: number, prevPage: number) Popover: onOpen(), onClose() RadioGroup: onChange(evt) { console.log(evt.target.value); } Select: onChange(evt, params) { console.log(params.selectedOptions) } Slider: onChange(evt, value) { console.log(value) } TextArea: onChange(evt) { console.log(evt.target.value); }

As you can see, there’s a pretty significant amount of variation between components. It would probably be good for us to make these more consistent, or at least develop some guidelines around what to use when.

I’d like to make a case for gravitating towards the following API long term:

onChange(value, event, meta)
  • If this is a form control with a value, just pass that directly as the first argument. This prevents people from needing to remember how to dig into the event object like evt.target.checked or evt.target.value, etc. It also works better for components like multiple Select or Slider where the event doesn’t actually have the form control value.
  • Event is second. I’d argue if you have the value first, event is rarely needed. It can also be confusing for something like multiple Select, where things like removing a tag technically trigger a change event. Or for pagination where clicking back/next or selecting a page from the dropdown can both trigger page changes.
  • Meta is third. This is an object that can include things like previous value (if useful), or any other metadata that may be useful to the user.

The only benefit I can think for keeping event as the first argument is that it matches what people expect when using a native html element, but in many cases our components are not native html elements and the event can come from multiple different types of dom interactions. I’d rather be consistent within our own components than consistent with native elements.

Curious to hear what folks think about this – do you agree there’s a need to standardize? Are there downsides with the api proposed in this issue?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:31 (31 by maintainers)

github_iconTop GitHub Comments

2reactions
abmaicommented, Sep 12, 2018

Maybe controversial opinion here. If we’re building for the future anyway - and we’re not entirely trying to mimic native handlers, why not go all the way to object params i.e. onChange({value, event, meta}) so we can forgo order altogether and allow users to just pick out what they need?

I agree that we should come to some sort of standardized API for the non-native change handlers.

1reaction
schnerdcommented, Sep 13, 2018

In my recent designed components value was not popular, or it was not a primitive type (object for instance). So I would leave event as first argument. Sometimes value is found in event itself.

By the same reasoning, sometimes value is not found in the event (select, slider, pagination, datepicker, etc), so event is “not popular” in those cases. By encouraging users to use the explicit value property we’d reduce the cognitive overload they have when implementing different components.

I think Anh’s API does a pretty good job of balancing the concern of whether event or value should come first – they’re both available if necessary, it’s consistent across all components, and the value is readily available.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Event Proposal Guide 2022 | Cvent Blog
There are many types of proposals, from an event proposal for sponsorship to ... Check for consistency, grammar, spelling, and proper design ...
Read more >
[Proposal] Replace mapEventToState with on<Event> in Bloc ...
I am proposing to remove the mapEventToState API in favor of on<Event> . This would allow developers to register event handlers by calling ......
Read more >
5 Secrets to Creating Winning Event Proposals - YouTube
The event industry is a wildly competitive group of creative businesses proposing showstopping concepts. Are you one of them?
Read more >
Proposal for step handling overhaul - Orekit Forum
The first aspect is the scheduling between calls to OrekitStepHandler.handleStep and calls to EventDetector.eventOccurred . When an event ...
Read more >
Proposal Configuration.pdf
Structural integrity checks on configuration changes should be up to the implementation or the respective reacting event handler. Since there are usecases where ......
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