Skip to content

Commit

Permalink
feat(Combobox): ✨ Label now supports other elements (#2185)
Browse files Browse the repository at this point in the history
resolves #2179 

`label` type changed to `ReactNode`
  • Loading branch information
mimarz authored Jul 26, 2024
1 parent 69a38cf commit 567329b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-walls-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@digdir/designsystemet-react": patch
---

feat(Combobox): :sparkles: Label now supports other elements
16 changes: 16 additions & 0 deletions packages/react/src/components/form/Combobox/Combobox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,20 @@ describe('Combobox', () => {

expect(combobox).toHaveAttribute('aria-busy', 'true');
});

it('should have correct label when used as ReactNode', async () => {
await render({
label: (
<>
<strong>
<abbr>CSS</abbr>
</strong>
(Cascading Style Sheets)
</>
),
});
const combobox = screen.getByRole('combobox');

expect(combobox).toHaveAccessibleName('CSS (Cascading Style Sheets)');
});
});
10 changes: 6 additions & 4 deletions packages/react/src/components/form/Combobox/Combobox.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState, useRef, useEffect, useId, forwardRef } from 'react';
import type * as React from 'react';
import type { ReactNode, InputHTMLAttributes } from 'react';
import { FloatingFocusManager, FloatingPortal } from '@floating-ui/react';
import cl from 'clsx/lite';
import { useVirtualizer } from '@tanstack/react-virtual';
Expand All @@ -25,9 +25,11 @@ import { ComboboxContext } from './ComboboxContext';

export type ComboboxProps = {
/**
* Label for the combobox
* Label for the combobox.
*
* Passed label will be encapsulated by a `label` element.
*/
label?: string;
label?: ReactNode;
/**
* Visually hides `label` and `description` (still available for screen readers)
* @default false
Expand Down Expand Up @@ -114,7 +116,7 @@ export type ComboboxProps = {
chipSrLabel?: (option: Option) => string;
} & PortalProps &
FormFieldProps &
Omit<React.InputHTMLAttributes<HTMLInputElement>, 'size'>;
Omit<InputHTMLAttributes<HTMLInputElement>, 'size'>;

export const ComboboxComponent = forwardRef<HTMLInputElement, ComboboxProps>(
(
Expand Down

0 comments on commit 567329b

Please sign in to comment.