@appica/ui-react 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +37 -0
- package/agent-rules.md +3 -0
- package/appica.css +1 -1
- package/dist/components/autocomplete/autocomplete.d.ts.map +1 -1
- package/dist/components/autocomplete/autocomplete.js +1 -5
- package/dist/components/autocomplete/autocomplete.js.map +1 -1
- package/dist/components/breadcrumb/breadcrumb.js +1 -1
- package/dist/components/breadcrumb/breadcrumb.js.map +1 -1
- package/dist/components/carousel/carousel.d.ts +25 -4
- package/dist/components/carousel/carousel.d.ts.map +1 -1
- package/dist/components/carousel/carousel.js +277 -48
- package/dist/components/carousel/carousel.js.map +1 -1
- package/dist/components/combobox/combobox.d.ts.map +1 -1
- package/dist/components/combobox/combobox.js +1 -5
- package/dist/components/combobox/combobox.js.map +1 -1
- package/dist/components/copy-button/copy-button.d.ts +2 -2
- package/dist/components/copy-button/copy-button.d.ts.map +1 -1
- package/dist/components/copy-button/copy-button.js +49 -46
- package/dist/components/copy-button/copy-button.js.map +1 -1
- package/dist/components/date-field/date-field.d.ts.map +1 -1
- package/dist/components/date-field/date-field.js +7 -0
- package/dist/components/date-field/date-field.js.map +1 -1
- package/dist/components/date-picker/date-picker.d.ts +10 -0
- package/dist/components/date-picker/date-picker.d.ts.map +1 -1
- package/dist/components/date-picker/date-picker.js +14 -0
- package/dist/components/date-picker/date-picker.js.map +1 -1
- package/dist/components/field/field.d.ts.map +1 -1
- package/dist/components/field/field.js +1 -0
- package/dist/components/field/field.js.map +1 -1
- package/dist/components/loader/loader.d.ts +1 -1
- package/dist/components/loader/loader.d.ts.map +1 -1
- package/dist/components/loader/loader.js +7 -5
- package/dist/components/loader/loader.js.map +1 -1
- package/dist/components/number-field/number-field.d.ts +1 -1
- package/dist/components/number-field/number-field.d.ts.map +1 -1
- package/dist/components/number-field/number-field.js +7 -0
- package/dist/components/number-field/number-field.js.map +1 -1
- package/dist/components/pagination/pagination.d.ts.map +1 -1
- package/dist/components/pagination/pagination.js +1 -2
- package/dist/components/pagination/pagination.js.map +1 -1
- package/dist/components/spinner/spinner.d.ts +1 -1
- package/dist/components/spinner/spinner.d.ts.map +1 -1
- package/dist/components/spinner/spinner.js +7 -5
- package/dist/components/spinner/spinner.js.map +1 -1
- package/dist/components/time-field/time-field.d.ts.map +1 -1
- package/dist/components/time-field/time-field.js +7 -0
- package/dist/components/time-field/time-field.js.map +1 -1
- package/dist/index.js +1 -1
- package/package.json +11 -11
- package/src/components/autocomplete/autocomplete.test.tsx +33 -0
- package/src/components/autocomplete/autocomplete.tsx +1 -5
- package/src/components/breadcrumb/breadcrumb.test.tsx +57 -2
- package/src/components/breadcrumb/breadcrumb.tsx +1 -1
- package/src/components/carousel/carousel.test.tsx +918 -13
- package/src/components/carousel/carousel.tsx +338 -54
- package/src/components/chip/chip.test.tsx +4 -5
- package/src/components/combobox/combobox.test.tsx +33 -0
- package/src/components/combobox/combobox.tsx +1 -5
- package/src/components/context-menu/context-menu.test.tsx +2 -1
- package/src/components/copy-button/copy-button.test.tsx +68 -3
- package/src/components/copy-button/copy-button.tsx +49 -43
- package/src/components/date-field/date-field.test.tsx +82 -0
- package/src/components/date-field/date-field.tsx +7 -0
- package/src/components/date-picker/date-picker.test.tsx +70 -0
- package/src/components/date-picker/date-picker.tsx +26 -0
- package/src/components/dropdown-menu/dropdown-menu.test.tsx +2 -1
- package/src/components/field/field.test.tsx +34 -0
- package/src/components/field/field.tsx +1 -0
- package/src/components/loader/loader.test.tsx +17 -4
- package/src/components/loader/loader.tsx +2 -2
- package/src/components/number-field/number-field.test.tsx +59 -0
- package/src/components/number-field/number-field.tsx +8 -0
- package/src/components/pagination/pagination.test.tsx +83 -0
- package/src/components/pagination/pagination.tsx +1 -2
- package/src/components/spinner/spinner.test.tsx +17 -4
- package/src/components/spinner/spinner.tsx +2 -2
- package/src/components/time-field/time-field.test.tsx +64 -0
- package/src/components/time-field/time-field.tsx +7 -0
|
@@ -252,6 +252,39 @@ describe('Combobox', () => {
|
|
|
252
252
|
expect(screen.queryByRole('button', { name: 'Toggle popup' })).toBeNull()
|
|
253
253
|
})
|
|
254
254
|
|
|
255
|
+
it('keeps ComboboxEmpty in the accessibility tree as a status region while results show', async () => {
|
|
256
|
+
const user = setupUser()
|
|
257
|
+
renderSingle()
|
|
258
|
+
await user.click(screen.getByRole('combobox'))
|
|
259
|
+
await screen.findByRole('option', { name: 'Next.js' })
|
|
260
|
+
|
|
261
|
+
const empty = document.querySelector('[data-slot="combobox-empty"]')
|
|
262
|
+
expect(empty).toBeEmptyDOMElement()
|
|
263
|
+
expect(empty).not.toHaveClass('hidden')
|
|
264
|
+
expect(screen.getByRole('status')).toBe(empty)
|
|
265
|
+
})
|
|
266
|
+
|
|
267
|
+
it('renders the no-results text inside the status region', async () => {
|
|
268
|
+
const user = setupUser()
|
|
269
|
+
renderSingle()
|
|
270
|
+
const input = screen.getByRole('combobox')
|
|
271
|
+
await user.click(input)
|
|
272
|
+
await user.type(input, 'zzzzzz')
|
|
273
|
+
const status = await screen.findByRole('status')
|
|
274
|
+
expect(status).toHaveTextContent('No items found.')
|
|
275
|
+
expect(status).toHaveAttribute('data-slot', 'combobox-empty')
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
it('has no accessibility violations with the popup open and no matches', async () => {
|
|
279
|
+
const user = setupUser()
|
|
280
|
+
renderSingle()
|
|
281
|
+
const input = screen.getByRole('combobox')
|
|
282
|
+
await user.click(input)
|
|
283
|
+
await user.type(input, 'zzzzzz')
|
|
284
|
+
await screen.findByText('No items found.')
|
|
285
|
+
expect(await axe(document.body, { rules: { region: { enabled: false } } })).toHaveNoViolations()
|
|
286
|
+
})
|
|
287
|
+
|
|
255
288
|
it('shows ComboboxEmpty when the filter has no matches', async () => {
|
|
256
289
|
const user = setupUser()
|
|
257
290
|
renderSingle()
|
|
@@ -492,11 +492,7 @@ function ComboboxEmpty({ className, ...props }: ComboboxEmptyProps) {
|
|
|
492
492
|
return (
|
|
493
493
|
<BaseCombobox.Empty
|
|
494
494
|
data-slot="combobox-empty"
|
|
495
|
-
className={cn(
|
|
496
|
-
'text-foreground-muted hidden text-center group-data-empty/combobox-content:block',
|
|
497
|
-
EMPTY_SIZE[size],
|
|
498
|
-
className,
|
|
499
|
-
)}
|
|
495
|
+
className={cn('text-foreground-muted text-center empty:p-0', EMPTY_SIZE[size], className)}
|
|
500
496
|
{...props}
|
|
501
497
|
/>
|
|
502
498
|
)
|
|
@@ -146,7 +146,8 @@ describe('ContextMenu', () => {
|
|
|
146
146
|
const user = setupUser()
|
|
147
147
|
render(<BasicMenu />)
|
|
148
148
|
await user.pointer({ keys: '[MouseRight]', target: screen.getByTestId('trigger') })
|
|
149
|
-
await screen.findByRole('
|
|
149
|
+
const menu = await screen.findByRole('menu')
|
|
150
|
+
await waitFor(() => expect(menu.contains(document.activeElement)).toBe(true))
|
|
150
151
|
await user.keyboard('{ArrowDown}')
|
|
151
152
|
await waitFor(() => {
|
|
152
153
|
const profile = screen.getByRole('menuitem', { name: 'Profile' })
|
|
@@ -24,6 +24,35 @@ describe('CopyButton', () => {
|
|
|
24
24
|
expect(screen.getByText('Copy SVG')).toBeInTheDocument()
|
|
25
25
|
})
|
|
26
26
|
|
|
27
|
+
it('names the button from its visible children instead of an aria-label', () => {
|
|
28
|
+
render(
|
|
29
|
+
<CopyButton value="hello" label="Copy markup">
|
|
30
|
+
Copy SVG
|
|
31
|
+
</CopyButton>,
|
|
32
|
+
)
|
|
33
|
+
const button = screen.getByRole('button', { name: 'Copy SVG' })
|
|
34
|
+
expect(button).not.toHaveAttribute('aria-label')
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('keeps the aria-label on an icon-only button', () => {
|
|
38
|
+
render(<CopyButton value="hello" label="Copy code" />)
|
|
39
|
+
expect(screen.getByRole('button', { name: 'Copy code' })).toHaveAttribute('aria-label', 'Copy code')
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('treats empty children as icon-only', () => {
|
|
43
|
+
render(<CopyButton value="hello">{false}</CopyButton>)
|
|
44
|
+
expect(screen.getByRole('button')).toHaveAttribute('aria-label', 'Copy')
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('lets an explicit aria-label win', () => {
|
|
48
|
+
render(
|
|
49
|
+
<CopyButton value="hello" aria-label="Copy the install command">
|
|
50
|
+
Copy
|
|
51
|
+
</CopyButton>,
|
|
52
|
+
)
|
|
53
|
+
expect(screen.getByRole('button', { name: 'Copy the install command' })).toBeInTheDocument()
|
|
54
|
+
})
|
|
55
|
+
|
|
27
56
|
it('swaps a string label to the copied label on success', async () => {
|
|
28
57
|
const { user } = setup()
|
|
29
58
|
render(
|
|
@@ -32,11 +61,39 @@ describe('CopyButton', () => {
|
|
|
32
61
|
</CopyButton>,
|
|
33
62
|
)
|
|
34
63
|
|
|
35
|
-
|
|
64
|
+
const button = screen.getByRole('button')
|
|
65
|
+
await user.click(button)
|
|
36
66
|
|
|
37
67
|
await waitFor(() => expect(screen.queryByText('Copy SVG')).not.toBeInTheDocument())
|
|
38
|
-
|
|
39
|
-
expect(
|
|
68
|
+
expect(button).toHaveAccessibleName('Copied')
|
|
69
|
+
expect(button).not.toHaveAttribute('aria-label')
|
|
70
|
+
expect(screen.getByRole('status')).toHaveTextContent('Copied')
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('renders the status region as a sibling of the button, not inside it', async () => {
|
|
74
|
+
const { user } = setup()
|
|
75
|
+
const { container } = render(
|
|
76
|
+
<div data-testid="parent">
|
|
77
|
+
<CopyButton value="hello" copiedLabel="Code copied" />
|
|
78
|
+
</div>,
|
|
79
|
+
)
|
|
80
|
+
const button = screen.getByRole('button')
|
|
81
|
+
const status = screen.getByRole('status')
|
|
82
|
+
|
|
83
|
+
expect(button.contains(status)).toBe(false)
|
|
84
|
+
expect(status.parentElement).toBe(button.parentElement)
|
|
85
|
+
expect(status.parentElement).toBe(screen.getByTestId('parent'))
|
|
86
|
+
expect(status).toBeEmptyDOMElement()
|
|
87
|
+
expect(container.querySelectorAll('[data-slot="copy-button"]')).toHaveLength(1)
|
|
88
|
+
|
|
89
|
+
await user.click(button)
|
|
90
|
+
await waitFor(() => expect(status).toHaveTextContent('Code copied'))
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('forwards the ref to the button', () => {
|
|
94
|
+
const ref = React.createRef<HTMLButtonElement>()
|
|
95
|
+
render(<CopyButton value="hello" ref={ref} />)
|
|
96
|
+
expect(ref.current).toBe(screen.getByRole('button'))
|
|
40
97
|
})
|
|
41
98
|
|
|
42
99
|
it('renders a custom resting icon in place of the copy glyph and still swaps to the check', async () => {
|
|
@@ -157,4 +214,12 @@ describe('CopyButton', () => {
|
|
|
157
214
|
const { container } = render(<CopyButton value="hello" />)
|
|
158
215
|
expect(await axe(container)).toHaveNoViolations()
|
|
159
216
|
})
|
|
217
|
+
|
|
218
|
+
it('has no accessibility violations with a visible label in the copied state', async () => {
|
|
219
|
+
const { user } = setup()
|
|
220
|
+
const { container } = render(<CopyButton value="hello">Copy SVG</CopyButton>)
|
|
221
|
+
await user.click(screen.getByRole('button'))
|
|
222
|
+
await waitFor(() => expect(screen.getByRole('status')).toHaveTextContent('Copied'))
|
|
223
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
224
|
+
})
|
|
160
225
|
})
|
|
@@ -56,12 +56,12 @@ interface CopyButtonProps extends Omit<ButtonProps, 'value' | 'onCopy'> {
|
|
|
56
56
|
*/
|
|
57
57
|
timeout?: number
|
|
58
58
|
/**
|
|
59
|
-
* Accessible name
|
|
59
|
+
* Accessible name of an icon-only button in the idle state. Visible children name the button instead.
|
|
60
60
|
* @default 'Copy'
|
|
61
61
|
*/
|
|
62
62
|
label?: string
|
|
63
63
|
/**
|
|
64
|
-
*
|
|
64
|
+
* Announced after a successful copy, and names an icon-only button until it reverts; a string child also swaps to this.
|
|
65
65
|
* @default 'Copied'
|
|
66
66
|
*/
|
|
67
67
|
copiedLabel?: string
|
|
@@ -110,51 +110,57 @@ function CopyButton({
|
|
|
110
110
|
resetRef.current = setTimeout(() => setCopied(false), timeout)
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
+
const iconOnly = React.Children.toArray(children).length === 0
|
|
114
|
+
|
|
113
115
|
return (
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
{icon ?? (
|
|
134
|
-
<svg {...ICON_SVG_PROPS}>
|
|
135
|
-
<path d="M4.012 16.737c-.307-.175-.562-.427-.739-.732S3.001 15.353 3 15V5c0-1.1.9-2 2-2h10c.75 0 1.158.385 1.5 1M7 9.667c0-.707.281-1.386.781-1.886S8.96 7 9.667 7h8.666c.35 0 .697.069 1.021.203s.618.33.865.578.444.542.578.865A2.67 2.67 0 0 1 21 9.667v8.666a2.67 2.67 0 0 1-.781 1.886 2.67 2.67 0 0 1-1.886.781H9.667a2.67 2.67 0 0 1-1.021-.203c-.324-.134-.618-.331-.865-.578s-.444-.542-.578-.865S7 18.683 7 18.333V9.667z" />
|
|
136
|
-
</svg>
|
|
137
|
-
)}
|
|
138
|
-
</span>
|
|
139
|
-
<svg {...ICON_SVG_PROPS}>
|
|
140
|
-
<path
|
|
141
|
-
d={CHECK_PATH}
|
|
142
|
-
pathLength={1}
|
|
143
|
-
strokeDasharray="1 2"
|
|
116
|
+
<>
|
|
117
|
+
{/* First, so last-child rules (`space-x-*`, `divide-*`) still see the button as last, and
|
|
118
|
+
`!important`, so a parent's `*:` utilities (ButtonGroup's `*:relative`) can't pull it into flow. */}
|
|
119
|
+
<span role="status" className="sr-only!">
|
|
120
|
+
{copied ? copiedLabel : ''}
|
|
121
|
+
</span>
|
|
122
|
+
<Button
|
|
123
|
+
data-slot="copy-button"
|
|
124
|
+
data-copied={copied || undefined}
|
|
125
|
+
type="button"
|
|
126
|
+
variant={variant}
|
|
127
|
+
size={size}
|
|
128
|
+
aria-label={iconOnly ? (copied ? copiedLabel : label) : undefined}
|
|
129
|
+
onClick={handleClick}
|
|
130
|
+
className={cn('group/copy', className)}
|
|
131
|
+
{...props}
|
|
132
|
+
>
|
|
133
|
+
<span data-icon={children != null ? 'start' : undefined} className="grid place-items-center *:[grid-area:1/1]">
|
|
134
|
+
<span
|
|
144
135
|
className={cn(
|
|
145
|
-
'[
|
|
146
|
-
'
|
|
147
|
-
'group-data-copied/copy:delay-100 group-data-copied/copy:duration-350 group-data-copied/copy:ease-out',
|
|
136
|
+
'grid place-items-center transition-[opacity,scale] delay-150 duration-150 ease-out group-data-copied/copy:delay-0',
|
|
137
|
+
'group-data-copied/copy:scale-50 group-data-copied/copy:opacity-0',
|
|
148
138
|
'motion-reduce:transition-none',
|
|
149
139
|
)}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
140
|
+
>
|
|
141
|
+
{icon ?? (
|
|
142
|
+
<svg {...ICON_SVG_PROPS}>
|
|
143
|
+
<path d="M4.012 16.737c-.307-.175-.562-.427-.739-.732S3.001 15.353 3 15V5c0-1.1.9-2 2-2h10c.75 0 1.158.385 1.5 1M7 9.667c0-.707.281-1.386.781-1.886S8.96 7 9.667 7h8.666c.35 0 .697.069 1.021.203s.618.33.865.578.444.542.578.865A2.67 2.67 0 0 1 21 9.667v8.666a2.67 2.67 0 0 1-.781 1.886 2.67 2.67 0 0 1-1.886.781H9.667a2.67 2.67 0 0 1-1.021-.203c-.324-.134-.618-.331-.865-.578s-.444-.542-.578-.865S7 18.683 7 18.333V9.667z" />
|
|
144
|
+
</svg>
|
|
145
|
+
)}
|
|
146
|
+
</span>
|
|
147
|
+
<svg {...ICON_SVG_PROPS}>
|
|
148
|
+
<path
|
|
149
|
+
d={CHECK_PATH}
|
|
150
|
+
pathLength={1}
|
|
151
|
+
strokeDasharray="1 2"
|
|
152
|
+
className={cn(
|
|
153
|
+
'[stroke-dashoffset:1.02] group-data-copied/copy:[stroke-dashoffset:0]',
|
|
154
|
+
'transition-[stroke-dashoffset] duration-200 ease-in',
|
|
155
|
+
'group-data-copied/copy:delay-100 group-data-copied/copy:duration-350 group-data-copied/copy:ease-out',
|
|
156
|
+
'motion-reduce:transition-none',
|
|
157
|
+
)}
|
|
158
|
+
/>
|
|
159
|
+
</svg>
|
|
160
|
+
</span>
|
|
161
|
+
{typeof children === 'string' && copied ? copiedLabel : children}
|
|
162
|
+
</Button>
|
|
163
|
+
</>
|
|
158
164
|
)
|
|
159
165
|
}
|
|
160
166
|
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
1
2
|
import { render, screen, fireEvent } from '@testing-library/react'
|
|
2
3
|
import userEvent from '@testing-library/user-event'
|
|
3
4
|
import { describe, it, expect, vi } from 'vitest'
|
|
4
5
|
import { axe } from 'vitest-axe'
|
|
6
|
+
import { Field, FieldDescription, FieldError, FieldLabel } from '../field/field'
|
|
5
7
|
import { DateField } from './date-field'
|
|
6
8
|
|
|
7
9
|
function getSegment(name: 'day' | 'month' | 'year') {
|
|
@@ -349,6 +351,86 @@ describe('DateField', () => {
|
|
|
349
351
|
expect(root).toHaveAttribute('data-disabled')
|
|
350
352
|
})
|
|
351
353
|
|
|
354
|
+
describe('inside a Field', () => {
|
|
355
|
+
function renderInField(props: React.ComponentProps<typeof DateField> = {}, invalid = false) {
|
|
356
|
+
return render(
|
|
357
|
+
<Field invalid={invalid}>
|
|
358
|
+
<FieldLabel>Date of birth</FieldLabel>
|
|
359
|
+
<DateField defaultValue={JUN_23} {...props} />
|
|
360
|
+
<FieldDescription>As shown on your passport.</FieldDescription>
|
|
361
|
+
<FieldError match>Enter a valid date.</FieldError>
|
|
362
|
+
</Field>,
|
|
363
|
+
)
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
it('takes its accessible name from the FieldLabel', () => {
|
|
367
|
+
renderInField()
|
|
368
|
+
const group = screen.getByRole('group', { name: 'Date of birth' })
|
|
369
|
+
expect(group).toHaveAttribute('data-slot', 'date-field')
|
|
370
|
+
expect(screen.getByText('Date of birth')).not.toHaveAttribute('for')
|
|
371
|
+
})
|
|
372
|
+
|
|
373
|
+
it('is described by the FieldDescription', () => {
|
|
374
|
+
renderInField()
|
|
375
|
+
const description = screen.getByText('As shown on your passport.')
|
|
376
|
+
expect(screen.getByRole('group', { name: 'Date of birth' })).toHaveAttribute(
|
|
377
|
+
'aria-describedby',
|
|
378
|
+
expect.stringContaining(description.id),
|
|
379
|
+
)
|
|
380
|
+
})
|
|
381
|
+
|
|
382
|
+
it('is described by the FieldError and marks every segment invalid', () => {
|
|
383
|
+
renderInField({}, true)
|
|
384
|
+
const group = screen.getByRole('group', { name: 'Date of birth' })
|
|
385
|
+
const error = screen.getByText('Enter a valid date.')
|
|
386
|
+
expect(group).toHaveAttribute('aria-describedby', expect.stringContaining(error.id))
|
|
387
|
+
expect(group).toHaveAttribute('aria-invalid', 'true')
|
|
388
|
+
for (const segment of screen.getAllByRole('spinbutton')) {
|
|
389
|
+
expect(segment).toHaveAttribute('aria-invalid', 'true')
|
|
390
|
+
}
|
|
391
|
+
})
|
|
392
|
+
|
|
393
|
+
it('lets consumer aria-label and aria-describedby win over the Field wiring', () => {
|
|
394
|
+
renderInField({ 'aria-label': 'Birthday', 'aria-describedby': 'custom-hint' })
|
|
395
|
+
const group = screen.getByRole('group', { name: 'Birthday' })
|
|
396
|
+
expect(group).not.toHaveAttribute('aria-labelledby')
|
|
397
|
+
expect(group).toHaveAttribute('aria-describedby', 'custom-hint')
|
|
398
|
+
})
|
|
399
|
+
|
|
400
|
+
it('lets a consumer aria-labelledby win over the FieldLabel', () => {
|
|
401
|
+
const { container } = render(
|
|
402
|
+
<Field>
|
|
403
|
+
<FieldLabel>Date of birth</FieldLabel>
|
|
404
|
+
<span id="custom-label">Birthday</span>
|
|
405
|
+
<DateField defaultValue={JUN_23} aria-labelledby="custom-label" />
|
|
406
|
+
</Field>,
|
|
407
|
+
)
|
|
408
|
+
expect(getRoot(container)).toHaveAttribute('aria-labelledby', 'custom-label')
|
|
409
|
+
expect(screen.getByRole('group', { name: 'Birthday' })).toBe(getRoot(container))
|
|
410
|
+
})
|
|
411
|
+
|
|
412
|
+
it('lets a consumer aria-invalid win on the group and segments', () => {
|
|
413
|
+
renderInField({ 'aria-invalid': false }, true)
|
|
414
|
+
const group = screen.getByRole('group', { name: 'Date of birth' })
|
|
415
|
+
expect(group).toHaveAttribute('aria-invalid', 'false')
|
|
416
|
+
for (const segment of screen.getAllByRole('spinbutton')) {
|
|
417
|
+
expect(segment).toHaveAttribute('aria-invalid', 'false')
|
|
418
|
+
}
|
|
419
|
+
})
|
|
420
|
+
|
|
421
|
+
it('does not mark segments invalid when the field is valid', () => {
|
|
422
|
+
renderInField()
|
|
423
|
+
for (const segment of screen.getAllByRole('spinbutton')) {
|
|
424
|
+
expect(segment).not.toHaveAttribute('aria-invalid')
|
|
425
|
+
}
|
|
426
|
+
})
|
|
427
|
+
|
|
428
|
+
it('has no accessibility violations when labeled, described and invalid', async () => {
|
|
429
|
+
const { container } = renderInField({}, true)
|
|
430
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
431
|
+
})
|
|
432
|
+
})
|
|
433
|
+
|
|
352
434
|
it('renders startSlot and endSlot containers', () => {
|
|
353
435
|
const { container } = render(
|
|
354
436
|
<DateField
|
|
@@ -4,6 +4,7 @@ import * as React from 'react'
|
|
|
4
4
|
import { format as formatDate, formatISO, getDaysInMonth, isValid } from 'date-fns'
|
|
5
5
|
import { type VariantProps } from 'class-variance-authority'
|
|
6
6
|
import { useFieldRootContext } from '@base-ui/react/internals/field-root-context'
|
|
7
|
+
import { useLabelableContext, useLabelableId } from '@base-ui/react/internals/labelable-provider'
|
|
7
8
|
import { cn } from '../../internal/utils'
|
|
8
9
|
import { useDirection } from '../../hooks/use-direction'
|
|
9
10
|
import { inputVariants } from '../input/input-variants'
|
|
@@ -223,6 +224,9 @@ function DateField({
|
|
|
223
224
|
const name = nameProp ?? field.name
|
|
224
225
|
const ariaInvalid = rest['aria-invalid']
|
|
225
226
|
const invalid = ariaInvalid === true || ariaInvalid === 'true' || field.invalid === true
|
|
227
|
+
const resolvedAriaInvalid = ariaInvalid ?? (invalid || undefined)
|
|
228
|
+
const { labelId, getDescriptionProps } = useLabelableContext()
|
|
229
|
+
useLabelableId({ id: null })
|
|
226
230
|
|
|
227
231
|
const nodes = React.useMemo(() => parseFormat(format), [format])
|
|
228
232
|
const segmentCount = React.useMemo(() => nodes.reduce((n, x) => n + (x.kind === 'segment' ? 1 : 0), 0), [nodes])
|
|
@@ -462,6 +466,8 @@ function DateField({
|
|
|
462
466
|
data-invalid={invalid || undefined}
|
|
463
467
|
aria-invalid={invalid || undefined}
|
|
464
468
|
aria-disabled={disabled || undefined}
|
|
469
|
+
aria-labelledby={rest['aria-label'] == null ? labelId : undefined}
|
|
470
|
+
aria-describedby={getDescriptionProps({})['aria-describedby']}
|
|
465
471
|
role="group"
|
|
466
472
|
ref={ref}
|
|
467
473
|
className={cn(
|
|
@@ -512,6 +518,7 @@ function DateField({
|
|
|
512
518
|
spellCheck={false}
|
|
513
519
|
aria-label={node.type}
|
|
514
520
|
aria-readonly={readOnly || undefined}
|
|
521
|
+
aria-invalid={resolvedAriaInvalid}
|
|
515
522
|
aria-valuemin={range.min}
|
|
516
523
|
aria-valuemax={range.max}
|
|
517
524
|
aria-valuenow={partVal ?? undefined}
|
|
@@ -3,6 +3,7 @@ import userEvent from '@testing-library/user-event'
|
|
|
3
3
|
import { describe, it, expect, vi } from 'vitest'
|
|
4
4
|
import { axe } from 'vitest-axe'
|
|
5
5
|
import * as React from 'react'
|
|
6
|
+
import { Field, FieldDescription, FieldLabel } from '../field/field'
|
|
6
7
|
import { DatePicker, type DateRange } from './date-picker'
|
|
7
8
|
|
|
8
9
|
const JUN_10 = new Date(2026, 5, 10)
|
|
@@ -163,6 +164,52 @@ describe('DatePicker', () => {
|
|
|
163
164
|
expect(dayCells).toHaveLength(2)
|
|
164
165
|
})
|
|
165
166
|
|
|
167
|
+
it('labels the two fields Start date and End date by default', () => {
|
|
168
|
+
const { container } = render(<DatePicker mode="range" defaultValue={{ from: JUN_10, to: JUN_17 }} />)
|
|
169
|
+
const fields = container.querySelectorAll('[data-slot="date-field"]')
|
|
170
|
+
expect(screen.getByRole('group', { name: 'Start date' })).toBe(fields[0])
|
|
171
|
+
expect(screen.getByRole('group', { name: 'End date' })).toBe(fields[1])
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
it('uses startAriaLabel and endAriaLabel when passed', () => {
|
|
175
|
+
render(<DatePicker mode="range" startAriaLabel="Check-in" endAriaLabel="Check-out" />)
|
|
176
|
+
expect(screen.getByRole('group', { name: 'Check-in' })).toBeInTheDocument()
|
|
177
|
+
expect(screen.getByRole('group', { name: 'Check-out' })).toBeInTheDocument()
|
|
178
|
+
expect(screen.queryByRole('group', { name: 'Start date' })).toBeNull()
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
it('labels the time fields with the same start and end labels when showTime is set', () => {
|
|
182
|
+
const { container } = render(<DatePicker mode="range" showTime />)
|
|
183
|
+
const timeFields = container.querySelectorAll('[data-slot="time-field"]')
|
|
184
|
+
expect(timeFields[0]).toHaveAttribute('aria-label', 'Start date')
|
|
185
|
+
expect(timeFields[1]).toHaveAttribute('aria-label', 'End date')
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
it('makes the wrapper a group labeled by the FieldLabel', () => {
|
|
189
|
+
const { container } = render(
|
|
190
|
+
<Field>
|
|
191
|
+
<FieldLabel>Trip dates</FieldLabel>
|
|
192
|
+
<DatePicker mode="range" defaultValue={{ from: JUN_10, to: JUN_17 }} />
|
|
193
|
+
</Field>,
|
|
194
|
+
)
|
|
195
|
+
const wrapper = container.querySelector('[data-slot="date-picker"]')
|
|
196
|
+
expect(screen.getByRole('group', { name: 'Trip dates' })).toBe(wrapper)
|
|
197
|
+
expect(screen.getByRole('group', { name: 'Start date' })).toBeInTheDocument()
|
|
198
|
+
expect(screen.getByRole('group', { name: 'End date' })).toBeInTheDocument()
|
|
199
|
+
expect(screen.getByText('Trip dates')).not.toHaveAttribute('for')
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
it('has no accessibility violations inside a Field', async () => {
|
|
203
|
+
const { container } = render(
|
|
204
|
+
<Field>
|
|
205
|
+
<FieldLabel>Trip dates</FieldLabel>
|
|
206
|
+
<DatePicker mode="range" showTime defaultValue={{ from: JUN_10, to: JUN_17 }} />
|
|
207
|
+
<FieldDescription>Check-in and check-out.</FieldDescription>
|
|
208
|
+
</Field>,
|
|
209
|
+
)
|
|
210
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
211
|
+
})
|
|
212
|
+
|
|
166
213
|
it('does NOT auto-close the popover after both ends are picked', async () => {
|
|
167
214
|
const user = userEvent.setup()
|
|
168
215
|
render(<DatePicker mode="range" defaultMonth={JUN_10} />)
|
|
@@ -284,8 +331,31 @@ describe('DatePicker', () => {
|
|
|
284
331
|
expect(root).toHaveAttribute('data-disabled')
|
|
285
332
|
})
|
|
286
333
|
|
|
334
|
+
it('names the single-mode field from a surrounding FieldLabel', () => {
|
|
335
|
+
render(
|
|
336
|
+
<Field>
|
|
337
|
+
<FieldLabel>Departure</FieldLabel>
|
|
338
|
+
<DatePicker defaultValue={JUN_10} defaultMonth={JUN_10} />
|
|
339
|
+
<FieldDescription>Pick a weekday.</FieldDescription>
|
|
340
|
+
</Field>,
|
|
341
|
+
)
|
|
342
|
+
const group = screen.getByRole('group', { name: 'Departure' })
|
|
343
|
+
expect(group).toHaveAttribute('data-slot', 'date-field')
|
|
344
|
+
expect(group).toHaveAttribute('aria-describedby', screen.getByText('Pick a weekday.').id)
|
|
345
|
+
})
|
|
346
|
+
|
|
287
347
|
it('has no accessibility violations', async () => {
|
|
288
348
|
const { container } = render(<DatePicker defaultValue={JUN_10} defaultMonth={JUN_10} />)
|
|
289
349
|
expect(await axe(container)).toHaveNoViolations()
|
|
290
350
|
})
|
|
351
|
+
|
|
352
|
+
it('has no accessibility violations inside a Field', async () => {
|
|
353
|
+
const { container } = render(
|
|
354
|
+
<Field>
|
|
355
|
+
<FieldLabel>Departure</FieldLabel>
|
|
356
|
+
<DatePicker showTime defaultValue={JUN_10} defaultMonth={JUN_10} />
|
|
357
|
+
</Field>,
|
|
358
|
+
)
|
|
359
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
360
|
+
})
|
|
291
361
|
})
|
|
@@ -9,6 +9,7 @@ import { Input, inputVariants } from '../input'
|
|
|
9
9
|
import { Popover, PopoverContent, PopoverTrigger, type PopoverContentProps } from '../popover/popover'
|
|
10
10
|
import { buttonVariants } from '../button/button-variants'
|
|
11
11
|
import { useFieldRootContext } from '@base-ui/react/internals/field-root-context'
|
|
12
|
+
import { useLabelableContext } from '@base-ui/react/internals/labelable-provider'
|
|
12
13
|
import { cn } from '../../internal/utils'
|
|
13
14
|
|
|
14
15
|
type DatePickerSize = 'sm' | 'md' | 'lg'
|
|
@@ -144,6 +145,16 @@ type DatePickerBase = CalendarPassthrough & {
|
|
|
144
145
|
* @default 'Open calendar'
|
|
145
146
|
*/
|
|
146
147
|
triggerAriaLabel?: string
|
|
148
|
+
/**
|
|
149
|
+
* Accessible label for the start of a range (its date field, plus its time field with `showTime`).
|
|
150
|
+
* @default 'Start date'
|
|
151
|
+
*/
|
|
152
|
+
startAriaLabel?: string
|
|
153
|
+
/**
|
|
154
|
+
* Accessible label for the end of a range (its date field, plus its time field with `showTime`).
|
|
155
|
+
* @default 'End date'
|
|
156
|
+
*/
|
|
157
|
+
endAriaLabel?: string
|
|
147
158
|
}
|
|
148
159
|
|
|
149
160
|
type DatePickerProps =
|
|
@@ -226,6 +237,8 @@ function DatePicker(props: DatePickerProps) {
|
|
|
226
237
|
rangeSeparator = '–',
|
|
227
238
|
triggerIcon = DEFAULT_TRIGGER_ICON,
|
|
228
239
|
triggerAriaLabel = 'Open calendar',
|
|
240
|
+
startAriaLabel = 'Start date',
|
|
241
|
+
endAriaLabel = 'End date',
|
|
229
242
|
formatValue,
|
|
230
243
|
'aria-invalid': _ariaInvalid,
|
|
231
244
|
className,
|
|
@@ -241,6 +254,7 @@ function DatePicker(props: DatePickerProps) {
|
|
|
241
254
|
}
|
|
242
255
|
|
|
243
256
|
const field = useFieldRootContext(true)
|
|
257
|
+
const { labelId } = useLabelableContext()
|
|
244
258
|
const disabled = disabledProp || field.disabled
|
|
245
259
|
const name = nameProp ?? field.name
|
|
246
260
|
const invalid = props['aria-invalid'] === true || field.invalid === true
|
|
@@ -442,12 +456,15 @@ function DatePicker(props: DatePickerProps) {
|
|
|
442
456
|
startSlot={startSlot}
|
|
443
457
|
endSlot={endSlot}
|
|
444
458
|
trigger={triggerButton}
|
|
459
|
+
role="group"
|
|
460
|
+
ariaLabelledBy={labelId}
|
|
445
461
|
>
|
|
446
462
|
<DateField
|
|
447
463
|
unstyled
|
|
448
464
|
value={range?.from ?? null}
|
|
449
465
|
onValueChange={handleRangeFromDateChange}
|
|
450
466
|
format={dateFormat}
|
|
467
|
+
aria-label={startAriaLabel}
|
|
451
468
|
{...sharedFieldProps}
|
|
452
469
|
/>
|
|
453
470
|
{showTime && (
|
|
@@ -456,6 +473,7 @@ function DatePicker(props: DatePickerProps) {
|
|
|
456
473
|
value={range?.from ? format(range.from, 'HH:mm:ss') : null}
|
|
457
474
|
onValueChange={handleRangeFromTimeChange}
|
|
458
475
|
format={timeFormat}
|
|
476
|
+
aria-label={startAriaLabel}
|
|
459
477
|
{...sharedFieldProps}
|
|
460
478
|
/>
|
|
461
479
|
)}
|
|
@@ -467,6 +485,7 @@ function DatePicker(props: DatePickerProps) {
|
|
|
467
485
|
value={range?.to ?? null}
|
|
468
486
|
onValueChange={handleRangeToDateChange}
|
|
469
487
|
format={dateFormat}
|
|
488
|
+
aria-label={endAriaLabel}
|
|
470
489
|
{...sharedFieldProps}
|
|
471
490
|
/>
|
|
472
491
|
{showTime && (
|
|
@@ -475,6 +494,7 @@ function DatePicker(props: DatePickerProps) {
|
|
|
475
494
|
value={range?.to ? format(range.to, 'HH:mm:ss') : null}
|
|
476
495
|
onValueChange={handleRangeToTimeChange}
|
|
477
496
|
format={timeFormat}
|
|
497
|
+
aria-label={endAriaLabel}
|
|
478
498
|
{...sharedFieldProps}
|
|
479
499
|
/>
|
|
480
500
|
)}
|
|
@@ -560,6 +580,8 @@ interface UnifiedWrapperProps {
|
|
|
560
580
|
startSlot?: React.ReactNode
|
|
561
581
|
endSlot?: React.ReactNode
|
|
562
582
|
trigger: React.ReactNode
|
|
583
|
+
role?: 'group'
|
|
584
|
+
ariaLabelledBy?: string
|
|
563
585
|
children: React.ReactNode
|
|
564
586
|
}
|
|
565
587
|
|
|
@@ -572,6 +594,8 @@ function UnifiedWrapper({
|
|
|
572
594
|
startSlot,
|
|
573
595
|
endSlot,
|
|
574
596
|
trigger,
|
|
597
|
+
role,
|
|
598
|
+
ariaLabelledBy,
|
|
575
599
|
children,
|
|
576
600
|
}: UnifiedWrapperProps) {
|
|
577
601
|
return (
|
|
@@ -579,6 +603,8 @@ function UnifiedWrapper({
|
|
|
579
603
|
data-slot="date-picker"
|
|
580
604
|
data-disabled={disabled || undefined}
|
|
581
605
|
data-invalid={ariaInvalid || undefined}
|
|
606
|
+
role={role}
|
|
607
|
+
aria-labelledby={ariaLabelledBy}
|
|
582
608
|
className={cn(
|
|
583
609
|
inputVariants({ variant, size, state: 'within' }),
|
|
584
610
|
'flex w-full',
|
|
@@ -150,7 +150,8 @@ describe('DropdownMenu', () => {
|
|
|
150
150
|
const user = setupUser()
|
|
151
151
|
render(<BasicMenu />)
|
|
152
152
|
await user.click(screen.getByRole('button', { name: 'Open' }))
|
|
153
|
-
await screen.findByRole('
|
|
153
|
+
const menu = await screen.findByRole('menu')
|
|
154
|
+
await waitFor(() => expect(menu.contains(document.activeElement)).toBe(true))
|
|
154
155
|
await user.keyboard('{ArrowDown}')
|
|
155
156
|
await waitFor(() => {
|
|
156
157
|
const profile = screen.getByRole('menuitem', { name: 'Profile' })
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { render, screen } from '@testing-library/react'
|
|
2
|
+
import userEvent from '@testing-library/user-event'
|
|
2
3
|
import { describe, it, expect } from 'vitest'
|
|
3
4
|
import { axe } from 'vitest-axe'
|
|
4
5
|
import { Field as BaseField } from '@base-ui/react/field'
|
|
@@ -42,6 +43,27 @@ describe('Field', () => {
|
|
|
42
43
|
expect(screen.getByText('Email is required')).toBeInTheDocument()
|
|
43
44
|
})
|
|
44
45
|
|
|
46
|
+
it('wraps FieldError in a polite live region so a new error is announced', async () => {
|
|
47
|
+
const user = userEvent.setup()
|
|
48
|
+
const { container } = render(
|
|
49
|
+
<Field validationMode="onBlur">
|
|
50
|
+
<FieldLabel>Email</FieldLabel>
|
|
51
|
+
<BaseField.Control type="email" required defaultValue="" />
|
|
52
|
+
<FieldError match="valueMissing">Email is required</FieldError>
|
|
53
|
+
</Field>,
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
const region = container.querySelector('[aria-live="polite"]')
|
|
57
|
+
expect(region).toBeInTheDocument()
|
|
58
|
+
expect(screen.queryByText('Email is required')).not.toBeInTheDocument()
|
|
59
|
+
|
|
60
|
+
await user.type(screen.getByLabelText('Email'), 'a')
|
|
61
|
+
await user.clear(screen.getByLabelText('Email'))
|
|
62
|
+
await user.tab()
|
|
63
|
+
|
|
64
|
+
expect(region).toContainElement(await screen.findByText('Email is required'))
|
|
65
|
+
})
|
|
66
|
+
|
|
45
67
|
it('exposes validity state via FieldValidity render prop', () => {
|
|
46
68
|
render(
|
|
47
69
|
<Field invalid>
|
|
@@ -81,4 +103,16 @@ describe('Field', () => {
|
|
|
81
103
|
)
|
|
82
104
|
expect(await axe(container)).toHaveNoViolations()
|
|
83
105
|
})
|
|
106
|
+
|
|
107
|
+
it('has no accessibility violations with an error shown', async () => {
|
|
108
|
+
const { container } = render(
|
|
109
|
+
<Field invalid>
|
|
110
|
+
<FieldLabel>Email</FieldLabel>
|
|
111
|
+
<BaseField.Control type="email" defaultValue="" />
|
|
112
|
+
<FieldDescription>We never share your email.</FieldDescription>
|
|
113
|
+
<FieldError match>Email is required</FieldError>
|
|
114
|
+
</Field>,
|
|
115
|
+
)
|
|
116
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
117
|
+
})
|
|
84
118
|
})
|