@object-ui/fields 17.1.0 → 17.3.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 +786 -0
- package/dist/FieldEditWidget.d.ts +2 -2
- package/dist/__tests__/cell-truncation.test.d.ts +0 -0
- package/dist/__tests__/field-carrier-sdui.test.d.ts +0 -0
- package/dist/__tests__/spec-symbol-batch7.test.d.ts +8 -0
- package/dist/__tests__/widget-aria-invalid-e2e.test.d.ts +0 -0
- package/dist/__tests__/widget-aria-invalid-registry-e2e.test.d.ts +0 -0
- package/dist/__tests__/widget-dom-leak-e2e.test.d.ts +0 -0
- package/dist/__tests__/widget-props-contract.test.d.ts +8 -0
- package/dist/index.cjs +5 -5
- package/dist/index.d.ts +4 -0
- package/dist/index.js +2882 -2569
- package/dist/widgets/AddressField.d.ts +2 -2
- package/dist/widgets/AddressField.uniqueIds.test.d.ts +0 -0
- package/dist/widgets/AutoNumberField.d.ts +2 -2
- package/dist/widgets/AvatarField.d.ts +2 -2
- package/dist/widgets/BooleanField.d.ts +2 -2
- package/dist/widgets/CapabilityMultiSelectField.d.ts +2 -2
- package/dist/widgets/CharacterCount.d.ts +56 -0
- package/dist/widgets/CheckboxesField.d.ts +2 -2
- package/dist/widgets/CodeField.d.ts +2 -2
- package/dist/widgets/ColorField.d.ts +2 -2
- package/dist/widgets/CurrencyField.d.ts +2 -2
- package/dist/widgets/DateField.d.ts +2 -2
- package/dist/widgets/DateTimeField.d.ts +2 -2
- package/dist/widgets/EmailField.d.ts +2 -2
- package/dist/widgets/FileField.d.ts +2 -2
- package/dist/widgets/FilterConditionField.d.ts +2 -2
- package/dist/widgets/FormulaField.d.ts +2 -2
- package/dist/widgets/FullscreenFieldEditor.d.ts +155 -0
- package/dist/widgets/GeolocationField.d.ts +2 -2
- package/dist/widgets/GeolocationField.uniqueIds.test.d.ts +0 -0
- package/dist/widgets/GridField.d.ts +2 -2
- package/dist/widgets/ImageField.d.ts +2 -2
- package/dist/widgets/LocationField.d.ts +2 -2
- package/dist/widgets/LookupField.d.ts +2 -2
- package/dist/widgets/LookupField.gateHintLabel.test.d.ts +8 -0
- package/dist/widgets/MasterDetailField.d.ts +2 -2
- package/dist/widgets/MultiSelectField.d.ts +2 -2
- package/dist/widgets/NumberField.d.ts +2 -2
- package/dist/widgets/ObjectField.d.ts +2 -2
- package/dist/widgets/ObjectRefField.d.ts +2 -2
- package/dist/widgets/OptionsEmptyState.d.ts +45 -0
- package/dist/widgets/OptionsEmptyState.no-provider.test.d.ts +0 -0
- package/dist/widgets/OptionsEmptyState.test.d.ts +0 -0
- package/dist/widgets/PasswordField.d.ts +2 -2
- package/dist/widgets/PercentField.d.ts +2 -2
- package/dist/widgets/PhoneField.d.ts +2 -2
- package/dist/widgets/QRCodeField.d.ts +2 -2
- package/dist/widgets/RadioField.d.ts +2 -2
- package/dist/widgets/RatingField.d.ts +2 -2
- package/dist/widgets/RecipientPickerField.d.ts +2 -2
- package/dist/widgets/RecordPickerDialog.d.ts +17 -1
- package/dist/widgets/RecordPickerDialog.filterOptions.test.d.ts +8 -0
- package/dist/widgets/RecordPickerDialog.selectLabel.test.d.ts +8 -0
- package/dist/widgets/RichTextField.d.ts +20 -2
- package/dist/widgets/SelectField.d.ts +2 -2
- package/dist/widgets/SignatureField.d.ts +2 -2
- package/dist/widgets/SliderField.d.ts +2 -2
- package/dist/widgets/SummaryField.d.ts +2 -2
- package/dist/widgets/TagsField.d.ts +2 -2
- package/dist/widgets/TagsField.placeholder.no-provider.test.d.ts +0 -0
- package/dist/widgets/TagsField.placeholder.test.d.ts +0 -0
- package/dist/widgets/TextAreaField.d.ts +45 -6
- package/dist/widgets/TextField.d.ts +2 -2
- package/dist/widgets/TimeField.d.ts +2 -2
- package/dist/widgets/UrlField.d.ts +2 -2
- package/dist/widgets/UserField.d.ts +2 -2
- package/dist/widgets/VectorField.d.ts +2 -2
- package/dist/widgets/__tests__/FullscreenFieldEditor.i18n.test.d.ts +0 -0
- package/dist/widgets/__tests__/FullscreenFieldEditor.no-provider.test.d.ts +0 -0
- package/dist/widgets/__tests__/RichTextField.fullscreenDisabled.test.d.ts +0 -0
- package/dist/widgets/__tests__/RichTextField.mobileFullscreen.test.d.ts +0 -0
- package/dist/widgets/__tests__/TextAreaField.characterCount.announcements.test.d.ts +0 -0
- package/dist/widgets/__tests__/TextAreaField.characterCount.i18n.test.d.ts +0 -0
- package/dist/widgets/__tests__/TextAreaField.characterCount.no-provider.test.d.ts +0 -0
- package/dist/widgets/__tests__/TextAreaField.fullscreenCharacterCount.i18n.test.d.ts +0 -0
- package/dist/widgets/__tests__/TextAreaField.fullscreenCharacterCount.test.d.ts +0 -0
- package/dist/widgets/__tests__/TextAreaField.fullscreenDisabled.test.d.ts +0 -0
- package/dist/widgets/__tests__/TextAreaField.mobileFullscreen.test.d.ts +0 -0
- package/dist/widgets/file-value.d.ts +20 -33
- package/dist/widgets/toDomProps.d.ts +91 -0
- package/dist/widgets/types.d.ts +215 -4
- package/dist/withFieldCarrier.d.ts +56 -0
- package/package.json +13 -13
- package/src/FieldEditWidget.tsx +6 -4
- package/src/__tests__/cell-truncation.test.tsx +96 -0
- package/src/__tests__/field-carrier-sdui.test.tsx +169 -0
- package/src/__tests__/spec-symbol-batch7.test.ts +126 -0
- package/src/__tests__/validation-feedback.test.tsx +22 -13
- package/src/__tests__/widget-aria-invalid-e2e.test.tsx +265 -0
- package/src/__tests__/widget-aria-invalid-registry-e2e.test.tsx +338 -0
- package/src/__tests__/widget-dom-leak-e2e.test.tsx +598 -0
- package/src/__tests__/widget-props-contract.test.tsx +142 -0
- package/src/complex-widgets.test.tsx +3 -3
- package/src/datetime-widgets.test.tsx +2 -2
- package/src/index.tsx +77 -27
- package/src/widgets/AddressField.tsx +32 -13
- package/src/widgets/AddressField.uniqueIds.test.tsx +92 -0
- package/src/widgets/AutoNumberField.tsx +2 -2
- package/src/widgets/AvatarField.tsx +10 -3
- package/src/widgets/BooleanField.tsx +5 -5
- package/src/widgets/CapabilityMultiSelectField.tsx +4 -4
- package/src/widgets/CharacterCount.tsx +263 -0
- package/src/widgets/CheckboxesField.tsx +35 -18
- package/src/widgets/CodeField.tsx +10 -3
- package/src/widgets/ColorField.tsx +12 -3
- package/src/widgets/CurrencyField.tsx +6 -5
- package/src/widgets/DateField.tsx +4 -4
- package/src/widgets/DateTimeField.tsx +4 -4
- package/src/widgets/EmailField.tsx +6 -6
- package/src/widgets/FileField.tsx +14 -4
- package/src/widgets/FilterConditionField.tsx +14 -3
- package/src/widgets/FormulaField.tsx +3 -3
- package/src/widgets/FullscreenFieldEditor.tsx +271 -0
- package/src/widgets/GeolocationField.tsx +22 -7
- package/src/widgets/GeolocationField.uniqueIds.test.tsx +88 -0
- package/src/widgets/GridField.tsx +4 -4
- package/src/widgets/ImageField.tsx +10 -3
- package/src/widgets/LocationField.tsx +10 -3
- package/src/widgets/LookupField.gateHintLabel.test.tsx +135 -0
- package/src/widgets/LookupField.tsx +50 -15
- package/src/widgets/MasterDetailField.tsx +3 -3
- package/src/widgets/MultiSelectField.tsx +34 -18
- package/src/widgets/NumberField.tsx +5 -5
- package/src/widgets/ObjectField.tsx +18 -9
- package/src/widgets/ObjectRefField.tsx +14 -4
- package/src/widgets/OptionsEmptyState.no-provider.test.tsx +88 -0
- package/src/widgets/OptionsEmptyState.test.tsx +169 -0
- package/src/widgets/OptionsEmptyState.tsx +83 -0
- package/src/widgets/PasswordField.tsx +5 -5
- package/src/widgets/PeoplePicker.tsx +7 -1
- package/src/widgets/PercentField.tsx +6 -5
- package/src/widgets/PhoneField.tsx +6 -6
- package/src/widgets/QRCodeField.tsx +10 -3
- package/src/widgets/RadioField.tsx +28 -18
- package/src/widgets/RatingField.tsx +19 -4
- package/src/widgets/RecipientPickerField.tsx +22 -4
- package/src/widgets/RecordPickerDialog.filterOptions.test.tsx +611 -0
- package/src/widgets/RecordPickerDialog.selectLabel.test.tsx +112 -0
- package/src/widgets/RecordPickerDialog.tsx +173 -8
- package/src/widgets/RichTextField.tsx +152 -25
- package/src/widgets/SelectField.tsx +53 -22
- package/src/widgets/SignatureField.tsx +2 -2
- package/src/widgets/SliderField.tsx +3 -3
- package/src/widgets/SummaryField.tsx +3 -3
- package/src/widgets/TagsField.placeholder.no-provider.test.tsx +68 -0
- package/src/widgets/TagsField.placeholder.test.tsx +95 -0
- package/src/widgets/TagsField.tsx +25 -4
- package/src/widgets/TextAreaField.tsx +172 -94
- package/src/widgets/TextField.tsx +5 -5
- package/src/widgets/TimeField.tsx +4 -4
- package/src/widgets/UrlField.tsx +6 -6
- package/src/widgets/UserField.tsx +3 -3
- package/src/widgets/VectorField.tsx +3 -3
- package/src/widgets/__tests__/FullscreenFieldEditor.i18n.test.tsx +248 -0
- package/src/widgets/__tests__/FullscreenFieldEditor.no-provider.test.tsx +116 -0
- package/src/widgets/__tests__/RichTextField.fullscreenDisabled.test.tsx +244 -0
- package/src/widgets/__tests__/RichTextField.mobileFullscreen.test.tsx +195 -0
- package/src/widgets/__tests__/TextAreaField.characterCount.announcements.test.tsx +417 -0
- package/src/widgets/__tests__/TextAreaField.characterCount.i18n.test.tsx +287 -0
- package/src/widgets/__tests__/TextAreaField.characterCount.no-provider.test.tsx +160 -0
- package/src/widgets/__tests__/TextAreaField.fullscreenCharacterCount.i18n.test.tsx +161 -0
- package/src/widgets/__tests__/TextAreaField.fullscreenCharacterCount.test.tsx +346 -0
- package/src/widgets/__tests__/TextAreaField.fullscreenDisabled.test.tsx +246 -0
- package/src/widgets/__tests__/TextAreaField.mobileFullscreen.test.tsx +175 -0
- package/src/widgets/file-value.ts +21 -5
- package/src/widgets/toDomProps.ts +175 -0
- package/src/widgets/types.ts +230 -7
- package/src/widgets/useFieldTranslation.ts +60 -0
- package/src/withFieldCarrier.tsx +75 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI
|
|
3
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* `@object-ui/fields` ↔ `@objectstack/spec` symbol-collision guards
|
|
11
|
+
* (objectui#3161, objectstack#4115 ledger batch 7).
|
|
12
|
+
*
|
|
13
|
+
* Two symbols, two different verdicts:
|
|
14
|
+
*
|
|
15
|
+
* - `isFileIdToken` was a character-for-character copy of the spec's function
|
|
16
|
+
* under the spec's own name. It is a re-export now, and the gate below is
|
|
17
|
+
* REFERENCE IDENTITY — not behaviour. A faithful copy passes every behaviour
|
|
18
|
+
* test ever written for it (objectui#3003, re-proved on
|
|
19
|
+
* `isAggregatedViewContainer` in objectui#3169), so `toBe` is the only
|
|
20
|
+
* assertion that can tell a re-export from a fork.
|
|
21
|
+
*
|
|
22
|
+
* - `FieldWidgetProps` is now `FieldWidgetComponentProps`. The spec owns that
|
|
23
|
+
* name for the DECLARED widget-plugin props contract; this package's is the
|
|
24
|
+
* React interface its widgets implement. The pins record what still makes
|
|
25
|
+
* re-exporting impossible (`field` is the far richer `FieldMetadata` here)
|
|
26
|
+
* and, since objectui#3222, what no longer does: the validation slot is the
|
|
27
|
+
* spec's `error` on both sides.
|
|
28
|
+
*
|
|
29
|
+
* The other direction — "the spec must not start owning the NEW names" — is not
|
|
30
|
+
* asserted here because `scripts/check-spec-symbol-derivation.mjs` already
|
|
31
|
+
* answers it on every CI run, for every name in the package rather than the
|
|
32
|
+
* handful a test would remember to list.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
import { describe, it, expect } from 'vitest';
|
|
36
|
+
import { isFileIdToken as specIsFileIdToken } from '@objectstack/spec/data';
|
|
37
|
+
import type { FieldWidgetProps as SpecFieldWidgetProps } from '@objectstack/spec/ui';
|
|
38
|
+
|
|
39
|
+
import { isFileIdToken, fileIdOf } from '../widgets/file-value';
|
|
40
|
+
import type { FieldWidgetComponentProps } from '../widgets/types';
|
|
41
|
+
|
|
42
|
+
describe('isFileIdToken IS the spec function', () => {
|
|
43
|
+
it('is the same function object, not an equivalent one', () => {
|
|
44
|
+
// The whole point. Every value/behaviour assertion below this line would
|
|
45
|
+
// also pass against a re-forked copy; this one would not.
|
|
46
|
+
expect(isFileIdToken).toBe(specIsFileIdToken);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('is the arbiter the rest of the module routes through', () => {
|
|
50
|
+
// `fileIdOf` must keep agreeing with the shared predicate, so a future
|
|
51
|
+
// "small local tweak" to the id shape cannot be reintroduced one caller at
|
|
52
|
+
// a time.
|
|
53
|
+
for (const value of ['abc123', 'AbC-_9', 'x'.repeat(64)]) {
|
|
54
|
+
expect(specIsFileIdToken(value)).toBe(true);
|
|
55
|
+
expect(fileIdOf(value)).toBe(value);
|
|
56
|
+
}
|
|
57
|
+
for (const value of ['https://example.com/f.png', '/api/v1/storage/files/1', 'a.b', 'x'.repeat(65)]) {
|
|
58
|
+
expect(specIsFileIdToken(value)).toBe(false);
|
|
59
|
+
expect(fileIdOf(value)).toBeUndefined();
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
/* -------------------------------------------------------------------------- */
|
|
65
|
+
/* Compile-time pins. A violation is a `tsc` error, not a runtime failure. */
|
|
66
|
+
/* This package's tsconfig.json includes its tests, so `type-check` compiles */
|
|
67
|
+
/* them (scripts/check-type-check-coverage.mjs keeps that true). */
|
|
68
|
+
/* -------------------------------------------------------------------------- */
|
|
69
|
+
|
|
70
|
+
type Assert<T extends true> = T;
|
|
71
|
+
type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 ? true : false;
|
|
72
|
+
type Extends<A, B> = [A] extends [B] ? true : false;
|
|
73
|
+
type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
74
|
+
type HasKey<T, K extends string> = K extends keyof T ? true : false;
|
|
75
|
+
|
|
76
|
+
describe('FieldWidgetComponentProps is the RENDERED layer of the spec contract', () => {
|
|
77
|
+
it('is pinned at compile time', () => {
|
|
78
|
+
// The import above is itself half the tripwire: the day the spec stops
|
|
79
|
+
// exporting `FieldWidgetProps`, this file stops compiling and the rename's
|
|
80
|
+
// reason is up for re-triage (the plain name could come back).
|
|
81
|
+
type _SpecIsReal = Assert<Equal<IsAny<SpecFieldWidgetProps>, false>>;
|
|
82
|
+
|
|
83
|
+
// The validation-message slot no longer diverges. objectui#3161 could only
|
|
84
|
+
// RECORD that the declared contract called it `error` while every widget
|
|
85
|
+
// here read `errorMessage`; objectui#3222 resolved it in the direction the
|
|
86
|
+
// contract points — this package adopted `error`, and the form renderer
|
|
87
|
+
// started producing it, so the name and the delivery landed together.
|
|
88
|
+
// Both sides are pinned: the spec still owns the name, and this package
|
|
89
|
+
// still spells it the same way.
|
|
90
|
+
type _SpecNamesItError = Assert<HasKey<SpecFieldWidgetProps, 'error'>>;
|
|
91
|
+
type _LocalNamesItErrorToo = Assert<HasKey<FieldWidgetComponentProps, 'error'>>;
|
|
92
|
+
type _SameOptionalStringSlot = Assert<
|
|
93
|
+
Equal<FieldWidgetComponentProps['error'], SpecFieldWidgetProps['error']>
|
|
94
|
+
>;
|
|
95
|
+
// The old name is gone, not kept as an alias. An alias would be exactly the
|
|
96
|
+
// lenient second dialect AGENTS.md #0.1 forbids — and the reason a missed
|
|
97
|
+
// call site would have gone quiet again.
|
|
98
|
+
type _OldNameRetired = Assert<Equal<HasKey<FieldWidgetComponentProps, 'errorMessage'>, false>>;
|
|
99
|
+
|
|
100
|
+
// …and the reason nobody noticed used to sit right here: three pins
|
|
101
|
+
// asserting that `[key: string]: any` was still present, and that
|
|
102
|
+
// `props.required` / `props.error` therefore read as `any`. objectui#3221
|
|
103
|
+
// removed that index signature, so those pins have gone red on purpose and
|
|
104
|
+
// are gone with it. What replaces them is the inverse claim — the type is
|
|
105
|
+
// now CLOSED, so a key the spec declares and this one does not can finally
|
|
106
|
+
// be reported as missing. That is what made objectui#3222's rename
|
|
107
|
+
// decidable by the compiler instead of by a symbol guard.
|
|
108
|
+
type _NoStringIndexSignature = Assert<Equal<Extends<string, keyof FieldWidgetComponentProps>, false>>;
|
|
109
|
+
// `required` is the one key that stayed behind, and deliberately so
|
|
110
|
+
// (objectui#3222): the required marker has a single author — the form
|
|
111
|
+
// renderer's `<FormLabel>` — and lowering the flag into widget props gives
|
|
112
|
+
// it a second, i.e. the double-display failure that also keeps the
|
|
113
|
+
// validation TEXT out of the widget. The a11y state a widget could
|
|
114
|
+
// legitimately carry is `aria-required`, and `AriaAttributes` already
|
|
115
|
+
// supplies that key with no contract change at all.
|
|
116
|
+
type _RequiredIsAbsent = Assert<Equal<HasKey<FieldWidgetComponentProps, 'required'>, false>>;
|
|
117
|
+
|
|
118
|
+
// `data-*` stays open (it is open in HTML too), but as a template-literal
|
|
119
|
+
// key — the distinction that keeps `keyof` finite above. A pin, because
|
|
120
|
+
// widening it back to `[key: string]` would silently restore the defect
|
|
121
|
+
// while every assertion here still read as "closed".
|
|
122
|
+
type _DataAttributesStayOpen = Assert<Extends<'data-testid', keyof FieldWidgetComponentProps>>;
|
|
123
|
+
|
|
124
|
+
expect(true).toBe(true);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -10,7 +10,16 @@
|
|
|
10
10
|
* P3.2 Field Widget Polish - Validation Feedback
|
|
11
11
|
*
|
|
12
12
|
* Tests that field widgets consistently handle validation feedback:
|
|
13
|
-
*
|
|
13
|
+
* `error` prop, aria-invalid attribute, and disabled states.
|
|
14
|
+
*
|
|
15
|
+
* The slot is `error` — the name `@objectstack/spec/ui`'s
|
|
16
|
+
* `FieldWidgetPropsSchema` gives it — since objectui#3222. These are the
|
|
17
|
+
* CONSUMER half only: they prove a widget handed a message marks its control
|
|
18
|
+
* invalid. That was true before #3222 too, under the name `errorMessage`, and
|
|
19
|
+
* it did not help anyone, because no host ever passed the prop. The PRODUCER
|
|
20
|
+
* half is `form-error-delivery.test.tsx` in `@object-ui/components` plus
|
|
21
|
+
* `widget-aria-invalid-e2e.test.tsx` next to this file; a rename alone would
|
|
22
|
+
* have swapped one dead key for another.
|
|
14
23
|
*/
|
|
15
24
|
|
|
16
25
|
import { describe, it, expect, vi } from 'vitest';
|
|
@@ -32,23 +41,23 @@ const noop = vi.fn();
|
|
|
32
41
|
|
|
33
42
|
describe('P3.2 Validation Feedback', () => {
|
|
34
43
|
// ---------------------------------------------------------------
|
|
35
|
-
// aria-invalid on
|
|
44
|
+
// aria-invalid on `error`
|
|
36
45
|
// ---------------------------------------------------------------
|
|
37
46
|
describe('aria-invalid attribute', () => {
|
|
38
|
-
it('EmailField sets aria-invalid when
|
|
47
|
+
it('EmailField sets aria-invalid when `error` provided', () => {
|
|
39
48
|
render(
|
|
40
49
|
<EmailField
|
|
41
50
|
value=""
|
|
42
51
|
onChange={noop}
|
|
43
52
|
field={{ type: 'email' } as any}
|
|
44
|
-
|
|
53
|
+
error="Invalid email"
|
|
45
54
|
/>
|
|
46
55
|
);
|
|
47
56
|
const input = screen.getByRole('textbox');
|
|
48
57
|
expect(input).toHaveAttribute('aria-invalid', 'true');
|
|
49
58
|
});
|
|
50
59
|
|
|
51
|
-
it('EmailField does not set aria-invalid without
|
|
60
|
+
it('EmailField does not set aria-invalid without `error`', () => {
|
|
52
61
|
render(
|
|
53
62
|
<EmailField
|
|
54
63
|
value=""
|
|
@@ -60,52 +69,52 @@ describe('P3.2 Validation Feedback', () => {
|
|
|
60
69
|
expect(input).not.toHaveAttribute('aria-invalid', 'true');
|
|
61
70
|
});
|
|
62
71
|
|
|
63
|
-
it('UrlField sets aria-invalid when
|
|
72
|
+
it('UrlField sets aria-invalid when `error` provided', () => {
|
|
64
73
|
render(
|
|
65
74
|
<UrlField
|
|
66
75
|
value=""
|
|
67
76
|
onChange={noop}
|
|
68
77
|
field={{ type: 'url' } as any}
|
|
69
|
-
|
|
78
|
+
error="Invalid URL"
|
|
70
79
|
/>
|
|
71
80
|
);
|
|
72
81
|
const input = screen.getByDisplayValue('');
|
|
73
82
|
expect(input).toHaveAttribute('aria-invalid', 'true');
|
|
74
83
|
});
|
|
75
84
|
|
|
76
|
-
it('PhoneField sets aria-invalid when
|
|
85
|
+
it('PhoneField sets aria-invalid when `error` provided', () => {
|
|
77
86
|
render(
|
|
78
87
|
<PhoneField
|
|
79
88
|
value=""
|
|
80
89
|
onChange={noop}
|
|
81
90
|
field={{ type: 'phone' } as any}
|
|
82
|
-
|
|
91
|
+
error="Invalid phone"
|
|
83
92
|
/>
|
|
84
93
|
);
|
|
85
94
|
const input = screen.getByRole('textbox');
|
|
86
95
|
expect(input).toHaveAttribute('aria-invalid', 'true');
|
|
87
96
|
});
|
|
88
97
|
|
|
89
|
-
it('TextAreaField sets aria-invalid when
|
|
98
|
+
it('TextAreaField sets aria-invalid when `error` provided', () => {
|
|
90
99
|
render(
|
|
91
100
|
<TextAreaField
|
|
92
101
|
value=""
|
|
93
102
|
onChange={noop}
|
|
94
103
|
field={{ type: 'textarea' } as any}
|
|
95
|
-
|
|
104
|
+
error="Required"
|
|
96
105
|
/>
|
|
97
106
|
);
|
|
98
107
|
const textarea = screen.getByRole('textbox');
|
|
99
108
|
expect(textarea).toHaveAttribute('aria-invalid', 'true');
|
|
100
109
|
});
|
|
101
110
|
|
|
102
|
-
it('CurrencyField sets aria-invalid when
|
|
111
|
+
it('CurrencyField sets aria-invalid when `error` provided', () => {
|
|
103
112
|
render(
|
|
104
113
|
<CurrencyField
|
|
105
114
|
value={0}
|
|
106
115
|
onChange={noop}
|
|
107
116
|
field={{ type: 'currency' } as any}
|
|
108
|
-
|
|
117
|
+
error="Must be positive"
|
|
109
118
|
/>
|
|
110
119
|
);
|
|
111
120
|
const input = screen.getByRole('spinbutton');
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ObjectUI
|
|
3
|
+
* Copyright (c) 2024-present ObjectStack Inc.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* End-to-end: a REAL widget, inside the REAL form renderer, announces an
|
|
11
|
+
* invalid field to assistive tech (objectui#3222).
|
|
12
|
+
*
|
|
13
|
+
* The two halves of this contract have unit tests of their own — the widgets
|
|
14
|
+
* mark themselves invalid when handed an `error`
|
|
15
|
+
* (`validation-feedback.test.tsx`), and the form renderer produces one
|
|
16
|
+
* (`form-error-delivery.test.tsx` in `@object-ui/components`). Neither alone
|
|
17
|
+
* would have caught the actual defect, because the defect was precisely that
|
|
18
|
+
* the two halves never met: the widgets read `errorMessage`, the spec's
|
|
19
|
+
* published contract said `error`, and NOTHING in `packages/` or `apps/`
|
|
20
|
+
* produced either. Seven widgets computed `aria-invalid={!!errorMessage}` from
|
|
21
|
+
* a value that was `undefined` forever, so `aria-invalid` had never once been
|
|
22
|
+
* set on a failing field.
|
|
23
|
+
*
|
|
24
|
+
* So this file joins them: real react-hook-form validation, real widget, one
|
|
25
|
+
* assertion per widget that the control is `aria-invalid="true"` after the
|
|
26
|
+
* failure and `"false"` before it. `aria-invalid="false"` up front is the load
|
|
27
|
+
* bearing half of each pair — `<FormControl>` is a Radix `Slot` that hands its
|
|
28
|
+
* child a correct `aria-invalid`, and the widget's own attribute is written
|
|
29
|
+
* AFTER the props spread, so it wins. A widget computing it from a prop nobody
|
|
30
|
+
* passes does not merely miss the signal, it OVERWRITES the correct one with
|
|
31
|
+
* `false`.
|
|
32
|
+
*
|
|
33
|
+
* The widgets are registered raw rather than through `registerAllFields()`,
|
|
34
|
+
* which wraps every loader in `React.lazy`: an unbounded module load inside a
|
|
35
|
+
* bounded `findBy`/`waitFor` window is the repo's known flake generator
|
|
36
|
+
* (AGENTS.md 测试纪律, objectui#3010). Same component, no Suspense boundary.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
import { describe, it, expect, beforeAll, beforeEach, afterEach, vi } from 'vitest';
|
|
40
|
+
import { render, screen, fireEvent, waitFor, cleanup } from '@testing-library/react';
|
|
41
|
+
import '@testing-library/jest-dom';
|
|
42
|
+
import { ComponentRegistry } from '@object-ui/core';
|
|
43
|
+
// Module scope: pulls in the form renderer's registration side effect.
|
|
44
|
+
import '@object-ui/components';
|
|
45
|
+
|
|
46
|
+
import { EmailField } from '../widgets/EmailField';
|
|
47
|
+
import { PhoneField } from '../widgets/PhoneField';
|
|
48
|
+
import { UrlField } from '../widgets/UrlField';
|
|
49
|
+
import { TextAreaField } from '../widgets/TextAreaField';
|
|
50
|
+
import { CurrencyField } from '../widgets/CurrencyField';
|
|
51
|
+
import { PercentField } from '../widgets/PercentField';
|
|
52
|
+
import { RichTextField } from '../widgets/RichTextField';
|
|
53
|
+
import { TextField } from '../widgets/TextField';
|
|
54
|
+
import { SelectField } from '../widgets/SelectField';
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The widgets that read the validation slot, by their form-path key: the seven
|
|
58
|
+
* from objectui#3222, plus `select` (objectui#3306) — the widget whose control
|
|
59
|
+
* is a Radix trigger rather than a native input, where every aria-* used to be
|
|
60
|
+
* swallowed by the non-DOM `Select.Root`.
|
|
61
|
+
*/
|
|
62
|
+
const WIDGETS = [
|
|
63
|
+
['email', EmailField],
|
|
64
|
+
['phone', PhoneField],
|
|
65
|
+
['url', UrlField],
|
|
66
|
+
['textarea', TextAreaField],
|
|
67
|
+
['currency', CurrencyField],
|
|
68
|
+
['percent', PercentField],
|
|
69
|
+
['markdown', RichTextField],
|
|
70
|
+
['select', SelectField],
|
|
71
|
+
] as const;
|
|
72
|
+
|
|
73
|
+
beforeAll(() => {
|
|
74
|
+
for (const [type, Widget] of WIDGETS) {
|
|
75
|
+
ComponentRegistry.register(type, Widget as any, { namespace: 'field', skipFallback: true });
|
|
76
|
+
}
|
|
77
|
+
}, 30000);
|
|
78
|
+
|
|
79
|
+
beforeEach(() => {
|
|
80
|
+
if (!(Element.prototype as any).scrollIntoView) {
|
|
81
|
+
(Element.prototype as any).scrollIntoView = () => {};
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
afterEach(() => {
|
|
86
|
+
cleanup();
|
|
87
|
+
vi.restoreAllMocks();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
function renderForm(field: any, defaultValue: unknown) {
|
|
91
|
+
const Form = ComponentRegistry.get('form')!;
|
|
92
|
+
return render(
|
|
93
|
+
<Form
|
|
94
|
+
schema={{
|
|
95
|
+
type: 'form',
|
|
96
|
+
mode: 'create',
|
|
97
|
+
showSubmit: true,
|
|
98
|
+
showCancel: false,
|
|
99
|
+
submitLabel: 'Create',
|
|
100
|
+
defaultValues: { [field.name]: defaultValue },
|
|
101
|
+
fields: [field],
|
|
102
|
+
onSubmit: () => {},
|
|
103
|
+
}}
|
|
104
|
+
/>,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The control the widget rendered, located through the form row rather than by
|
|
110
|
+
* role — the seven widgets render `input[type=email|tel|url|number]` and
|
|
111
|
+
* `textarea` between them, and the point of the assertion is the ATTRIBUTE,
|
|
112
|
+
* not which element type carries it.
|
|
113
|
+
*/
|
|
114
|
+
function controlOf(name: string): Element {
|
|
115
|
+
const control = document.querySelector(
|
|
116
|
+
`[data-field="${name}"] input, [data-field="${name}"] textarea`,
|
|
117
|
+
);
|
|
118
|
+
if (!control) throw new Error(`no control rendered for field "${name}"`);
|
|
119
|
+
return control;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const submit = () => fireEvent.click(screen.getByRole('button', { name: /create/i }));
|
|
123
|
+
|
|
124
|
+
describe('field widgets announce an invalid field to AT (objectui#3222)', () => {
|
|
125
|
+
// `field:textarea` because the bare `textarea` key is a BUILTIN_FIELD_TYPE in
|
|
126
|
+
// the form renderer and never reaches the registry — it is the prefixed id
|
|
127
|
+
// (`mapFieldTypeToFormType`) that resolves to this package's widget.
|
|
128
|
+
it.each([
|
|
129
|
+
['email', 'email', ''],
|
|
130
|
+
['phone', 'phone', ''],
|
|
131
|
+
['url', 'url', ''],
|
|
132
|
+
['field:textarea', 'notes', ''],
|
|
133
|
+
['currency', 'amount', null],
|
|
134
|
+
['percent', 'ratio', null],
|
|
135
|
+
['markdown', 'body', ''],
|
|
136
|
+
] as const)(
|
|
137
|
+
'a required %s field is aria-invalid only AFTER validation fails',
|
|
138
|
+
async (type, name, emptyValue) => {
|
|
139
|
+
renderForm({ name, label: name, type, required: true }, emptyValue);
|
|
140
|
+
|
|
141
|
+
// Before the submit there is no error, and the widget must say so —
|
|
142
|
+
// this is the half that a Slot-provided `aria-invalid` cannot fake.
|
|
143
|
+
expect(controlOf(name)).toHaveAttribute('aria-invalid', 'false');
|
|
144
|
+
|
|
145
|
+
submit();
|
|
146
|
+
|
|
147
|
+
await waitFor(() => {
|
|
148
|
+
expect(controlOf(name)).toHaveAttribute('aria-invalid', 'true');
|
|
149
|
+
});
|
|
150
|
+
},
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
it('the message itself is rendered once, by the form — not by the widget', async () => {
|
|
154
|
+
// Responsibility split (the ruling's scope item 3): the widget consumes
|
|
155
|
+
// `error` for `aria-invalid` ONLY. `<FormMessage/>` owns the text; a widget
|
|
156
|
+
// that printed it too would show the user the same sentence twice.
|
|
157
|
+
renderForm({ name: 'email', label: 'Email', type: 'email', required: true }, '');
|
|
158
|
+
|
|
159
|
+
submit();
|
|
160
|
+
|
|
161
|
+
await waitFor(() => expect(screen.getAllByText('Email is required')).toHaveLength(1));
|
|
162
|
+
// And the message is NOT inside the control the widget rendered.
|
|
163
|
+
expect(controlOf('email')).not.toHaveTextContent('Email is required');
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('a widget that does NOT read `error` is still marked invalid, by the Slot', async () => {
|
|
167
|
+
// The counterpart that makes the diagnosis precise: `TextField` never
|
|
168
|
+
// computed its own `aria-invalid`, so `<FormControl>`'s Slot value reached
|
|
169
|
+
// its input untouched and it announced invalid fields correctly all along.
|
|
170
|
+
// Only the seven widgets that DID compute one were broken — they wrote
|
|
171
|
+
// theirs after the props spread, so `!!undefined` won.
|
|
172
|
+
//
|
|
173
|
+
// Pinned because the obvious "cleanup" here is to stop forwarding `error`
|
|
174
|
+
// to widgets that do not declare a use for it. That cannot be known (a
|
|
175
|
+
// third-party widget is exactly the case the contract exists for), and
|
|
176
|
+
// withholding it would recreate declared-≠-delivered one widget at a time.
|
|
177
|
+
ComponentRegistry.register('plaintext', TextField as any, { namespace: 'field', skipFallback: true });
|
|
178
|
+
renderForm({ name: 'title', label: 'Title', type: 'plaintext', required: true }, '');
|
|
179
|
+
|
|
180
|
+
submit();
|
|
181
|
+
|
|
182
|
+
await waitFor(() => expect(controlOf('title')).toHaveAttribute('aria-invalid', 'true'));
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('clears the invalid state when the field becomes valid again', async () => {
|
|
186
|
+
renderForm({ name: 'email', label: 'Email', type: 'email', required: true }, '');
|
|
187
|
+
|
|
188
|
+
submit();
|
|
189
|
+
await waitFor(() => expect(controlOf('email')).toHaveAttribute('aria-invalid', 'true'));
|
|
190
|
+
|
|
191
|
+
fireEvent.change(controlOf('email'), { target: { value: 'a@example.com' } });
|
|
192
|
+
|
|
193
|
+
await waitFor(() => expect(controlOf('email')).toHaveAttribute('aria-invalid', 'false'));
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
describe('field:select announces validation state on its TRIGGER (objectui#3306)', () => {
|
|
198
|
+
// Radix `Select.Root` renders no DOM element and silently drops every prop
|
|
199
|
+
// it does not recognise. The widget used to spread its DOM pass-through onto
|
|
200
|
+
// Root, so `aria-invalid` / `aria-describedby` / `aria-required` all
|
|
201
|
+
// vanished: a failed required select showed the red message while assistive
|
|
202
|
+
// tech was told nothing. The real control is the focusable
|
|
203
|
+
// `<button role="combobox">` the trigger renders — that is where every
|
|
204
|
+
// assertion below reads from, never a wrapper.
|
|
205
|
+
//
|
|
206
|
+
// `field:select` (not bare `select`) because `select` is a
|
|
207
|
+
// BUILTIN_FIELD_TYPE in the form renderer and never reaches the registry —
|
|
208
|
+
// same reason as `field:textarea` above.
|
|
209
|
+
const OPTIONS = [
|
|
210
|
+
{ label: 'Alpha', value: 'alpha' },
|
|
211
|
+
{ label: 'Beta', value: 'beta' },
|
|
212
|
+
];
|
|
213
|
+
|
|
214
|
+
const selectField = {
|
|
215
|
+
name: 'choice',
|
|
216
|
+
label: 'Choice',
|
|
217
|
+
type: 'field:select',
|
|
218
|
+
required: true,
|
|
219
|
+
options: OPTIONS,
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/** The trigger button, by role — fails loudly if it stops being a combobox. */
|
|
223
|
+
function trigger(): Element {
|
|
224
|
+
const el = document.querySelector('[data-field="choice"] [role="combobox"]');
|
|
225
|
+
if (!el) throw new Error('no select trigger rendered for field "choice"');
|
|
226
|
+
return el;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
it('a required select is aria-invalid only AFTER validation fails, on the trigger', async () => {
|
|
230
|
+
renderForm(selectField, null);
|
|
231
|
+
|
|
232
|
+
// The explicit "false" half is load-bearing, same as the widgets above: a
|
|
233
|
+
// valid field SAYS it is valid rather than staying mute.
|
|
234
|
+
expect(trigger()).toHaveAttribute('aria-invalid', 'false');
|
|
235
|
+
|
|
236
|
+
submit();
|
|
237
|
+
|
|
238
|
+
// Prove the failure actually rendered before reading aria off it — an
|
|
239
|
+
// error variant that silently produces no error tests nothing.
|
|
240
|
+
await waitFor(() =>
|
|
241
|
+
expect(screen.getAllByText('Choice is required')).toHaveLength(1),
|
|
242
|
+
);
|
|
243
|
+
expect(trigger()).toHaveAttribute('aria-invalid', 'true');
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it('carries aria-required on the trigger — the state channel of objectui#3290', () => {
|
|
247
|
+
renderForm(selectField, null);
|
|
248
|
+
|
|
249
|
+
// Root would swallow it; the trigger is "the control" for a Radix select.
|
|
250
|
+
expect(trigger()).toHaveAttribute('aria-required', 'true');
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it('aria-describedby on the trigger references the rendered error message', async () => {
|
|
254
|
+
renderForm(selectField, null);
|
|
255
|
+
|
|
256
|
+
submit();
|
|
257
|
+
|
|
258
|
+
const message = await screen.findByText('Choice is required');
|
|
259
|
+
// The association is only real if the id chain closes: the trigger must
|
|
260
|
+
// point at the exact element carrying the message text.
|
|
261
|
+
expect(message.id).not.toBe('');
|
|
262
|
+
const describedBy = trigger().getAttribute('aria-describedby') ?? '';
|
|
263
|
+
expect(describedBy.split(/\s+/)).toContain(message.id);
|
|
264
|
+
});
|
|
265
|
+
});
|