Created
August 7, 2022 12:06
-
-
Save themegabyte/25fa5fe8d6e62d76ea0ccbafe75e4e90 to your computer and use it in GitHub Desktop.
handleChange for MUI DropDown with multiple enabled. Limits the number of entries to a maximum of 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const handleChange = (event, selectedChildNode) => { | |
| const { | |
| target: { value }, | |
| } = event; | |
| console.log(event.target.value); | |
| // allow a maximum of 3 values only | |
| if (annualCompensationProp.length > 2) { | |
| // make a copy so not to mutate original state | |
| let newArray = annualCompensationProp.slice(); | |
| // removes the incoming newItem if it already is in the array | |
| // This is because the user can also deselect and already selected item | |
| let arr = removeItemOnce(newArray, selectedChildNode.props.value); | |
| setannualCompensationProp(arr); | |
| } else | |
| setannualCompensationProp( | |
| // On autofill we get a stringified value. | |
| typeof value === "string" ? value.split(",") : value | |
| ); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment