Skip to content

Commit

Permalink
search
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsnes committed Oct 30, 2024
1 parent dda6728 commit eccd23c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/react/src/components/form/Search/Search.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Search', () => {
expect(screen.getByDisplayValue('test')).toBeDefined();
});

test('has correct label when label is hidden', () => {
test('has correct aria-label when hiding label', () => {
render({ label: 'label', hideLabel: true });

expect(screen.getByLabelText('label')).toBeDefined();
Expand Down Expand Up @@ -48,14 +48,14 @@ describe('Search', () => {

it('Focuses on search field when label is clicked and id is not given', async () => {
const label = 'Lorem ipsum';
const { user } = render({ label });
const { user } = render({ label, hideLabel: false });
await act(async () => await user.click(screen.getByText(label)));
expect(screen.getByRole('searchbox')).toHaveFocus();
});

it('Focuses on search field when label is clicked and id is given', async () => {
const label = 'Lorem ipsum';
const { user } = render({ id: 'some-unique-id', label });
const { user } = render({ id: 'some-unique-id', label, hideLabel: false });
await act(async () => await user.click(screen.getByText(label)));
expect(screen.getByRole('searchbox')).toHaveFocus();
});
Expand Down
14 changes: 13 additions & 1 deletion packages/react/src/components/form/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { forwardRef, useCallback, useRef, useState } from 'react';
import type { DefaultProps } from '../../../types';
import { omit } from '../../../utilities';
import { Button } from '../../Button/Button';
import { Label } from '../../Label';
import { Paragraph } from '../../Paragraph';
import type { FormFieldProps } from '../useFormField';

Expand Down Expand Up @@ -113,6 +114,17 @@ export const Search = forwardRef<HTMLInputElement, SearchProps>(
className,
)}
>
{!hideLabel && (
<Label
data-size={size}
weight='medium'
htmlFor={inputProps.id}
className={cl('ds-search__label', hideLabel && 'ds-sr-only')}
>
<span>{label}</span>
</Label>
)}

<div className={'ds-search__field'}>
<div className={cl('ds-search__field', `ds-search--${size}`)}>
{isSimple && (
Expand All @@ -136,7 +148,7 @@ export const Search = forwardRef<HTMLInputElement, SearchProps>(
{...omit(['size', 'error', 'errorId', 'readOnly'], rest)}
{...inputProps}
onChange={handleChange}
aria-label={label?.toString()}
aria-label={hideLabel ? label?.toString() : undefined}
/>
{showClearButton && (
<button
Expand Down

0 comments on commit eccd23c

Please sign in to comment.