@appica/ui-react 1.3.0 → 1.3.1
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 +24 -0
- package/agent-rules.md +2 -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 +2 -2
- package/dist/components/carousel/carousel.d.ts.map +1 -1
- package/dist/components/carousel/carousel.js +151 -32
- 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/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 +512 -12
- package/src/components/carousel/carousel.tsx +171 -33
- 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
|
@@ -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
|
})
|
|
@@ -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
|
})
|