Skip to content

Instantly share code, notes, and snippets.

@MikaelCarpenter
Created December 14, 2018 19:09
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MikaelCarpenter/1fe226967b00eea82e6a4760b244ada1 to your computer and use it in GitHub Desktop.
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
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 }
...
/>
);
}
}
@imri29
Copy link

imri29 commented Feb 10, 2021

thanks for this, nice and elegant solution. helped me a lot.

@adrianbunea
Copy link

Thanks! Made my day!

@SnapeEye
Copy link

SnapeEye commented Feb 3, 2023

Worked for most components! Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment