Skip to content

Commit

Permalink
use dynamic id in StudioSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
standeren committed Jan 8, 2025
1 parent f2cc3c9 commit 1e25e72
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('StudioSearch', () => {
testCustomAttributes(renderTestSearch, getSearchBox);
});

it('should find search component with label name', () => {
it('should render search field with label name when provided', () => {
const label = 'Search for something';
renderTestSearch({ label });
const search = screen.getByRole('searchbox', { name: label });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import React, { forwardRef } from 'react';
import React, { forwardRef, useId } from 'react';
import { Label, Search, type SearchProps } from '@digdir/designsystemet-react';
import type { WithoutAsChild } from '../../types/WithoutAsChild';
import { StudioFieldset } from '../StudioFieldset';

export type StudioSearchProps = WithoutAsChild<SearchProps> & { legend?: string };
export type StudioSearchProps = WithoutAsChild<SearchProps>;

const StudioSearch = forwardRef<HTMLInputElement, StudioSearchProps>(
({ size = 'sm', label, legend, ...rest }, ref) => {
({ size = 'sm', label, id, ...rest }, ref) => {
const generatedId = useId();
const searchId = id ?? generatedId;
const showLabel = !!label;

return showLabel ? (
<StudioFieldset legend={legend}>
<Label htmlFor='searchId'>{label}</Label>
<Search {...rest} id='searchId' size={size} ref={ref} />
</StudioFieldset>
<>
<Label htmlFor={searchId}>{label}</Label>
<Search {...rest} id={searchId} size={size} ref={ref} />
</>
) : (
<Search {...rest} size={size} ref={ref} />
);
Expand Down

0 comments on commit 1e25e72

Please sign in to comment.