Repository navigation
Unable to Add data-testid Attribute to ReactSelect Component #6017
Replies: 1 comment
|
React Select is a composite component, so an arbitrary For React Select 5 with TypeScript: import { components, type ControlProps, type GroupBase } from 'react-select';
function TestableControl<
Option,
IsMulti extends boolean,
Group extends GroupBase<Option>,
>(props: ControlProps<Option, IsMulti, Group>) {
const innerProps = {
...props.innerProps,
'data-testid': 'dashboard-supplier-widgets-date-filter-select',
};
return <components.Control {...props} innerProps={innerProps} />;
}Define this component outside your render function, keep your existing Select props, and add: components={{ Control: TestableControl }}The attribute will be on the control container. Spreading the existing Since you're using If you use Testing Library, an accessible label with |
Uh oh!
There was an error while loading. Please reload this page.
Unable to Add data-testid Attribute to ReactSelect Component
Hi team,
I'm trying to add the data-testid attribute to the Select component from react-select for testing purposes, but it doesn't seem to be rendered in the DOM.
Here is the code I'm using:
<Select data-testid='dashboard-supplier-widgets-date-filter-select' isMulti={false} required id='dateFilterDropdown' options={WidgetsDateFilterOptions} value={{ value: props.dateFilter, label: WidgetsDateFilterOptions.find((option) => option.value === props.dateFilter)?.label, }} onChange={(selectedOption) => props.onDateFilterChange(selectedOption.value)} placeholder={t('widget_filter_text')} formatOptionLabel={formatWidgetsOptionLabel} isSearchable={false} menuPortalTarget={document.body} menuPlacement='auto' />Even though the data-testid is passed as a prop, I don't see it applied in the rendered HTML. Is there a recommended way to add custom attributes like data-testid to the Select component?
Any guidance would be greatly appreciated.
Thanks!
All reactions