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.

Get the individual option that was added or removed onChange?

See original GitHub issue

Maybe I’m missing something, but I can’t seem to find it anywhere in the docs. When using multi select, the onChange handler only returns the new array of items. There is no way to tell which option was added or removed.

Is there any handler that can tell me which option was removed and which option was added instead of a complete array of currently selected options?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:15
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
brandonmpcommented, Jan 12, 2017

yeah, this library is incredible, but i found this design decision difficult to understand

I’m just using a simple diff function onChange for now.

Here’s a snippet of my solution. I just picked this library up today, so it’s likely this isn’t the optimal way to do this, but I’m procrastinating atm, so what the hell. Note a few caveats for my use case:

  1. since I am using Creatable, I’ve assigned the handler to onNewOptionClick as well
  2. rather than filter through my options as an array of objects, I’ve just used the simpleValue option so that my onChange handler receives a comma-delimited string instead of an array of objects. Unnecessary for an example this trivial, but on larger arrays, I imagine this might make a difference.

This works for my use case, but given how customizable this component is, I doubt it’s copy-pasta for everyone.

// these two props are passed in from above
const allTags = [
  { value: 'one', label: 'Fake Tag' },
  { value: 'two', label: 'Fake Tag2' },
  { value: 'three', label: 'Fake Tag3' },
  { value: 'four', label: 'Fake Tag4' },
]
const currentTags = ['one', 'two']


const AddTagDealy = (props) => {

  const _onChange = (newTags) => {
    const { currentTags } = props
    // split on whatever your delimiter is
    const newTagsArray = newTags.split(',')

    // is this a tag addition or deletion? find which array is longer to find out
     const isDeletion = newTagsArray.length < currentTags.length

    // note brackets are just es6 array destructuring
    const [ alteredValue ] = isDeletion ?
        // if deletion
      currentTags.filter(tag => !newTagsArray.includes(tag))
      // if addition
      : newTagsArray.filter(tag => !currentTags.includes(tag))

    console.log('CHANGED VALUE', alteredValue, "Was it a deletion?", isDeletion)
  }
 
  const { allTags, currentTags } = props

  return (
    <Select.Creatable
      name='form-field-name'
      value={currentTags}
      multi
      backspaceRemoves={false}
      deleteRemoves={false}
      clearable={false}
      simpleValue
      onNewOptionClick={_onChange}
      options={allTags}
      onChange={_onChange}
      />
  )
}

So if user clicks to delete ‘Fake Tag2’, the console logs two, and true

If they add the custom value ‘Fake Tag99’, the console logs 'Fake Tag99' and false

It’s a bit inconsistent, because clicking an item gets you the value, while custom creations get you what amounts to the label, but afaik it’s the only way.

1reaction
ansmonjolcommented, Feb 23, 2017

Did the same as @brandonmp. I’ve a list of item already selected for my item. In the onChange function of the Select i do this:

  • Check if it’s a deletion with a if(this.state.myItemSelectedValues.length > onChangeNewVal)
  • Parse my this.state.myItemSelectedValues array
  • If my onChangeNewVal array does not include my this.state.myItemSelectedValues[i] (use .includes() function), I unshift or push it in my available options array
Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - How to catch remove event in react-select when ...
So, to detect if some option was removed, you would have to compare the current state with the new values the onChange emitted....
Read more >
How to add & remove owners & managers for your Business ...
Owners of Business Profiles can invite users to become owners and managers. Each person can have their own access and they don't share...
Read more >
Change, remove or turn off animation effects - Microsoft Support
Although you can't remove all animations from an entire presentation in one step (you have to remove animations from each object individually, as...
Read more >
Renew, change, update, or cancel your health plan for 2023
If you have Marketplace health insurance in 2022, you can renew, change, or update your plan for 2023 until January 15, 2023. Enroll...
Read more >
Change Your Flood Zone Designation | FEMA.gov
How to Submit a Letter of Map Change (LOMC) Request · Homeowners, Renters, Community Officials and Other Individuals · Land Surveyors, ...
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