Created
December 14, 2018 19:09
-
-
Save MikaelCarpenter/1fe226967b00eea82e6a4760b244ada1 to your computer and use it in GitHub Desktop.
Solution for adding data attributes to a react-select component by passing in "custom components" where you've manipulated the innerProps prop
This file contains 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
import React, { Component } from 'react'; | |
import ReactSelect, { components } from 'react-select'; | |
const TextOption = props => ( | |
components.Option && ( | |
<components.Option { ...props }> | |
... | |
</components.Option> | |
) | |
); | |
const addDataAcceptance = ( Component, dataAcceptance ) => ( | |
props => ( | |
<Component | |
{ ...props } | |
innerProps={ Object.assign({}, props.innerProps, { 'data-acceptance': dataAcceptance }) } | |
/> | |
) | |
); | |
const CUSTOM_COMPONENTS = { | |
Option: addDataAcceptance( TextOption, 'TextOption' ), | |
Menu: addDataAcceptance( components.Menu, 'Menu' ), | |
... | |
}; | |
class Select extends Component { | |
render() { | |
return ( | |
<ReactSelect | |
components={ CUSTOM_COMPONENTS } | |
... | |
/> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing! Super useful