-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(label): add required indicator component
- Loading branch information
Showing
6 changed files
with
145 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,65 @@ | ||
import { render, screen } from '@testing-library/react' | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { Label } from './Label' | ||
import { Label } from '.' | ||
|
||
describe('Label', () => { | ||
it('should render applying correct labelling when wrapping controls', () => { | ||
render( | ||
<Label> | ||
Name <input type="text" name="name" /> | ||
Title <input type="text" name="title" /> | ||
</Label> | ||
) | ||
|
||
expect(screen.getByLabelText('Name')).toBeInTheDocument() | ||
expect(screen.getByLabelText('Title')).toBeInTheDocument() | ||
}) | ||
|
||
it('should render applying correct labelling when not wrapping controls ', () => { | ||
render( | ||
<> | ||
<Label htmlFor="id">Name</Label> | ||
<input id="id" type="text" name="name" /> | ||
<Label htmlFor="id">Title</Label> | ||
<input id="id" type="text" name="title" /> | ||
</> | ||
) | ||
|
||
expect(screen.getByLabelText('Name')).toBeInTheDocument() | ||
expect(screen.getByLabelText('Title')).toBeInTheDocument() | ||
}) | ||
|
||
it('should render default required indicator', () => { | ||
render( | ||
<> | ||
<Label htmlFor="id"> | ||
Title | ||
<Label.RequiredIndicator /> | ||
</Label> | ||
<input id="id" type="text" name="title" /> | ||
</> | ||
) | ||
|
||
expect(screen.getByLabelText('Title')).toBeInTheDocument() | ||
|
||
const requiredEl = screen.getByText('*') | ||
|
||
expect(requiredEl).toHaveAttribute('role', 'presentation') | ||
expect(requiredEl).toHaveAttribute('aria-hidden', 'true') | ||
}) | ||
|
||
it('should render custom required indicator', () => { | ||
render( | ||
<> | ||
<Label htmlFor="id"> | ||
Title | ||
<Label.RequiredIndicator>Required</Label.RequiredIndicator> | ||
</Label> | ||
<input id="id" type="text" name="title" /> | ||
</> | ||
) | ||
|
||
expect(screen.getByLabelText('Title')).toBeInTheDocument() | ||
|
||
const requiredEl = screen.getByText('Required') | ||
|
||
expect(requiredEl).toHaveAttribute('role', 'presentation') | ||
expect(requiredEl).toHaveAttribute('aria-hidden', 'true') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
import { Label as LabelPrimitive } from '@radix-ui/react-label' | ||
import { Label as LabelPrimitive, LabelProps as LabelPrimitiveProps } from '@radix-ui/react-label' | ||
import { cx } from 'class-variance-authority' | ||
import { ComponentPropsWithoutRef, forwardRef } from 'react' | ||
import { forwardRef } from 'react' | ||
|
||
export interface LabelProps extends ComponentPropsWithoutRef<'label'> { | ||
/** | ||
* Change the component to the HTML tag or custom component of the only child. | ||
*/ | ||
asChild?: boolean | ||
/** | ||
* The id of the element the label is associated with. | ||
*/ | ||
htmlFor?: string | ||
} | ||
export type LabelProps = LabelPrimitiveProps | ||
|
||
export const Label = forwardRef<HTMLLabelElement, LabelProps>(({ className, ...others }, ref) => { | ||
return <LabelPrimitive ref={ref} className={cx('text-body-1', className)} {...others} /> | ||
return ( | ||
<LabelPrimitive | ||
ref={ref} | ||
data-spark-component="label" | ||
className={cx('text-body-1', className)} | ||
{...others} | ||
/> | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { cx } from 'class-variance-authority' | ||
import { ComponentPropsWithoutRef, forwardRef } from 'react' | ||
|
||
export type LabelRequiredIndicatorProps = ComponentPropsWithoutRef<'span'> | ||
|
||
export const LabelRequiredIndicator = forwardRef<HTMLSpanElement, LabelRequiredIndicatorProps>( | ||
({ className, children = '*', ...others }, ref) => { | ||
return ( | ||
<span | ||
ref={ref} | ||
data-spark-component="label-required-indicator" | ||
role="presentation" | ||
aria-hidden="true" | ||
className={cx(className, 'text-on-surface/dim-3 text-caption')} | ||
{...others} | ||
> | ||
{children} | ||
</span> | ||
) | ||
} | ||
) | ||
|
||
LabelRequiredIndicator.displayName = 'Label.RequiredIndicator' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,11 @@ | ||
export * from './Label' | ||
import { Label as Root } from './Label' | ||
import { LabelRequiredIndicator } from './LabelRequiredIndicator' | ||
|
||
export type { LabelProps } from './Label' | ||
export type { LabelRequiredIndicatorProps } from './LabelRequiredIndicator' | ||
|
||
export const Label: typeof Root & { | ||
RequiredIndicator: typeof LabelRequiredIndicator | ||
} = Object.assign(Root, { | ||
RequiredIndicator: LabelRequiredIndicator, | ||
}) |