diff --git a/packages/web-react/src/components/Checkbox/Checkbox.tsx b/packages/web-react/src/components/Checkbox/Checkbox.tsx index 276fe16bb1..2023c4843a 100644 --- a/packages/web-react/src/components/Checkbox/Checkbox.tsx +++ b/packages/web-react/src/components/Checkbox/Checkbox.tsx @@ -4,7 +4,7 @@ import classNames from 'classnames'; import React, { ForwardedRef, forwardRef } from 'react'; import { useStyleProps } from '../../hooks'; import { SpiritCheckboxProps } from '../../types'; -import { HelperText, ValidationText, useAriaIds } from '../Field'; +import { HelperText, Label, ValidationText, useAriaIds } from '../Field'; import { useValidationTextRole } from '../Field/useValidationTextRole'; import { useCheckboxStyleProps } from './useCheckboxStyleProps'; @@ -33,7 +33,11 @@ const _Checkbox = (props: SpiritCheckboxProps, ref: ForwardedRef + + ); }; diff --git a/packages/web-react/src/components/Field/Label.tsx b/packages/web-react/src/components/Field/Label.tsx new file mode 100644 index 0000000000..18b7328267 --- /dev/null +++ b/packages/web-react/src/components/Field/Label.tsx @@ -0,0 +1,18 @@ +'use client'; + +import React, { ElementType } from 'react'; +import { useStyleProps } from '../../hooks'; +import { SpiritLabelProps } from '../../types'; + +const Label = (props: SpiritLabelProps): JSX.Element => { + const { elementType: ElementTag = 'label', children, htmlFor, for: labelFor, ...restProps } = props; + const { styleProps, props: otherProps } = useStyleProps(restProps); + + return ( + + {children} + + ); +}; + +export default Label; diff --git a/packages/web-react/src/components/Field/README.md b/packages/web-react/src/components/Field/README.md index ce6eb22e9f..af16dd6215 100644 --- a/packages/web-react/src/components/Field/README.md +++ b/packages/web-react/src/components/Field/README.md @@ -63,4 +63,38 @@ Advanced example: | `helperText` | \[`ReactNode` \| `ReactNode[]`] | — | ✕ | Validation text, only visible if validationState is | | `id` | `string` | — | ✕ | Component id | +## Label + +The `Label` component is used to associate text with a form control, such as an input, checkbox, or radio button. +It improves accessibility by allowing users to click the label to interact with the corresponding input. +This component can be customized using various props to fit different use cases. + +Simple Label example: + +```jsx + +``` + +### Full Example + +```jsx + +``` + +### API + +| Name | Type | Default | Required | Description | +| ------------- | ------------- | ------- | -------- | ----------------------------------------------- | +| `elementType` | `ElementType` | `label` | ✕ | Type of element used as wrapper | +| `htmlFor` | `string` | — | ✕ | ID of the associated form element (e.g., input) | + +On top of the API options, the components accept [additional attributes][readme-additional-attributes]. +If you need more control over the styling of a component, you can use [style props][readme-style-props] +and [escape hatches][readme-escape-hatches]. + [aria-alert-role]: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/alert_role +[readme-additional-attributes]: https://github.com/lmc-eu/spirit-design-system/blob/main/packages/web-react/README.md#additional-attributes +[readme-escape-hatches]: https://github.com/lmc-eu/spirit-design-system/blob/main/packages/web-react/README.md#escape-hatches +[readme-style-props]: https://github.com/lmc-eu/spirit-design-system/blob/main/packages/web-react/README.md#style-props diff --git a/packages/web-react/src/components/Field/__tests__/Label.test.tsx b/packages/web-react/src/components/Field/__tests__/Label.test.tsx new file mode 100644 index 0000000000..b197d70606 --- /dev/null +++ b/packages/web-react/src/components/Field/__tests__/Label.test.tsx @@ -0,0 +1,25 @@ +import '@testing-library/jest-dom'; +import { render, screen } from '@testing-library/react'; +import React from 'react'; +import { restPropsTest, stylePropsTest } from '@local/tests'; +import { SpiritLabelProps } from '../../../types'; +import Label from '../Label'; + +describe('Label', () => { + stylePropsTest(Label); + + restPropsTest((props: SpiritLabelProps) =>