Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(SearchInput): Add ability to add id to input in SearchInput component #9974

Merged
merged 3 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading