Skip to content

Commit

Permalink
feat(SearchInput): Add ability to add id to input in SearchInput comp…
Browse files Browse the repository at this point in the history
…onent (#9974)

* Add ability to add id to SearchInput

* Fix typo

* Update prop name and add tests
  • Loading branch information
jessiehuff authored Feb 28, 2024
1 parent 47e52de commit 2d7171e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export interface SearchInputProps extends Omit<React.HTMLProps<HTMLDivElement>,
hasWordsAttrLabel?: React.ReactNode;
/** A suggestion for autocompleting. */
hint?: string;
/** Id for the search input */
searchInputId?: string;
/** @hide A reference object to attach to the input box. */
innerRef?: React.RefObject<any>;
/** A flag for controlling the open state of a custom advanced search implementation. */
Expand Down Expand Up @@ -126,6 +128,7 @@ export interface SearchInputProps extends Omit<React.HTMLProps<HTMLDivElement>,

const SearchInputBase: React.FunctionComponent<SearchInputProps> = ({
className,
searchInputId,
value = '',
attributes = [] as string[],
formAdditionalItems,
Expand Down Expand Up @@ -298,6 +301,7 @@ const SearchInputBase: React.FunctionComponent<SearchInputProps> = ({
onKeyDown={onEnter}
onChange={onChangeHandler}
name={name}
inputId={searchInputId}
/>
{(renderUtilities || areUtilitiesDisplayed) && (
<TextInputGroupUtilities>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export interface TextInputGroupMainProps extends Omit<React.HTMLProps<HTMLDivEle
isExpanded?: boolean;
/** @beta The id of the element(s) controlled by the input. Required if role has a value of "combobox". */
'aria-controls'?: string;
/** The id of the input element */
inputId?: string;
}

const TextInputGroupMainBase: React.FunctionComponent<TextInputGroupMainProps> = ({
Expand All @@ -73,6 +75,7 @@ const TextInputGroupMainBase: React.FunctionComponent<TextInputGroupMainProps> =
role,
isExpanded,
'aria-controls': ariaControls,
inputId,
...props
}: TextInputGroupMainProps) => {
const { isDisabled } = React.useContext(TextInputGroupContext);
Expand All @@ -94,6 +97,7 @@ const TextInputGroupMainBase: React.FunctionComponent<TextInputGroupMainProps> =
disabled
aria-hidden="true"
value={hint}
id={inputId}
/>
)}
{icon && <span className={css(styles.textInputGroupIcon)}>{icon}</span>}
Expand All @@ -110,6 +114,7 @@ const TextInputGroupMainBase: React.FunctionComponent<TextInputGroupMainProps> =
placeholder={inputPlaceHolder}
name={name}
aria-activedescendant={ariaActivedescendant}
id={inputId}
{...(role && { role })}
{...(isExpanded !== undefined && { 'aria-expanded': isExpanded })}
{...(ariaControls && { 'aria-controls': ariaControls })}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ describe('TextInputGroupMain', () => {
expect(inputValue).toBeVisible();
});

it('passes the inputId prop to the input', () => {
render(<TextInputGroupMain inputId="input-id">Test</TextInputGroupMain>);

const input = screen.getByRole('textbox');

expect(input).toHaveAttribute('id', 'input-id');
});

it('passes the placeholder prop to the input', () => {
render(<TextInputGroupMain placeholder="placeholder text">Test</TextInputGroupMain>);

Expand Down

0 comments on commit 2d7171e

Please sign in to comment.