Skip to content

Instantly share code, notes, and snippets.

@MikaelCarpenter
Created December 14, 2018 19:09
Show Gist options
  • 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 }
...
/>
);
}
}
@SnapeEye
Copy link

SnapeEye commented Feb 3, 2023

Worked for most components! Thanks a lot!

@ahsanu123
Copy link

thanks, looking this for all day.

@aleclara95
Copy link

Thanks for sharing! Super useful

@Guilbertoliveira
Copy link

tks bro

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