Skip to content

Commit

Permalink
feat(select): adds data-test-id to Select component (#2790)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertmihaiman authored Jul 6, 2022
1 parent 67bd696 commit 2ba14ed
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type SelectProps = Omit<
searchable?: boolean;
shiftIconLeftwards?: boolean;
value?: string | undefined;
dataTestId?: string;
};

export const Select: React.FC<SelectProps> = ({
Expand All @@ -48,6 +49,7 @@ export const Select: React.FC<SelectProps> = ({
searchable,
shiftIconLeftwards = false,
value,
dataTestId,
...rest
}) => {
const { state, dispatch, Mode, selectRef } = useSelect({ value, defaultValue });
Expand Down Expand Up @@ -111,6 +113,7 @@ export const Select: React.FC<SelectProps> = ({
className='Select__value'
disabled={disabled}
onClick={handleToggle}
data-test-id={dataTestId}
aria-haspopup='listbox'
aria-expanded={state.mode === Mode.Open}
aria-labelledby={`Select__label--${idRef} Select__value--${idRef}`}
Expand Down Expand Up @@ -139,6 +142,7 @@ export const Select: React.FC<SelectProps> = ({
'Select__options--up': dropDirection === 'up',
'Select__options--down': dropDirection === 'down'
})}
dataTestId={dataTestId}
searchable={searchable}
options={options}
onSelect={handleSelect}
Expand Down
5 changes: 4 additions & 1 deletion src/components/Select/SelectOption.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ export type SelectOptionProps = Omit<React.HTMLAttributes<HTMLLIElement>, 'onCli
selected: boolean;
option: SelectableOption;
onClick: (option: SelectableOption) => void;
dataTestId?: string;
};

export const SelectOption: React.FC<SelectOptionProps> = ({
active,
selected,
option,
onClick,
className
className,
dataTestId
}) => {
const ref = useRef<HTMLLIElement>(null);

Expand All @@ -43,6 +45,7 @@ export const SelectOption: React.FC<SelectOptionProps> = ({
'SelectOption--disabled': option.disabled
})}
onClick={handleClick}
data-test-id={dataTestId ? `${dataTestId}--${option.label.replace(' ', '-')}` : undefined}
aria-label={option.label}
aria-selected={selected}
role='option'
Expand Down
3 changes: 3 additions & 0 deletions src/components/Select/SelectOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type SelectOptionsProps = Omit<
selectedOption?: SelectableOption | undefined;
onFocus?(option: SelectableOption): void;
onSelect(option: SelectableOption): void;
dataTestId?: string;
};

export const SelectOptions: React.FC<SelectOptionsProps> = ({
Expand All @@ -30,6 +31,7 @@ export const SelectOptions: React.FC<SelectOptionsProps> = ({
onSelect,
onFocus,
className,
dataTestId,
...rest
}) => {
const [searchPhrase, setSearchPhrase] = useState('');
Expand Down Expand Up @@ -82,6 +84,7 @@ export const SelectOptions: React.FC<SelectOptionsProps> = ({
id={`SelectOption--${option.value}-${idRef}`}
key={option.value}
option={option}
dataTestId={dataTestId}
active={activeOption?.value === option.value}
selected={selectedOption?.value === option.value}
onClick={onSelect}
Expand Down

0 comments on commit 2ba14ed

Please sign in to comment.