@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
|
@@ -41,6 +41,7 @@ type FieldErrorProps = React.ComponentProps<typeof BaseField.Error>
|
|
|
41
41
|
function FieldError({ className, ...props }: FieldErrorProps) {
|
|
42
42
|
return (
|
|
43
43
|
<div
|
|
44
|
+
aria-live="polite"
|
|
44
45
|
className={cn(
|
|
45
46
|
'grid grid-rows-[0fr]',
|
|
46
47
|
'has-[[data-slot=field-error]:not([data-starting-style]):not([data-ending-style])]:grid-rows-[1fr]',
|
|
@@ -4,14 +4,27 @@ import { axe } from 'vitest-axe'
|
|
|
4
4
|
import { Loader } from './loader'
|
|
5
5
|
|
|
6
6
|
describe('Loader', () => {
|
|
7
|
-
it('exposes a status region
|
|
7
|
+
it('exposes a status region that reads "Loading" as text by default', () => {
|
|
8
8
|
render(<Loader />)
|
|
9
|
-
|
|
9
|
+
const status = screen.getByRole('status')
|
|
10
|
+
expect(status).toHaveTextContent('Loading')
|
|
11
|
+
expect(status).not.toHaveAttribute('aria-label')
|
|
10
12
|
})
|
|
11
13
|
|
|
12
|
-
it('
|
|
14
|
+
it('renders a custom aria-label as the status text', () => {
|
|
13
15
|
render(<Loader aria-label="Uploading attachment" />)
|
|
14
|
-
|
|
16
|
+
const status = screen.getByRole('status')
|
|
17
|
+
expect(status).toHaveTextContent('Uploading attachment')
|
|
18
|
+
expect(status).not.toHaveAttribute('aria-label')
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('contributes its label to the name of a surrounding button', () => {
|
|
22
|
+
render(
|
|
23
|
+
<button type="button">
|
|
24
|
+
<Loader aria-label="Saving" currentColor />
|
|
25
|
+
</button>,
|
|
26
|
+
)
|
|
27
|
+
expect(screen.getByRole('button', { name: 'Saving' })).toBeInTheDocument()
|
|
15
28
|
})
|
|
16
29
|
|
|
17
30
|
it('renders the status region as a <span> so it nests inside <p> without invalid HTML', () => {
|
|
@@ -138,7 +138,7 @@ const VARIANT_COMPONENTS: Record<LoaderVariant, React.ComponentType<VariantInner
|
|
|
138
138
|
function Loader({
|
|
139
139
|
variant = 'bar',
|
|
140
140
|
currentColor = false,
|
|
141
|
-
'aria-label':
|
|
141
|
+
'aria-label': label = 'Loading',
|
|
142
142
|
className,
|
|
143
143
|
...props
|
|
144
144
|
}: LoaderProps) {
|
|
@@ -152,7 +152,6 @@ function Loader({
|
|
|
152
152
|
<span
|
|
153
153
|
data-slot="loader"
|
|
154
154
|
role="status"
|
|
155
|
-
aria-label={ariaLabel}
|
|
156
155
|
className={cn('inline-flex shrink-0 items-center justify-center align-middle', LOADER_SIZE, className)}
|
|
157
156
|
{...props}
|
|
158
157
|
>
|
|
@@ -163,6 +162,7 @@ function Loader({
|
|
|
163
162
|
{inner}
|
|
164
163
|
</LazyMotion>
|
|
165
164
|
)}
|
|
165
|
+
<span className="sr-only">{label}</span>
|
|
166
166
|
</span>
|
|
167
167
|
)
|
|
168
168
|
}
|
|
@@ -3,6 +3,7 @@ import { render, screen, waitFor } from '@testing-library/react'
|
|
|
3
3
|
import userEvent from '@testing-library/user-event'
|
|
4
4
|
import { describe, it, expect, vi } from 'vitest'
|
|
5
5
|
import { axe } from 'vitest-axe'
|
|
6
|
+
import { Field, FieldLabel } from '../field/field'
|
|
6
7
|
import { NumberField } from './number-field'
|
|
7
8
|
|
|
8
9
|
function renderWithLabel(props: React.ComponentProps<typeof NumberField> = {}, labelText = 'Qty', id = 'qty') {
|
|
@@ -221,4 +222,62 @@ describe('NumberField', () => {
|
|
|
221
222
|
const { container } = renderWithLabel({}, 'Quantity', 'qty-a11y')
|
|
222
223
|
expect(await axe(container)).toHaveNoViolations()
|
|
223
224
|
})
|
|
225
|
+
|
|
226
|
+
describe('accessible name', () => {
|
|
227
|
+
function getRoot(container: HTMLElement) {
|
|
228
|
+
return container.querySelector('[data-slot="number-field"]')
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
it('names the input from a root aria-label, not the root', () => {
|
|
232
|
+
const { container } = render(<NumberField defaultValue={1} aria-label="Quantity" />)
|
|
233
|
+
expect(screen.getByRole('textbox', { name: 'Quantity' })).toHaveAttribute('data-slot', 'number-field-input')
|
|
234
|
+
expect(getRoot(container)).not.toHaveAttribute('aria-label')
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
it('names the input from a root aria-labelledby, not the root', () => {
|
|
238
|
+
const { container } = render(
|
|
239
|
+
<>
|
|
240
|
+
<span id="qty-label">Quantity</span>
|
|
241
|
+
<NumberField defaultValue={1} aria-labelledby="qty-label" />
|
|
242
|
+
</>,
|
|
243
|
+
)
|
|
244
|
+
expect(screen.getByRole('textbox', { name: 'Quantity' })).toHaveAttribute('aria-labelledby', 'qty-label')
|
|
245
|
+
expect(getRoot(container)).not.toHaveAttribute('aria-labelledby')
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
it('keeps the input label from inputProps over the root label', () => {
|
|
249
|
+
const { container } = render(
|
|
250
|
+
<NumberField defaultValue={1} aria-label="Root label" inputProps={{ 'aria-label': 'Seats' }} />,
|
|
251
|
+
)
|
|
252
|
+
expect(screen.getByRole('textbox', { name: 'Seats' })).toBeInTheDocument()
|
|
253
|
+
expect(getRoot(container)).not.toHaveAttribute('aria-label')
|
|
254
|
+
})
|
|
255
|
+
|
|
256
|
+
it('keeps the FieldLabel over the root label inside a Field', () => {
|
|
257
|
+
const { container } = render(
|
|
258
|
+
<Field>
|
|
259
|
+
<FieldLabel>Seats</FieldLabel>
|
|
260
|
+
<NumberField defaultValue={1} aria-label="Root label" />
|
|
261
|
+
</Field>,
|
|
262
|
+
)
|
|
263
|
+
const input = screen.getByRole('textbox', { name: 'Seats' })
|
|
264
|
+
expect(input).not.toHaveAttribute('aria-label')
|
|
265
|
+
expect(getRoot(container)).not.toHaveAttribute('aria-label')
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
it('has no accessibility violations when labeled by a root aria-label', async () => {
|
|
269
|
+
const { container } = render(<NumberField defaultValue={1} aria-label="Quantity" />)
|
|
270
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
it('has no accessibility violations inside a Field', async () => {
|
|
274
|
+
const { container } = render(
|
|
275
|
+
<Field>
|
|
276
|
+
<FieldLabel>Seats</FieldLabel>
|
|
277
|
+
<NumberField defaultValue={1} />
|
|
278
|
+
</Field>,
|
|
279
|
+
)
|
|
280
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
281
|
+
})
|
|
282
|
+
})
|
|
224
283
|
})
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import * as React from 'react'
|
|
4
4
|
import { NumberField as BaseNumberField, type NumberFieldRoot } from '@base-ui/react/number-field'
|
|
5
|
+
import { useLabelableContext } from '@base-ui/react/internals/labelable-provider'
|
|
5
6
|
import { type VariantProps } from 'class-variance-authority'
|
|
6
7
|
import { AnimatePresence, LazyMotion, domAnimation, m } from 'motion/react'
|
|
7
8
|
import { cn } from '../../internal/utils'
|
|
@@ -118,6 +119,8 @@ function NumberField({
|
|
|
118
119
|
min,
|
|
119
120
|
placeholder = ' ',
|
|
120
121
|
inputProps,
|
|
122
|
+
'aria-label': ariaLabel,
|
|
123
|
+
'aria-labelledby': ariaLabelledBy,
|
|
121
124
|
...rootProps
|
|
122
125
|
}: NumberFieldProps) {
|
|
123
126
|
const resolvedVariant: NumberFieldVariant = variant
|
|
@@ -129,6 +132,10 @@ function NumberField({
|
|
|
129
132
|
const ariaInvalid = rootProps['aria-invalid']
|
|
130
133
|
const invalid = ariaInvalid === true || ariaInvalid === 'true'
|
|
131
134
|
|
|
135
|
+
const { labelId } = useLabelableContext()
|
|
136
|
+
const inputHasName = inputProps?.['aria-label'] != null || inputProps?.['aria-labelledby'] != null
|
|
137
|
+
const forwardedName = inputHasName || labelId ? {} : { 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy }
|
|
138
|
+
|
|
132
139
|
const reduced = useReducedMotion()
|
|
133
140
|
|
|
134
141
|
const formatter = React.useMemo(() => {
|
|
@@ -238,6 +245,7 @@ function NumberField({
|
|
|
238
245
|
<BaseNumberField.Input
|
|
239
246
|
data-slot="number-field-input"
|
|
240
247
|
placeholder={placeholder}
|
|
248
|
+
{...forwardedName}
|
|
241
249
|
{...inputProps}
|
|
242
250
|
onPointerDown={(e) => {
|
|
243
251
|
inputProps?.onPointerDown?.(e)
|
|
@@ -45,6 +45,11 @@ describe('Pagination', () => {
|
|
|
45
45
|
expect(nav).toHaveAttribute('data-slot', 'pagination')
|
|
46
46
|
})
|
|
47
47
|
|
|
48
|
+
it('relies on the implicit navigation role of <nav>', () => {
|
|
49
|
+
renderBasic()
|
|
50
|
+
expect(screen.getByRole('navigation', { name: 'pagination' })).not.toHaveAttribute('role')
|
|
51
|
+
})
|
|
52
|
+
|
|
48
53
|
it('renders content as a ul and items as li', () => {
|
|
49
54
|
renderBasic()
|
|
50
55
|
const list = screen.getByRole('list')
|
|
@@ -61,6 +66,47 @@ describe('Pagination', () => {
|
|
|
61
66
|
expect(active).toHaveAttribute('data-active')
|
|
62
67
|
})
|
|
63
68
|
|
|
69
|
+
it('keeps a current page rendered as a <span> out of the tab order', async () => {
|
|
70
|
+
const user = userEvent.setup()
|
|
71
|
+
render(
|
|
72
|
+
<Pagination>
|
|
73
|
+
<PaginationList>
|
|
74
|
+
<PaginationItem>
|
|
75
|
+
<PaginationLink href="#1">1</PaginationLink>
|
|
76
|
+
</PaginationItem>
|
|
77
|
+
<PaginationItem>
|
|
78
|
+
<PaginationLink active render={<span />}>
|
|
79
|
+
2
|
|
80
|
+
</PaginationLink>
|
|
81
|
+
</PaginationItem>
|
|
82
|
+
<PaginationItem>
|
|
83
|
+
<PaginationLink href="#3">3</PaginationLink>
|
|
84
|
+
</PaginationItem>
|
|
85
|
+
</PaginationList>
|
|
86
|
+
</Pagination>,
|
|
87
|
+
)
|
|
88
|
+
const current = screen.getByText('2')
|
|
89
|
+
expect(current.tagName).toBe('SPAN')
|
|
90
|
+
expect(current).toHaveAttribute('aria-current', 'page')
|
|
91
|
+
expect(current).not.toHaveAttribute('tabindex')
|
|
92
|
+
|
|
93
|
+
await user.tab()
|
|
94
|
+
expect(screen.getByRole('link', { name: '1' })).toHaveFocus()
|
|
95
|
+
await user.tab()
|
|
96
|
+
expect(screen.getByRole('link', { name: '3' })).toHaveFocus()
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('keeps a current page rendered as a link reachable', async () => {
|
|
100
|
+
const user = userEvent.setup()
|
|
101
|
+
renderBasic()
|
|
102
|
+
const current = screen.getByRole('link', { current: 'page' })
|
|
103
|
+
expect(current).not.toHaveAttribute('aria-disabled')
|
|
104
|
+
await user.tab()
|
|
105
|
+
await user.tab()
|
|
106
|
+
await user.tab()
|
|
107
|
+
expect(current).toHaveFocus()
|
|
108
|
+
})
|
|
109
|
+
|
|
64
110
|
it('marks disabled links with aria-disabled and data-disabled', () => {
|
|
65
111
|
render(
|
|
66
112
|
<Pagination>
|
|
@@ -76,6 +122,20 @@ describe('Pagination', () => {
|
|
|
76
122
|
const link = screen.getByRole('link', { name: 'Previous' })
|
|
77
123
|
expect(link).toHaveAttribute('aria-disabled', 'true')
|
|
78
124
|
expect(link).toHaveAttribute('data-disabled')
|
|
125
|
+
expect(link).toHaveAttribute('tabindex', '-1')
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
it('keeps a non-active link without an href focusable', () => {
|
|
129
|
+
render(
|
|
130
|
+
<Pagination>
|
|
131
|
+
<PaginationList>
|
|
132
|
+
<PaginationItem>
|
|
133
|
+
<PaginationLink onClick={() => {}}>4</PaginationLink>
|
|
134
|
+
</PaginationItem>
|
|
135
|
+
</PaginationList>
|
|
136
|
+
</Pagination>,
|
|
137
|
+
)
|
|
138
|
+
expect(screen.getByText('4')).toHaveAttribute('tabindex', '0')
|
|
79
139
|
})
|
|
80
140
|
|
|
81
141
|
it('forwards className alongside variant classes on the link', () => {
|
|
@@ -144,4 +204,27 @@ describe('Pagination', () => {
|
|
|
144
204
|
const { container } = renderBasic()
|
|
145
205
|
expect(await axe(container)).toHaveNoViolations()
|
|
146
206
|
})
|
|
207
|
+
|
|
208
|
+
it('has no accessibility violations with a <span> current page and disabled controls', async () => {
|
|
209
|
+
const { container } = render(
|
|
210
|
+
<Pagination>
|
|
211
|
+
<PaginationList>
|
|
212
|
+
<PaginationItem>
|
|
213
|
+
<PaginationLink disabled render={<span />}>
|
|
214
|
+
Prev
|
|
215
|
+
</PaginationLink>
|
|
216
|
+
</PaginationItem>
|
|
217
|
+
<PaginationItem>
|
|
218
|
+
<PaginationLink active render={<span />}>
|
|
219
|
+
1
|
|
220
|
+
</PaginationLink>
|
|
221
|
+
</PaginationItem>
|
|
222
|
+
<PaginationItem>
|
|
223
|
+
<PaginationLink href="#2">2</PaginationLink>
|
|
224
|
+
</PaginationItem>
|
|
225
|
+
</PaginationList>
|
|
226
|
+
</Pagination>,
|
|
227
|
+
)
|
|
228
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
229
|
+
})
|
|
147
230
|
})
|
|
@@ -43,7 +43,6 @@ function Pagination({ className, variant = 'outline', size = 'md', ...props }: P
|
|
|
43
43
|
return (
|
|
44
44
|
<PaginationContext.Provider value={ctx}>
|
|
45
45
|
<nav
|
|
46
|
-
role="navigation"
|
|
47
46
|
aria-label="pagination"
|
|
48
47
|
data-slot="pagination"
|
|
49
48
|
className={cn('data-disabled:opacity-disabled flex w-fit', className)}
|
|
@@ -103,7 +102,7 @@ function PaginationLink({ className, active = false, disabled = false, render, .
|
|
|
103
102
|
'data-slot': 'pagination-link',
|
|
104
103
|
'data-active': active || undefined,
|
|
105
104
|
'aria-current': active ? 'page' : undefined,
|
|
106
|
-
...focusableProps(disabled),
|
|
105
|
+
...(active && !disabled ? {} : focusableProps(disabled)),
|
|
107
106
|
className: cn(
|
|
108
107
|
buttonVariants({ variant, size }),
|
|
109
108
|
LINK_SIZE_OVERRIDES[size],
|
|
@@ -4,14 +4,27 @@ import { axe } from 'vitest-axe'
|
|
|
4
4
|
import { Spinner } from './spinner'
|
|
5
5
|
|
|
6
6
|
describe('Spinner', () => {
|
|
7
|
-
it('exposes a status region
|
|
7
|
+
it('exposes a status region that reads "Loading" as text by default', () => {
|
|
8
8
|
render(<Spinner />)
|
|
9
|
-
|
|
9
|
+
const status = screen.getByRole('status')
|
|
10
|
+
expect(status).toHaveTextContent('Loading')
|
|
11
|
+
expect(status).not.toHaveAttribute('aria-label')
|
|
10
12
|
})
|
|
11
13
|
|
|
12
|
-
it('
|
|
14
|
+
it('renders a custom aria-label as the status text', () => {
|
|
13
15
|
render(<Spinner aria-label="Fetching results" />)
|
|
14
|
-
|
|
16
|
+
const status = screen.getByRole('status')
|
|
17
|
+
expect(status).toHaveTextContent('Fetching results')
|
|
18
|
+
expect(status).not.toHaveAttribute('aria-label')
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('contributes its label to the name of a surrounding button', () => {
|
|
22
|
+
render(
|
|
23
|
+
<button type="button">
|
|
24
|
+
<Spinner aria-label="Saving" currentColor />
|
|
25
|
+
</button>,
|
|
26
|
+
)
|
|
27
|
+
expect(screen.getByRole('button', { name: 'Saving' })).toBeInTheDocument()
|
|
15
28
|
})
|
|
16
29
|
|
|
17
30
|
it('renders the status region as a <span> so it nests inside <p> without invalid HTML', () => {
|
|
@@ -194,7 +194,7 @@ const VARIANT_COMPONENTS: Record<SpinnerVariant, React.ComponentType<VariantInne
|
|
|
194
194
|
function Spinner({
|
|
195
195
|
variant = 'circular',
|
|
196
196
|
currentColor = false,
|
|
197
|
-
'aria-label':
|
|
197
|
+
'aria-label': label = 'Loading',
|
|
198
198
|
className,
|
|
199
199
|
...props
|
|
200
200
|
}: SpinnerProps) {
|
|
@@ -208,7 +208,6 @@ function Spinner({
|
|
|
208
208
|
<span
|
|
209
209
|
data-slot="spinner"
|
|
210
210
|
role="status"
|
|
211
|
-
aria-label={ariaLabel}
|
|
212
211
|
className={cn('inline-flex shrink-0 items-center justify-center align-middle', SPINNER_SIZE, className)}
|
|
213
212
|
{...props}
|
|
214
213
|
>
|
|
@@ -219,6 +218,7 @@ function Spinner({
|
|
|
219
218
|
{inner}
|
|
220
219
|
</LazyMotion>
|
|
221
220
|
)}
|
|
221
|
+
<span className="sr-only">{label}</span>
|
|
222
222
|
</span>
|
|
223
223
|
)
|
|
224
224
|
}
|
|
@@ -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 { TimeField } from './time-field'
|
|
6
8
|
|
|
7
9
|
function getSegments(name: 'hour' | 'minute' | 'second' | 'period') {
|
|
@@ -346,6 +348,68 @@ describe('TimeField', () => {
|
|
|
346
348
|
expect(root).toHaveAttribute('data-disabled')
|
|
347
349
|
})
|
|
348
350
|
|
|
351
|
+
describe('inside a Field', () => {
|
|
352
|
+
function renderInField(props: React.ComponentProps<typeof TimeField> = {}, invalid = false) {
|
|
353
|
+
return render(
|
|
354
|
+
<Field invalid={invalid}>
|
|
355
|
+
<FieldLabel>Start time</FieldLabel>
|
|
356
|
+
<TimeField defaultValue="09:30" {...props} />
|
|
357
|
+
<FieldDescription>Local time.</FieldDescription>
|
|
358
|
+
<FieldError match>Enter a valid time.</FieldError>
|
|
359
|
+
</Field>,
|
|
360
|
+
)
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
it('takes its accessible name from the FieldLabel', () => {
|
|
364
|
+
renderInField()
|
|
365
|
+
const group = screen.getByRole('group', { name: 'Start time' })
|
|
366
|
+
expect(group).toHaveAttribute('data-slot', 'time-field')
|
|
367
|
+
expect(screen.getByText('Start time')).not.toHaveAttribute('for')
|
|
368
|
+
})
|
|
369
|
+
|
|
370
|
+
it('is described by the FieldDescription', () => {
|
|
371
|
+
renderInField()
|
|
372
|
+
const description = screen.getByText('Local time.')
|
|
373
|
+
expect(screen.getByRole('group', { name: 'Start time' })).toHaveAttribute(
|
|
374
|
+
'aria-describedby',
|
|
375
|
+
expect.stringContaining(description.id),
|
|
376
|
+
)
|
|
377
|
+
})
|
|
378
|
+
|
|
379
|
+
it('is described by the FieldError and marks every segment invalid', () => {
|
|
380
|
+
renderInField({ format: 'hh:mm a' }, true)
|
|
381
|
+
const group = screen.getByRole('group', { name: 'Start time' })
|
|
382
|
+
const error = screen.getByText('Enter a valid time.')
|
|
383
|
+
expect(group).toHaveAttribute('aria-describedby', expect.stringContaining(error.id))
|
|
384
|
+
expect(group).toHaveAttribute('aria-invalid', 'true')
|
|
385
|
+
const segments = screen.getAllByRole('spinbutton')
|
|
386
|
+
expect(segments).toHaveLength(3)
|
|
387
|
+
for (const segment of segments) {
|
|
388
|
+
expect(segment).toHaveAttribute('aria-invalid', 'true')
|
|
389
|
+
}
|
|
390
|
+
})
|
|
391
|
+
|
|
392
|
+
it('lets consumer aria-label and aria-describedby win over the Field wiring', () => {
|
|
393
|
+
renderInField({ 'aria-label': 'Departure', 'aria-describedby': 'custom-hint' })
|
|
394
|
+
const group = screen.getByRole('group', { name: 'Departure' })
|
|
395
|
+
expect(group).not.toHaveAttribute('aria-labelledby')
|
|
396
|
+
expect(group).toHaveAttribute('aria-describedby', 'custom-hint')
|
|
397
|
+
})
|
|
398
|
+
|
|
399
|
+
it('lets a consumer aria-invalid win on the group and segments', () => {
|
|
400
|
+
renderInField({ 'aria-invalid': false }, true)
|
|
401
|
+
expect(screen.getByRole('group', { name: 'Start time' })).toHaveAttribute('aria-invalid', 'false')
|
|
402
|
+
for (const segment of screen.getAllByRole('spinbutton')) {
|
|
403
|
+
expect(segment).toHaveAttribute('aria-invalid', 'false')
|
|
404
|
+
}
|
|
405
|
+
})
|
|
406
|
+
|
|
407
|
+
it('has no accessibility violations when labeled, described and invalid', async () => {
|
|
408
|
+
const { container } = renderInField({}, true)
|
|
409
|
+
expect(await axe(container)).toHaveNoViolations()
|
|
410
|
+
})
|
|
411
|
+
})
|
|
412
|
+
|
|
349
413
|
it('renders startSlot and endSlot containers', () => {
|
|
350
414
|
const { container } = render(
|
|
351
415
|
<TimeField
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import * as React from 'react'
|
|
4
4
|
import { type VariantProps } from 'class-variance-authority'
|
|
5
5
|
import { useFieldRootContext } from '@base-ui/react/internals/field-root-context'
|
|
6
|
+
import { useLabelableContext, useLabelableId } from '@base-ui/react/internals/labelable-provider'
|
|
6
7
|
import { cn } from '../../internal/utils'
|
|
7
8
|
import { inputVariants } from '../input/input-variants'
|
|
8
9
|
|
|
@@ -256,6 +257,9 @@ function TimeField({
|
|
|
256
257
|
const name = nameProp ?? field.name
|
|
257
258
|
const ariaInvalid = rest['aria-invalid']
|
|
258
259
|
const invalid = ariaInvalid === true || ariaInvalid === 'true' || field.invalid === true
|
|
260
|
+
const resolvedAriaInvalid = ariaInvalid ?? (invalid || undefined)
|
|
261
|
+
const { labelId, getDescriptionProps } = useLabelableContext()
|
|
262
|
+
useLabelableId({ id: null })
|
|
259
263
|
|
|
260
264
|
const nodes = React.useMemo(() => parseFormat(format), [format])
|
|
261
265
|
const segmentCount = React.useMemo(() => nodes.reduce((n, x) => n + (x.kind === 'segment' ? 1 : 0), 0), [nodes])
|
|
@@ -563,6 +567,8 @@ function TimeField({
|
|
|
563
567
|
data-invalid={invalid || undefined}
|
|
564
568
|
aria-invalid={invalid || undefined}
|
|
565
569
|
aria-disabled={disabled || undefined}
|
|
570
|
+
aria-labelledby={rest['aria-label'] == null ? labelId : undefined}
|
|
571
|
+
aria-describedby={getDescriptionProps({})['aria-describedby']}
|
|
566
572
|
role="group"
|
|
567
573
|
ref={ref}
|
|
568
574
|
className={cn(
|
|
@@ -625,6 +631,7 @@ function TimeField({
|
|
|
625
631
|
spellCheck={false}
|
|
626
632
|
aria-label={ariaLabel(node.type)}
|
|
627
633
|
aria-readonly={readOnly || undefined}
|
|
634
|
+
aria-invalid={resolvedAriaInvalid}
|
|
628
635
|
aria-valuemin={isPeriod ? 0 : range.min}
|
|
629
636
|
aria-valuemax={isPeriod ? 1 : range.max}
|
|
630
637
|
aria-valuenow={isPeriod ? periodAriaNow : (partVal ?? undefined)}
|