@cdx-ui/utils 0.0.1-beta.6 → 0.0.1-beta.61
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/README.md +45 -22
- package/lib/commonjs/form-control/index.js +6 -0
- package/lib/commonjs/form-control/index.js.map +1 -1
- package/lib/commonjs/form-control/useFormControl.js +23 -0
- package/lib/commonjs/form-control/useFormControl.js.map +1 -1
- package/lib/module/form-control/index.js +1 -1
- package/lib/module/form-control/index.js.map +1 -1
- package/lib/module/form-control/useFormControl.js +22 -0
- package/lib/module/form-control/useFormControl.js.map +1 -1
- package/lib/typescript/form-control/index.d.ts +1 -1
- package/lib/typescript/form-control/index.d.ts.map +1 -1
- package/lib/typescript/form-control/useFormControl.d.ts +10 -0
- package/lib/typescript/form-control/useFormControl.d.ts.map +1 -1
- package/package.json +4 -2
- package/src/common/accessibilityUtils.test.ts +12 -0
- package/src/common/cn.test.ts +19 -0
- package/src/common/composeEventHandlers.test.ts +35 -0
- package/src/common/getSpacedChild.test.tsx +64 -0
- package/src/common/mergeRefs.test.ts +40 -0
- package/src/common/useControllableState.test.tsx +90 -0
- package/src/context/createContext.test.tsx +31 -0
- package/src/form-control/index.ts +1 -0
- package/src/form-control/useFormControl.test.tsx +214 -0
- package/src/form-control/useFormControl.tsx +27 -0
- package/src/style/context/index.test.tsx +31 -0
- package/src/style/withStyleContext/index.test.tsx +55 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @cdx-ui/utils
|
|
2
2
|
|
|
3
|
-
Shared utilities used across
|
|
3
|
+
Shared utilities used across FDS packages.
|
|
4
4
|
|
|
5
5
|
Provides common helpers for styling, event handling, state management, and context propagation used by [`@cdx-ui/primitives`](../primitives/) and [`@cdx-ui/components`](../components/).
|
|
6
6
|
|
|
@@ -14,42 +14,65 @@ pnpm add @cdx-ui/utils
|
|
|
14
14
|
|
|
15
15
|
### Common helpers
|
|
16
16
|
|
|
17
|
-
| Export
|
|
18
|
-
|
|
|
19
|
-
| `cn(...inputs)`
|
|
20
|
-
| `composeEventHandlers(...handlers)` | Chains multiple event callbacks into a single handler; skips `null`/`undefined` entries.
|
|
21
|
-
| `useControllableState(params)`
|
|
22
|
-
| `mergeRefs(...refs)`
|
|
23
|
-
| `flattenChildren(children)`
|
|
24
|
-
| `ariaAttr(condition)`
|
|
17
|
+
| Export | Description |
|
|
18
|
+
| ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
19
|
+
| `cn(...inputs)` | Merges class names with `clsx` + `tailwind-merge` — conditionals and conflict resolution in one call. |
|
|
20
|
+
| `composeEventHandlers(...handlers)` | Chains multiple event callbacks into a single handler; skips `null`/`undefined` entries. |
|
|
21
|
+
| `useControllableState(params)` | Hook that supports both controlled and uncontrolled modes for a single value. Derives `isControlled` from whether the prop is `undefined`. |
|
|
22
|
+
| `mergeRefs(...refs)` | Combines multiple refs (callback or object) into one ref callback. |
|
|
23
|
+
| `flattenChildren(children)` | Recursively flattens React `children` (including Fragments) into a flat array with re-keyed elements. |
|
|
24
|
+
| `ariaAttr(condition)` | Returns `true` (boolean) or `undefined` — shorthand for boolean ARIA attribute values. |
|
|
25
25
|
|
|
26
26
|
### Context
|
|
27
27
|
|
|
28
|
-
| Export
|
|
29
|
-
|
|
|
28
|
+
| Export | Description |
|
|
29
|
+
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
30
30
|
| `createContext(displayName)` | Wrapper around React's `createContext` that returns a `[Provider, useHook]` tuple. The hook throws a descriptive error if called outside the provider — eliminates silent `undefined` bugs. |
|
|
31
31
|
|
|
32
32
|
### Style context
|
|
33
33
|
|
|
34
34
|
Used internally by `@cdx-ui/components` to propagate variant props (size, color, etc.) from a compound root to its sub-components.
|
|
35
35
|
|
|
36
|
-
| Export
|
|
37
|
-
|
|
|
36
|
+
| Export | Description |
|
|
37
|
+
| ------------------------------------- | ---------------------------------------------------------------------------------- |
|
|
38
38
|
| `withStyleContext(Component, scope?)` | HOC that adds a `context` prop and provides it to descendants via `ParentContext`. |
|
|
39
|
-
| `useStyleContext(scope?)`
|
|
40
|
-
| `useParentContext()`
|
|
41
|
-
| `ParentContext` | The raw React context (rarely needed directly). |
|
|
39
|
+
| `useStyleContext(scope?)` | Reads the style context set by the nearest `withStyleContext` wrapper. |
|
|
40
|
+
| `useParentContext()` | Reads the full `StyleContextMap` from the nearest provider. |
|
|
42
41
|
|
|
43
42
|
### Form control
|
|
44
43
|
|
|
45
44
|
Low-level form-field state used by the `Field` and `Input` primitives.
|
|
46
45
|
|
|
47
|
-
| Export
|
|
48
|
-
|
|
|
49
|
-
| `useFormControlRoot(props)`
|
|
50
|
-
| `useFormControl(props)`
|
|
51
|
-
| `useFormControlContext()`
|
|
52
|
-
| `
|
|
46
|
+
| Export | Description |
|
|
47
|
+
| ------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
48
|
+
| `useFormControlRoot(props)` | Creates the root state object for a form field (id generation, tracks `hasFeedbackText`/`hasHelpText` for describedby assembly). |
|
|
49
|
+
| `useFormControl(props)` | Merges component-level props with inherited form-control context. Assembles `aria-describedby` from feedback/help text ids. |
|
|
50
|
+
| `useFormControlContext()` | Reads form-control state from the nearest provider. |
|
|
51
|
+
| `useReportFormControlLabelFocus(isActive)` | Reports focus/active state to the parent `FormControlContext` — used by `Input`/`Select.Trigger` to trigger `isFocused` styling on the associated label. |
|
|
52
|
+
| `FormControlContext` | The React context object for form-control state propagation. |
|
|
53
|
+
|
|
54
|
+
## Exported types
|
|
55
|
+
|
|
56
|
+
| Type | Source |
|
|
57
|
+
| ---------------------------- | ------------ |
|
|
58
|
+
| `UseControllableStateParams` | common |
|
|
59
|
+
| `FormControlContextValue` | form-control |
|
|
60
|
+
| `FormControlFieldProps` | form-control |
|
|
61
|
+
| `FormControlRootProps` | form-control |
|
|
62
|
+
| `FormControlRootReturn` | form-control |
|
|
63
|
+
| `FormControlState` | form-control |
|
|
64
|
+
| `StyleContextMap` | style |
|
|
65
|
+
| `WithStyleContextProps` | style |
|
|
66
|
+
|
|
67
|
+
## Testing
|
|
68
|
+
|
|
69
|
+
Tests are colocated next to source files (`*.test.{ts,tsx}`). Run via the `utils` Jest project:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
pnpm nx run @cdx-ui/utils:test
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
All runtime exports have test coverage using `@testing-library/react-native` (`renderHook`, `act`).
|
|
53
76
|
|
|
54
77
|
## Further reading
|
|
55
78
|
|
|
@@ -27,5 +27,11 @@ Object.defineProperty(exports, "useFormControlRoot", {
|
|
|
27
27
|
return _useFormControl.useFormControlRoot;
|
|
28
28
|
}
|
|
29
29
|
});
|
|
30
|
+
Object.defineProperty(exports, "useReportFormControlLabelFocus", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function () {
|
|
33
|
+
return _useFormControl.useReportFormControlLabelFocus;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
30
36
|
var _useFormControl = require("./useFormControl");
|
|
31
37
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_useFormControl","require"],"sourceRoot":"../../../src","sources":["form-control/index.ts"],"mappings":"
|
|
1
|
+
{"version":3,"names":["_useFormControl","require"],"sourceRoot":"../../../src","sources":["form-control/index.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,eAAA,GAAAC,OAAA","ignoreList":[]}
|
|
@@ -7,6 +7,7 @@ exports.FormControlContext = void 0;
|
|
|
7
7
|
exports.useFormControl = useFormControl;
|
|
8
8
|
exports.useFormControlContext = void 0;
|
|
9
9
|
exports.useFormControlRoot = useFormControlRoot;
|
|
10
|
+
exports.useReportFormControlLabelFocus = useReportFormControlLabelFocus;
|
|
10
11
|
var _react = _interopRequireDefault(require("react"));
|
|
11
12
|
var _reactNative = require("react-native");
|
|
12
13
|
var _accessibilityUtils = require("../common/accessibilityUtils");
|
|
@@ -46,6 +47,7 @@ function useFormControlRoot(props) {
|
|
|
46
47
|
* We use this to append its id the the `aria-describedby` of the `input`.
|
|
47
48
|
*/
|
|
48
49
|
const [hasHelpText, setHasHelpText] = _react.default.useState(false);
|
|
50
|
+
const [isLabelFocused, setIsLabelFocused] = _react.default.useState(false);
|
|
49
51
|
const inputRef = _react.default.useRef(null);
|
|
50
52
|
const focusInput = _react.default.useCallback(() => {
|
|
51
53
|
const node = inputRef.current;
|
|
@@ -68,6 +70,8 @@ function useFormControlRoot(props) {
|
|
|
68
70
|
feedbackId,
|
|
69
71
|
helpTextId,
|
|
70
72
|
htmlProps,
|
|
73
|
+
isLabelFocused,
|
|
74
|
+
setIsLabelFocused,
|
|
71
75
|
...(_reactNative.Platform.OS !== 'web' ? {
|
|
72
76
|
inputRef,
|
|
73
77
|
focusInput
|
|
@@ -76,6 +80,25 @@ function useFormControlRoot(props) {
|
|
|
76
80
|
return context;
|
|
77
81
|
}
|
|
78
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Reports focus/active state from `Input` / `Select.Trigger` to the parent `Field` for label styling.
|
|
85
|
+
* No-ops outside a `Field` (no `setIsLabelFocused` on context).
|
|
86
|
+
*/
|
|
87
|
+
function useReportFormControlLabelFocus(isActive) {
|
|
88
|
+
const {
|
|
89
|
+
setIsLabelFocused
|
|
90
|
+
} = useFormControlContext();
|
|
91
|
+
_react.default.useLayoutEffect(() => {
|
|
92
|
+
if (typeof setIsLabelFocused !== 'function') {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
setIsLabelFocused(isActive);
|
|
96
|
+
return () => {
|
|
97
|
+
setIsLabelFocused(false);
|
|
98
|
+
};
|
|
99
|
+
}, [isActive, setIsLabelFocused]);
|
|
100
|
+
}
|
|
101
|
+
|
|
79
102
|
/**
|
|
80
103
|
* Hook that merges local field props with the nearest form field context
|
|
81
104
|
* and returns normalised HTML / ARIA attributes for input elements.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_react","_interopRequireDefault","require","_reactNative","_accessibilityUtils","e","__esModule","default","FormControlContext","exports","React","createContext","useFormControlRoot","props","id","idProp","name","isRequired","isInvalid","isDisabled","isReadOnly","htmlProps","reactId","useId","labelId","feedbackId","helpTextId","hasFeedbackText","setHasFeedbackText","useState","hasHelpText","setHasHelpText","inputRef","useRef","focusInput","useCallback","node","current","focus","context","Boolean","Platform","OS","
|
|
1
|
+
{"version":3,"names":["_react","_interopRequireDefault","require","_reactNative","_accessibilityUtils","e","__esModule","default","FormControlContext","exports","React","createContext","useFormControlRoot","props","id","idProp","name","isRequired","isInvalid","isDisabled","isReadOnly","htmlProps","reactId","useId","labelId","feedbackId","helpTextId","hasFeedbackText","setHasFeedbackText","useState","hasHelpText","setHasHelpText","isLabelFocused","setIsLabelFocused","inputRef","useRef","focusInput","useCallback","node","current","focus","context","Boolean","Platform","OS","useReportFormControlLabelFocus","isActive","useFormControlContext","useLayoutEffect","undefined","useFormControl","field","describedBy","push","ariaDescribedBy","join","cleanProps","disabled","readOnly","required","ariaAttr","useContext"],"sourceRoot":"../../../src","sources":["form-control/useFormControl.tsx"],"mappings":";;;;;;;;;;AAAA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,YAAA,GAAAD,OAAA;AACA,IAAAE,mBAAA,GAAAF,OAAA;AAAwD,SAAAD,uBAAAI,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAE,OAAA,EAAAF,CAAA;AAiCjD,MAAMG,kBAAkB,GAAAC,OAAA,CAAAD,kBAAA,gBAAGE,cAAK,CAACC,aAAa,CAAmC,CAAC,CAAC,CAAC;;AAM3F;;AA2BA;AACA;AACA;AACA;AACO,SAASC,kBAAkBA,CAACC,KAA2B,EAAyB;EACrF,MAAM;IAAEC,EAAE,EAAEC,MAAM;IAAEC,IAAI;IAAEC,UAAU;IAAEC,SAAS;IAAEC,UA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AAU;IAAEH,UAAU;IAAE,GAAGuC;EAAW,CAAC,GAAG3C,KAAK;EAC9E,MAAMC,EAAE,GAAGD,KAAK,CAACC,EAAE,KAAKqC,KAAK,CAACrC,EAAE,GAAG,GAAGqC,KAAK,CAACrC,EAAE,QAAQ,GAAGmC,SAAS,CAAC;EAEnE,OAAO;IACL,GAAGO,UAAU;IACb1C,EAAE;IACFE,IAAI,EAAEmC,KAAK,CAACnC,IAAI;IAChByC,QAAQ,EAAEtC,UAAU,IAAIgC,KAAK,CAAChC,UAAU;IACxCuC,QAAQ,EAAEtC,UAAU,IAAI+B,KAAK,CAAC/B,UAAU;IACxCuC,QAAQ,EAAE1C,UAAU,IAAIkC,KAAK,CAAClC,UAAU;IACxC,cAAc,EAAE,IAAA2C,4BAAQ,EAAC1C,SAAS,IAAIiC,KAAK,CAACjC,SAAS,CAAC;IACtD,eAAe,EAAE,IAAA0C,4BAAQ,EAAC3C,UAAU,IAAIkC,KAAK,CAAClC,UAAU,CAAC;IACzD,eAAe,EAAE,IAAA2C,4BAAQ,EAACxC,UAAU,IAAI+B,KAAK,CAAC/B,UAAU,CAAC;IACzD,kBAAkB,EAAEkC,eAAe,IAAIL,SAAS;IAChD,iBAAiB,EAAEE,KAAK,CAAC3B;EAC3B,CAAC;AACH;AAEO,MAAMuB,qBAAqB,GAAGA,CAAA,KAAM;EACzC,OAAOrC,cAAK,CAACmD,UAAU,CAACrD,kBAAkB,CAAC;AAC7C,CAAC;AAACC,OAAA,CAAAsC,qBAAA,GAAAA,qBAAA","ignoreList":[]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
export { FormControlContext, useFormControl, useFormControlContext, useFormControlRoot } from './useFormControl';
|
|
3
|
+
export { FormControlContext, useFormControl, useFormControlContext, useFormControlRoot, useReportFormControlLabelFocus } from './useFormControl';
|
|
4
4
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["FormControlContext","useFormControl","useFormControlContext","useFormControlRoot"],"sourceRoot":"../../../src","sources":["form-control/index.ts"],"mappings":";;AAAA,SAMEA,kBAAkB,EAClBC,cAAc,EACdC,qBAAqB,EACrBC,kBAAkB,
|
|
1
|
+
{"version":3,"names":["FormControlContext","useFormControl","useFormControlContext","useFormControlRoot","useReportFormControlLabelFocus"],"sourceRoot":"../../../src","sources":["form-control/index.ts"],"mappings":";;AAAA,SAMEA,kBAAkB,EAClBC,cAAc,EACdC,qBAAqB,EACrBC,kBAAkB,EAClBC,8BAA8B,QACzB,kBAAkB","ignoreList":[]}
|
|
@@ -38,6 +38,7 @@ export function useFormControlRoot(props) {
|
|
|
38
38
|
* We use this to append its id the the `aria-describedby` of the `input`.
|
|
39
39
|
*/
|
|
40
40
|
const [hasHelpText, setHasHelpText] = React.useState(false);
|
|
41
|
+
const [isLabelFocused, setIsLabelFocused] = React.useState(false);
|
|
41
42
|
const inputRef = React.useRef(null);
|
|
42
43
|
const focusInput = React.useCallback(() => {
|
|
43
44
|
const node = inputRef.current;
|
|
@@ -60,6 +61,8 @@ export function useFormControlRoot(props) {
|
|
|
60
61
|
feedbackId,
|
|
61
62
|
helpTextId,
|
|
62
63
|
htmlProps,
|
|
64
|
+
isLabelFocused,
|
|
65
|
+
setIsLabelFocused,
|
|
63
66
|
...(Platform.OS !== 'web' ? {
|
|
64
67
|
inputRef,
|
|
65
68
|
focusInput
|
|
@@ -68,6 +71,25 @@ export function useFormControlRoot(props) {
|
|
|
68
71
|
return context;
|
|
69
72
|
}
|
|
70
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Reports focus/active state from `Input` / `Select.Trigger` to the parent `Field` for label styling.
|
|
76
|
+
* No-ops outside a `Field` (no `setIsLabelFocused` on context).
|
|
77
|
+
*/
|
|
78
|
+
export function useReportFormControlLabelFocus(isActive) {
|
|
79
|
+
const {
|
|
80
|
+
setIsLabelFocused
|
|
81
|
+
} = useFormControlContext();
|
|
82
|
+
React.useLayoutEffect(() => {
|
|
83
|
+
if (typeof setIsLabelFocused !== 'function') {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
setIsLabelFocused(isActive);
|
|
87
|
+
return () => {
|
|
88
|
+
setIsLabelFocused(false);
|
|
89
|
+
};
|
|
90
|
+
}, [isActive, setIsLabelFocused]);
|
|
91
|
+
}
|
|
92
|
+
|
|
71
93
|
/**
|
|
72
94
|
* Hook that merges local field props with the nearest form field context
|
|
73
95
|
* and returns normalised HTML / ARIA attributes for input elements.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["React","Platform","ariaAttr","FormControlContext","createContext","useFormControlRoot","props","id","idProp","name","isRequired","isInvalid","isDisabled","isReadOnly","htmlProps","reactId","useId","labelId","feedbackId","helpTextId","hasFeedbackText","setHasFeedbackText","useState","hasHelpText","setHasHelpText","inputRef","useRef","focusInput","useCallback","node","current","focus","context","Boolean","OS","
|
|
1
|
+
{"version":3,"names":["React","Platform","ariaAttr","FormControlContext","createContext","useFormControlRoot","props","id","idProp","name","isRequired","isInvalid","isDisabled","isReadOnly","htmlProps","reactId","useId","labelId","feedbackId","helpTextId","hasFeedbackText","setHasFeedbackText","useState","hasHelpText","setHasHelpText","isLabelFocused","setIsLabelFocused","inputRef","useRef","focusInput","useCallback","node","current","focus","context","Boolean","OS","useReportFormControlLabelFocus","isActive","useFormControlContext","useLayoutEffect","undefined","useFormControl","field","describedBy","push","ariaDescribedBy","join","cleanProps","disabled","readOnly","required","useContext"],"sourceRoot":"../../../src","sources":["form-control/useFormControl.tsx"],"mappings":";;AAAA,OAAOA,KAAK,MAAM,OAAO;AACzB,SAASC,QAAQ,QAAwB,cAAc;AACvD,SAASC,QAAQ,QAAQ,8BAA8B;AAiCvD,OAAO,MAAMC,kBAAkB,gBAAGH,KAAK,CAACI,aAAa,CAAmC,CAAC,CAAC,CAAC;;AAM3F;;AA2BA;AACA;AACA;AACA;AACA,OAAO,SAASC,kBAAkBA,CAACC,KAA2B,EAA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AM2B,eAAe,GAAGF,WAAW,CAACG,IAAI,CAAC,GAAG,CAAC;EAE7C,MAAM;IAAEpC,SAAS;IAAEC,UAAU;IAAEC,UAAU;IAAEH,UAAU;IAAE,GAAGsC;EAAW,CAAC,GAAG1C,KAAK;EAC9E,MAAMC,EAAE,GAAGD,KAAK,CAACC,EAAE,KAAKoC,KAAK,CAACpC,EAAE,GAAG,GAAGoC,KAAK,CAACpC,EAAE,QAAQ,GAAGkC,SAAS,CAAC;EAEnE,OAAO;IACL,GAAGO,UAAU;IACbzC,EAAE;IACFE,IAAI,EAAEkC,KAAK,CAAClC,IAAI;IAChBwC,QAAQ,EAAErC,UAAU,IAAI+B,KAAK,CAAC/B,UAAU;IACxCsC,QAAQ,EAAErC,UAAU,IAAI8B,KAAK,CAAC9B,UAAU;IACxCsC,QAAQ,EAAEzC,UAAU,IAAIiC,KAAK,CAACjC,UAAU;IACxC,cAAc,EAAER,QAAQ,CAACS,SAAS,IAAIgC,KAAK,CAAChC,SAAS,CAAC;IACtD,eAAe,EAAET,QAAQ,CAACQ,UAAU,IAAIiC,KAAK,CAACjC,UAAU,CAAC;IACzD,eAAe,EAAER,QAAQ,CAACW,UAAU,IAAI8B,KAAK,CAAC9B,UAAU,CAAC;IACzD,kBAAkB,EAAEiC,eAAe,IAAIL,SAAS;IAChD,iBAAiB,EAAEE,KAAK,CAAC1B;EAC3B,CAAC;AACH;AAEA,OAAO,MAAMsB,qBAAqB,GAAGA,CAAA,KAAM;EACzC,OAAOvC,KAAK,CAACoD,UAAU,CAACjD,kBAAkB,CAAC;AAC7C,CAAC","ignoreList":[]}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { type FormControlContextValue, type FormControlFieldProps, type FormControlRootProps, type FormControlRootReturn, type FormControlState, FormControlContext, useFormControl, useFormControlContext, useFormControlRoot, } from './useFormControl';
|
|
1
|
+
export { type FormControlContextValue, type FormControlFieldProps, type FormControlRootProps, type FormControlRootReturn, type FormControlState, FormControlContext, useFormControl, useFormControlContext, useFormControlRoot, useReportFormControlLabelFocus, } from './useFormControl';
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/form-control/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,uBAAuB,EAC5B,KAAK,qBAAqB,EAC1B,KAAK,oBAAoB,EACzB,KAAK,qBAAqB,EAC1B,KAAK,gBAAgB,EACrB,kBAAkB,EAClB,cAAc,EACd,qBAAqB,EACrB,kBAAkB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/form-control/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,uBAAuB,EAC5B,KAAK,qBAAqB,EAC1B,KAAK,oBAAoB,EACzB,KAAK,qBAAqB,EAC1B,KAAK,gBAAgB,EACrB,kBAAkB,EAClB,cAAc,EACd,qBAAqB,EACrB,kBAAkB,EAClB,8BAA8B,GAC/B,MAAM,kBAAkB,CAAC"}
|
|
@@ -24,6 +24,9 @@ export interface FormControlContextValue extends FormControlState {
|
|
|
24
24
|
*/
|
|
25
25
|
inputRef?: React.RefObject<TextInput | null>;
|
|
26
26
|
focusInput?: () => void;
|
|
27
|
+
/** True when the primary child control is focused or (for Select) open — drives `Field.Label` styling. */
|
|
28
|
+
isLabelFocused?: boolean;
|
|
29
|
+
setIsLabelFocused?: React.Dispatch<React.SetStateAction<boolean>>;
|
|
27
30
|
}
|
|
28
31
|
export declare const FormControlContext: React.Context<Partial<FormControlContextValue>>;
|
|
29
32
|
export interface FormControlFieldProps extends Partial<FormControlState> {
|
|
@@ -51,12 +54,19 @@ export interface FormControlRootReturn {
|
|
|
51
54
|
htmlProps: HTMLProps;
|
|
52
55
|
inputRef?: React.RefObject<TextInput | null>;
|
|
53
56
|
focusInput?: () => void;
|
|
57
|
+
isLabelFocused: boolean;
|
|
58
|
+
setIsLabelFocused: React.Dispatch<React.SetStateAction<boolean>>;
|
|
54
59
|
}
|
|
55
60
|
/**
|
|
56
61
|
* Builds state and ids for a `Form` primitive root (`createFormRoot`). Pass the returned object
|
|
57
62
|
* (minus `htmlProps`) to `FormControlContext.Provider` — this is not a React context consumer hook.
|
|
58
63
|
*/
|
|
59
64
|
export declare function useFormControlRoot(props: FormControlRootProps): FormControlRootReturn;
|
|
65
|
+
/**
|
|
66
|
+
* Reports focus/active state from `Input` / `Select.Trigger` to the parent `Field` for label styling.
|
|
67
|
+
* No-ops outside a `Field` (no `setIsLabelFocused` on context).
|
|
68
|
+
*/
|
|
69
|
+
export declare function useReportFormControlLabelFocus(isActive: boolean): void;
|
|
60
70
|
/**
|
|
61
71
|
* Hook that merges local field props with the nearest form field context
|
|
62
72
|
* and returns normalised HTML / ARIA attributes for input elements.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useFormControl.d.ts","sourceRoot":"","sources":["../../../src/form-control/useFormControl.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAY,KAAK,SAAS,EAAE,MAAM,cAAc,CAAC;AAGxD,KAAK,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAEzC,MAAM,WAAW,gBAAgB;IAC/B,UAAU,EAAE,OAAO,CAAC;IACpB,SAAS,EAAE,OAAO,CAAC;IACnB,UAAU,EAAE,OAAO,CAAC;IACpB,UAAU,EAAE,OAAO,CAAC;CACrB;AAED,MAAM,WAAW,uBAAwB,SAAQ,gBAAgB;IAC/D,eAAe,EAAE,OAAO,CAAC;IACzB,WAAW,EAAE,OAAO,CAAC;IACrB,kBAAkB,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC,CAAC;IAClE,cAAc,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC,CAAC;IAC9D,IAAI,CAA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CAAC,EAAE,MAAM,IAAI,CAAC;IACxB,cAAc,EAAE,OAAO,CAAC;IACxB,iBAAiB,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC,CAAC;CAClE;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,oBAAoB,GAAG,qBAAqB,CAsDrF;AAED;;;GAGG;AACH,wBAAgB,8BAA8B,CAAC,QAAQ,EAAE,OAAO,QAY/D;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,qBAAqB;;;;;;;;;;;EA4B1D;AAED,eAAO,MAAM,qBAAqB,wCAEjC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cdx-ui/utils",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.61",
|
|
4
4
|
"main": "lib/commonjs/index.js",
|
|
5
5
|
"module": "lib/module/index.js",
|
|
6
6
|
"react-native": "src/index.ts",
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"react-native-builder-bob": {
|
|
23
23
|
"source": "src",
|
|
24
24
|
"output": "lib",
|
|
25
|
+
"exclude": "**/{__tests__/**,__fixtures__/**,__mocks__/**,*.test.ts,*.test.tsx,*.spec.ts,*.spec.tsx}",
|
|
25
26
|
"targets": [
|
|
26
27
|
"commonjs",
|
|
27
28
|
"module",
|
|
@@ -67,6 +68,7 @@
|
|
|
67
68
|
},
|
|
68
69
|
"scripts": {
|
|
69
70
|
"build": "bob build",
|
|
70
|
-
"lint": "eslint src/"
|
|
71
|
+
"lint": "eslint src/",
|
|
72
|
+
"test": "jest --config ../../jest.config.js --selectProjects utils"
|
|
71
73
|
}
|
|
72
74
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ariaAttr } from './accessibilityUtils';
|
|
2
|
+
|
|
3
|
+
describe('ariaAttr', () => {
|
|
4
|
+
it('returns true when condition is true', () => {
|
|
5
|
+
expect(ariaAttr(true)).toBe(true);
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
it('returns undefined when condition is false or undefined', () => {
|
|
9
|
+
expect(ariaAttr(false)).toBeUndefined();
|
|
10
|
+
expect(ariaAttr(undefined)).toBeUndefined();
|
|
11
|
+
});
|
|
12
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { cn } from './cn';
|
|
2
|
+
|
|
3
|
+
describe('cn', () => {
|
|
4
|
+
it('merges conflicting Tailwind classes', () => {
|
|
5
|
+
expect(cn('px-2', 'px-4')).toBe('px-4');
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
it('joins non-conflicting classes', () => {
|
|
9
|
+
expect(cn('px-2', 'py-1')).toBe('px-2 py-1');
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('ignores falsy inputs', () => {
|
|
13
|
+
expect(cn('foo', undefined, null, false, '')).toBe('foo');
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('returns empty string when called with no inputs', () => {
|
|
17
|
+
expect(cn()).toBe('');
|
|
18
|
+
});
|
|
19
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { composeEventHandlers } from './composeEventHandlers';
|
|
2
|
+
|
|
3
|
+
describe('composeEventHandlers', () => {
|
|
4
|
+
it('calls handlers in order', () => {
|
|
5
|
+
const order: number[] = [];
|
|
6
|
+
const first = jest.fn(() => {
|
|
7
|
+
order.push(1);
|
|
8
|
+
});
|
|
9
|
+
const second = jest.fn(() => {
|
|
10
|
+
order.push(2);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
composeEventHandlers(first, second)({ type: 'press' });
|
|
14
|
+
|
|
15
|
+
expect(order).toEqual([1, 2]);
|
|
16
|
+
expect(first).toHaveBeenCalledWith({ type: 'press' });
|
|
17
|
+
expect(second).toHaveBeenCalledWith({ type: 'press' });
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('skips null and undefined handlers', () => {
|
|
21
|
+
const handler = jest.fn();
|
|
22
|
+
|
|
23
|
+
composeEventHandlers(null, undefined, handler)({});
|
|
24
|
+
|
|
25
|
+
expect(handler).toHaveBeenCalledTimes(1);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('returns a no-op when no handlers are provided', () => {
|
|
29
|
+
const handleEvent = composeEventHandlers();
|
|
30
|
+
|
|
31
|
+
expect(() => {
|
|
32
|
+
handleEvent({});
|
|
33
|
+
}).not.toThrow();
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import React, { isValidElement } from 'react';
|
|
2
|
+
import { Text } from 'react-native';
|
|
3
|
+
import { flattenChildren } from './getSpacedChild';
|
|
4
|
+
|
|
5
|
+
describe('flattenChildren', () => {
|
|
6
|
+
it('flattens fragment children', () => {
|
|
7
|
+
const result = flattenChildren(
|
|
8
|
+
<>
|
|
9
|
+
<Text key="a">A</Text>
|
|
10
|
+
<Text key="b">B</Text>
|
|
11
|
+
</>,
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
expect(result).toHaveLength(2);
|
|
15
|
+
expect(isValidElement(result[0]) && result[0].type).toBe(Text);
|
|
16
|
+
expect(isValidElement(result[1]) && result[1].type).toBe(Text);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('assigns composite keys to nested fragment children', () => {
|
|
20
|
+
const result = flattenChildren(
|
|
21
|
+
<React.Fragment key="group">
|
|
22
|
+
<Text key="child">Child</Text>
|
|
23
|
+
</React.Fragment>,
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
expect(result).toHaveLength(1);
|
|
27
|
+
const key = isValidElement(result[0]) ? String(result[0].key) : '';
|
|
28
|
+
expect(key).toContain('group');
|
|
29
|
+
expect(key).toContain('child');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('passes through non-element children', () => {
|
|
33
|
+
const result = flattenChildren(['plain', <Text key="t">T</Text>]);
|
|
34
|
+
|
|
35
|
+
expect(result).toHaveLength(2);
|
|
36
|
+
expect(result[0]).toBe('plain');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('preserves explicit element keys', () => {
|
|
40
|
+
const result = flattenChildren(<Text key="explicit">Label</Text>);
|
|
41
|
+
|
|
42
|
+
expect(result).toHaveLength(1);
|
|
43
|
+
const key = isValidElement(result[0]) ? String(result[0].key) : '';
|
|
44
|
+
expect(key).toContain('explicit');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('uses index keys when fragment and child keys are missing', () => {
|
|
48
|
+
const result = flattenChildren(
|
|
49
|
+
<>
|
|
50
|
+
<Text>First</Text>
|
|
51
|
+
<React.Fragment>
|
|
52
|
+
<Text>Nested</Text>
|
|
53
|
+
</React.Fragment>
|
|
54
|
+
</>,
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
expect(result).toHaveLength(2);
|
|
58
|
+
const firstKey = isValidElement(result[0]) ? String(result[0].key) : '';
|
|
59
|
+
const secondKey = isValidElement(result[1]) ? String(result[1].key) : '';
|
|
60
|
+
expect(firstKey).toContain('0');
|
|
61
|
+
expect(secondKey).toContain('1');
|
|
62
|
+
expect(secondKey).toContain('0');
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { createRef } from 'react';
|
|
2
|
+
import { mergeRefs } from './mergeRefs';
|
|
3
|
+
|
|
4
|
+
describe('mergeRefs', () => {
|
|
5
|
+
it('assigns value to object refs', () => {
|
|
6
|
+
const refA = createRef<string>();
|
|
7
|
+
const refB = createRef<string>();
|
|
8
|
+
|
|
9
|
+
mergeRefs(refA, refB)('value');
|
|
10
|
+
|
|
11
|
+
expect(refA.current).toBe('value');
|
|
12
|
+
expect(refB.current).toBe('value');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('invokes callback refs', () => {
|
|
16
|
+
const callback = jest.fn();
|
|
17
|
+
|
|
18
|
+
mergeRefs(callback)('value');
|
|
19
|
+
|
|
20
|
+
expect(callback).toHaveBeenCalledWith('value');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('skips undefined refs and assigns null', () => {
|
|
24
|
+
const ref = createRef<string | null>();
|
|
25
|
+
|
|
26
|
+
mergeRefs(undefined, ref)(null);
|
|
27
|
+
|
|
28
|
+
expect(ref.current).toBeNull();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('updates callback and object refs together', () => {
|
|
32
|
+
const callback = jest.fn();
|
|
33
|
+
const objectRef = createRef<string | null>();
|
|
34
|
+
|
|
35
|
+
mergeRefs(callback, objectRef)('shared');
|
|
36
|
+
|
|
37
|
+
expect(callback).toHaveBeenCalledWith('shared');
|
|
38
|
+
expect(objectRef.current).toBe('shared');
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { act, renderHook } from '@testing-library/react-native';
|
|
2
|
+
import { useControllableState } from './useControllableState';
|
|
3
|
+
|
|
4
|
+
describe('useControllableState', () => {
|
|
5
|
+
it('manages uncontrolled state from defaultProp', () => {
|
|
6
|
+
const onChange = jest.fn();
|
|
7
|
+
const { result } = renderHook(() => useControllableState({ defaultProp: 'initial', onChange }));
|
|
8
|
+
|
|
9
|
+
expect(result.current[0]).toBe('initial');
|
|
10
|
+
|
|
11
|
+
act(() => {
|
|
12
|
+
result.current[1]('next');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
expect(result.current[0]).toBe('next');
|
|
16
|
+
expect(onChange).toHaveBeenCalledWith('next');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('uses prop value in controlled mode and notifies onChange', () => {
|
|
20
|
+
const onChange = jest.fn();
|
|
21
|
+
const { result } = renderHook(() => useControllableState({ prop: 'controlled', onChange }));
|
|
22
|
+
|
|
23
|
+
expect(result.current[0]).toBe('controlled');
|
|
24
|
+
|
|
25
|
+
act(() => {
|
|
26
|
+
result.current[1]('updated');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
expect(onChange).toHaveBeenCalledWith('updated');
|
|
30
|
+
expect(result.current[0]).toBe('controlled');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('supports function updaters in uncontrolled mode', () => {
|
|
34
|
+
const { result } = renderHook(() => useControllableState({ defaultProp: 1 }));
|
|
35
|
+
|
|
36
|
+
act(() => {
|
|
37
|
+
result.current[1]((prev: number | undefined) => (prev ?? 0) + 1);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
expect(result.current[0]).toBe(2);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('follows prop updates in controlled mode', () => {
|
|
44
|
+
const { result, rerender } = renderHook(({ value }) => useControllableState({ prop: value }), {
|
|
45
|
+
initialProps: { value: 'a' },
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
rerender({ value: 'b' });
|
|
49
|
+
|
|
50
|
+
expect(result.current[0]).toBe('b');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('supports function updaters in controlled mode', () => {
|
|
54
|
+
const onChange = jest.fn();
|
|
55
|
+
const { result } = renderHook(() => useControllableState({ prop: 2, onChange }));
|
|
56
|
+
|
|
57
|
+
act(() => {
|
|
58
|
+
result.current[1]((prev: number | undefined) => (prev ?? 0) + 3);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
expect(onChange).toHaveBeenCalledWith(5);
|
|
62
|
+
expect(result.current[0]).toBe(2);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('does not notify onChange when controlled value is unchanged', () => {
|
|
66
|
+
const onChange = jest.fn();
|
|
67
|
+
const { result } = renderHook(() => useControllableState({ prop: 'same', onChange }));
|
|
68
|
+
|
|
69
|
+
act(() => {
|
|
70
|
+
result.current[1]('same');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('switches from uncontrolled to controlled when prop is provided', () => {
|
|
77
|
+
const { result, rerender } = renderHook(
|
|
78
|
+
(props: { prop?: string; defaultProp?: string }) => useControllableState(props),
|
|
79
|
+
{ initialProps: { defaultProp: 'internal' } },
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
act(() => {
|
|
83
|
+
result.current[1]('internal');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
rerender({ prop: 'external' });
|
|
87
|
+
|
|
88
|
+
expect(result.current[0]).toBe('external');
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import { renderHook } from '@testing-library/react-native';
|
|
3
|
+
import { createContext } from './createContext';
|
|
4
|
+
|
|
5
|
+
describe('createContext', () => {
|
|
6
|
+
it('provides value through Provider and consumer hook', () => {
|
|
7
|
+
const [Provider, useTestContext] = createContext<{ label: string }>('Test');
|
|
8
|
+
|
|
9
|
+
const wrapper = ({ children }: { children: ReactNode }) => (
|
|
10
|
+
<Provider value={{ label: 'hello' }}>{children}</Provider>
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
const { result } = renderHook(() => useTestContext(), { wrapper });
|
|
14
|
+
|
|
15
|
+
expect(result.current).toEqual({ label: 'hello' });
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('throws when consumer is used outside Provider', () => {
|
|
19
|
+
const [, useTestContext] = createContext<{ label: string }>('Test');
|
|
20
|
+
|
|
21
|
+
expect(() => {
|
|
22
|
+
renderHook(() => useTestContext());
|
|
23
|
+
}).toThrow('useTest must be used within a TestProvider');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('names the Provider from the display name argument', () => {
|
|
27
|
+
const [Provider] = createContext<{ label: string }>('Field');
|
|
28
|
+
|
|
29
|
+
expect(Provider.displayName).toBe('FieldProvider');
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import { Platform, type TextInput } from 'react-native';
|
|
3
|
+
import { act, renderHook } from '@testing-library/react-native';
|
|
4
|
+
import {
|
|
5
|
+
FormControlContext,
|
|
6
|
+
useFormControl,
|
|
7
|
+
useFormControlContext,
|
|
8
|
+
useFormControlRoot,
|
|
9
|
+
useReportFormControlLabelFocus,
|
|
10
|
+
} from './useFormControl';
|
|
11
|
+
|
|
12
|
+
function FormControlTestProvider({
|
|
13
|
+
children,
|
|
14
|
+
rootProps,
|
|
15
|
+
hasFeedbackText = false,
|
|
16
|
+
hasHelpText = false,
|
|
17
|
+
}: {
|
|
18
|
+
children: ReactNode;
|
|
19
|
+
rootProps?: Parameters<typeof useFormControlRoot>[0];
|
|
20
|
+
hasFeedbackText?: boolean;
|
|
21
|
+
hasHelpText?: boolean;
|
|
22
|
+
}) {
|
|
23
|
+
const root = useFormControlRoot(rootProps ?? { id: 'email' });
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<FormControlContext.Provider value={{ ...root, hasFeedbackText, hasHelpText }}>
|
|
27
|
+
{children}
|
|
28
|
+
</FormControlContext.Provider>
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe('useFormControlRoot', () => {
|
|
33
|
+
it('uses provided id and coerces boolean state flags', () => {
|
|
34
|
+
const { result } = renderHook(() =>
|
|
35
|
+
useFormControlRoot({
|
|
36
|
+
id: 'custom-id',
|
|
37
|
+
isRequired: true,
|
|
38
|
+
isInvalid: true,
|
|
39
|
+
isDisabled: false,
|
|
40
|
+
isReadOnly: false,
|
|
41
|
+
name: 'email',
|
|
42
|
+
'data-test': 'root',
|
|
43
|
+
}),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
expect(result.current.id).toBe('custom-id');
|
|
47
|
+
expect(result.current.labelId).toBe('custom-id-label');
|
|
48
|
+
expect(result.current.isRequired).toBe(true);
|
|
49
|
+
expect(result.current.isInvalid).toBe(true);
|
|
50
|
+
expect(result.current.isDisabled).toBe(false);
|
|
51
|
+
expect(result.current.isReadOnly).toBe(false);
|
|
52
|
+
expect(result.current.name).toBe('email');
|
|
53
|
+
expect(result.current.htmlProps).toEqual({ 'data-test': 'root' });
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('generates an id when none is provided', () => {
|
|
57
|
+
const { result } = renderHook(() => useFormControlRoot({}));
|
|
58
|
+
|
|
59
|
+
expect(result.current.id).toMatch(/^field-/);
|
|
60
|
+
expect(result.current.feedbackId).toBe(`${result.current.id}-feedback`);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('ignores empty string id and generates a fallback', () => {
|
|
64
|
+
const { result } = renderHook(() => useFormControlRoot({ id: '' }));
|
|
65
|
+
|
|
66
|
+
expect(result.current.id).toMatch(/^field-/);
|
|
67
|
+
expect(result.current.id).not.toBe('');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('exposes native focus helpers by default', () => {
|
|
71
|
+
const { result } = renderHook(() => useFormControlRoot({}));
|
|
72
|
+
|
|
73
|
+
expect(result.current.inputRef).toBeDefined();
|
|
74
|
+
expect(result.current.focusInput).toBeDefined();
|
|
75
|
+
|
|
76
|
+
const { inputRef, focusInput } = result.current;
|
|
77
|
+
if (!inputRef || !focusInput) {
|
|
78
|
+
throw new Error('expected native focus helpers');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const focus = jest.fn();
|
|
82
|
+
act(() => {
|
|
83
|
+
inputRef.current = { focus } as TextInput;
|
|
84
|
+
focusInput();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
expect(focus).toHaveBeenCalled();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('no-ops focusInput when ref is empty or focus is unavailable', () => {
|
|
91
|
+
const { result } = renderHook(() => useFormControlRoot({}));
|
|
92
|
+
const { focusInput, inputRef } = result.current;
|
|
93
|
+
|
|
94
|
+
if (!focusInput || !inputRef) {
|
|
95
|
+
throw new Error('expected native focus helpers');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
expect(() => {
|
|
99
|
+
focusInput();
|
|
100
|
+
}).not.toThrow();
|
|
101
|
+
|
|
102
|
+
act(() => {
|
|
103
|
+
inputRef.current = {} as TextInput;
|
|
104
|
+
focusInput();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
expect(inputRef.current).toEqual({});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('omits native focus helpers on web', () => {
|
|
111
|
+
const originalDescriptor = Object.getOwnPropertyDescriptor(Platform, 'OS');
|
|
112
|
+
Object.defineProperty(Platform, 'OS', { configurable: true, get: () => 'web' });
|
|
113
|
+
|
|
114
|
+
const { result } = renderHook(() => useFormControlRoot({}));
|
|
115
|
+
|
|
116
|
+
expect(result.current.inputRef).toBeUndefined();
|
|
117
|
+
expect(result.current.focusInput).toBeUndefined();
|
|
118
|
+
|
|
119
|
+
if (originalDescriptor) {
|
|
120
|
+
Object.defineProperty(Platform, 'OS', originalDescriptor);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe('useFormControl', () => {
|
|
126
|
+
it('merges local props with context and composes aria-describedby', () => {
|
|
127
|
+
const { result } = renderHook(() => useFormControl({ id: 'override', isDisabled: true }), {
|
|
128
|
+
wrapper: ({ children }) => (
|
|
129
|
+
<FormControlTestProvider
|
|
130
|
+
rootProps={{
|
|
131
|
+
id: 'email',
|
|
132
|
+
name: 'email',
|
|
133
|
+
isRequired: true,
|
|
134
|
+
isInvalid: true,
|
|
135
|
+
isDisabled: false,
|
|
136
|
+
isReadOnly: false,
|
|
137
|
+
}}
|
|
138
|
+
hasFeedbackText
|
|
139
|
+
hasHelpText
|
|
140
|
+
>
|
|
141
|
+
{children}
|
|
142
|
+
</FormControlTestProvider>
|
|
143
|
+
),
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
expect(result.current.id).toBe('override');
|
|
147
|
+
expect(result.current.name).toBe('email');
|
|
148
|
+
expect(result.current.disabled).toBe(true);
|
|
149
|
+
expect(result.current.required).toBe(true);
|
|
150
|
+
expect(result.current['aria-invalid']).toBe(true);
|
|
151
|
+
expect(result.current['aria-labelledby']).toBe('email-label');
|
|
152
|
+
expect(result.current['aria-describedby']).toBe('email-feedback email-helptext');
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('returns undefined aria-describedby when no help or feedback is present', () => {
|
|
156
|
+
const { result } = renderHook(() => useFormControl({}), {
|
|
157
|
+
wrapper: ({ children }) => <FormControlTestProvider>{children}</FormControlTestProvider>,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
expect(result.current['aria-describedby']).toBeUndefined();
|
|
161
|
+
expect(result.current.id).toBe('email-input');
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('composes aria-describedby from feedback or help text alone', () => {
|
|
165
|
+
const feedbackOnly = renderHook(() => useFormControl({}), {
|
|
166
|
+
wrapper: ({ children }) => (
|
|
167
|
+
<FormControlTestProvider hasFeedbackText>{children}</FormControlTestProvider>
|
|
168
|
+
),
|
|
169
|
+
}).result.current['aria-describedby'];
|
|
170
|
+
|
|
171
|
+
const helpOnly = renderHook(() => useFormControl({}), {
|
|
172
|
+
wrapper: ({ children }) => (
|
|
173
|
+
<FormControlTestProvider hasHelpText>{children}</FormControlTestProvider>
|
|
174
|
+
),
|
|
175
|
+
}).result.current['aria-describedby'];
|
|
176
|
+
|
|
177
|
+
expect(feedbackOnly).toBe('email-feedback');
|
|
178
|
+
expect(helpOnly).toBe('email-helptext');
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('merges readOnly from local props and omits id without field context', () => {
|
|
182
|
+
const { result } = renderHook(() => useFormControl({ isReadOnly: true }));
|
|
183
|
+
|
|
184
|
+
expect(result.current.readOnly).toBe(true);
|
|
185
|
+
expect(result.current.id).toBeUndefined();
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
describe('useReportFormControlLabelFocus', () => {
|
|
190
|
+
it('reports active state to the form control context', () => {
|
|
191
|
+
const { result, rerender } = renderHook(
|
|
192
|
+
({ active }: { active: boolean }) => {
|
|
193
|
+
useReportFormControlLabelFocus(active);
|
|
194
|
+
return useFormControlContext().isLabelFocused;
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
wrapper: ({ children }) => <FormControlTestProvider>{children}</FormControlTestProvider>,
|
|
198
|
+
initialProps: { active: true },
|
|
199
|
+
},
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
expect(result.current).toBe(true);
|
|
203
|
+
|
|
204
|
+
rerender({ active: false });
|
|
205
|
+
|
|
206
|
+
expect(result.current).toBe(false);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('no-ops when setIsLabelFocused is unavailable', () => {
|
|
210
|
+
expect(() => {
|
|
211
|
+
renderHook(() => useReportFormControlLabelFocus(true));
|
|
212
|
+
}).not.toThrow();
|
|
213
|
+
});
|
|
214
|
+
});
|
|
@@ -28,6 +28,9 @@ export interface FormControlContextValue extends FormControlState {
|
|
|
28
28
|
*/
|
|
29
29
|
inputRef?: React.RefObject<TextInput | null>;
|
|
30
30
|
focusInput?: () => void;
|
|
31
|
+
/** True when the primary child control is focused or (for Select) open — drives `Field.Label` styling. */
|
|
32
|
+
isLabelFocused?: boolean;
|
|
33
|
+
setIsLabelFocused?: React.Dispatch<React.SetStateAction<boolean>>;
|
|
31
34
|
}
|
|
32
35
|
|
|
33
36
|
export const FormControlContext = React.createContext<Partial<FormControlContextValue>>({});
|
|
@@ -59,6 +62,8 @@ export interface FormControlRootReturn {
|
|
|
59
62
|
htmlProps: HTMLProps;
|
|
60
63
|
inputRef?: React.RefObject<TextInput | null>;
|
|
61
64
|
focusInput?: () => void;
|
|
65
|
+
isLabelFocused: boolean;
|
|
66
|
+
setIsLabelFocused: React.Dispatch<React.SetStateAction<boolean>>;
|
|
62
67
|
}
|
|
63
68
|
|
|
64
69
|
/**
|
|
@@ -87,6 +92,8 @@ export function useFormControlRoot(props: FormControlRootProps): FormControlRoot
|
|
|
87
92
|
*/
|
|
88
93
|
const [hasHelpText, setHasHelpText] = React.useState(false);
|
|
89
94
|
|
|
95
|
+
const [isLabelFocused, setIsLabelFocused] = React.useState(false);
|
|
96
|
+
|
|
90
97
|
const inputRef = React.useRef<TextInput | null>(null);
|
|
91
98
|
|
|
92
99
|
const focusInput = React.useCallback(() => {
|
|
@@ -111,12 +118,32 @@ export function useFormControlRoot(props: FormControlRootProps): FormControlRoot
|
|
|
111
118
|
feedbackId,
|
|
112
119
|
helpTextId,
|
|
113
120
|
htmlProps,
|
|
121
|
+
isLabelFocused,
|
|
122
|
+
setIsLabelFocused,
|
|
114
123
|
...(Platform.OS !== 'web' ? { inputRef, focusInput } : {}),
|
|
115
124
|
};
|
|
116
125
|
|
|
117
126
|
return context;
|
|
118
127
|
}
|
|
119
128
|
|
|
129
|
+
/**
|
|
130
|
+
* Reports focus/active state from `Input` / `Select.Trigger` to the parent `Field` for label styling.
|
|
131
|
+
* No-ops outside a `Field` (no `setIsLabelFocused` on context).
|
|
132
|
+
*/
|
|
133
|
+
export function useReportFormControlLabelFocus(isActive: boolean) {
|
|
134
|
+
const { setIsLabelFocused } = useFormControlContext();
|
|
135
|
+
|
|
136
|
+
React.useLayoutEffect(() => {
|
|
137
|
+
if (typeof setIsLabelFocused !== 'function') {
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
setIsLabelFocused(isActive);
|
|
141
|
+
return () => {
|
|
142
|
+
setIsLabelFocused(false);
|
|
143
|
+
};
|
|
144
|
+
}, [isActive, setIsLabelFocused]);
|
|
145
|
+
}
|
|
146
|
+
|
|
120
147
|
/**
|
|
121
148
|
* Hook that merges local field props with the nearest form field context
|
|
122
149
|
* and returns normalised HTML / ARIA attributes for input elements.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import { renderHook } from '@testing-library/react-native';
|
|
3
|
+
import { ParentContext, useParentContext, useStyleContext } from './index';
|
|
4
|
+
|
|
5
|
+
describe('style context hooks', () => {
|
|
6
|
+
it('reads parent context values', () => {
|
|
7
|
+
const wrapper = ({ children }: { children: ReactNode }) => (
|
|
8
|
+
<ParentContext.Provider value={{ Button: { size: 'lg' }, Card: { padded: true } }}>
|
|
9
|
+
{children}
|
|
10
|
+
</ParentContext.Provider>
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
const { result: parentResult } = renderHook(() => useParentContext(), { wrapper });
|
|
14
|
+
const { result: styleResult } = renderHook(() => useStyleContext('Button'), { wrapper });
|
|
15
|
+
|
|
16
|
+
expect(parentResult.current).toEqual({ Button: { size: 'lg' }, Card: { padded: true } });
|
|
17
|
+
expect(styleResult.current).toEqual({ size: 'lg' });
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('defaults to Global scope', () => {
|
|
21
|
+
const wrapper = ({ children }: { children: ReactNode }) => (
|
|
22
|
+
<ParentContext.Provider value={{ Global: { theme: 'dark' } }}>
|
|
23
|
+
{children}
|
|
24
|
+
</ParentContext.Provider>
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
const { result } = renderHook(() => useStyleContext(), { wrapper });
|
|
28
|
+
|
|
29
|
+
expect(result.current).toEqual({ theme: 'dark' });
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Text, View } from 'react-native';
|
|
2
|
+
import { render, screen } from '@testing-library/react-native';
|
|
3
|
+
import { useStyleContext } from '../context';
|
|
4
|
+
import { withStyleContext } from './index';
|
|
5
|
+
|
|
6
|
+
const StyledView = withStyleContext(View, 'Button');
|
|
7
|
+
|
|
8
|
+
function StyleConsumer() {
|
|
9
|
+
const context = useStyleContext('Button');
|
|
10
|
+
return <Text testID="style-context">{JSON.stringify(context)}</Text>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
describe('withStyleContext', () => {
|
|
14
|
+
it('provides scoped context to descendants', () => {
|
|
15
|
+
render(
|
|
16
|
+
<StyledView context={{ size: 'lg' }} testID="wrapped">
|
|
17
|
+
<StyleConsumer />
|
|
18
|
+
</StyledView>,
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
expect(screen.getByTestId('style-context')).toHaveTextContent('{"size":"lg"}');
|
|
22
|
+
expect(screen.getByTestId('wrapped')).toBeTruthy();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('inherits parent context for other scopes', () => {
|
|
26
|
+
const Outer = withStyleContext(View, 'Card');
|
|
27
|
+
|
|
28
|
+
render(
|
|
29
|
+
<Outer context={{ padded: true }} testID="outer">
|
|
30
|
+
<StyledView context={{ size: 'sm' }} testID="inner">
|
|
31
|
+
<StyleConsumer />
|
|
32
|
+
</StyledView>
|
|
33
|
+
</Outer>,
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
expect(screen.getByTestId('style-context')).toHaveTextContent('{"size":"sm"}');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('defaults to the Global scope when none is provided', () => {
|
|
40
|
+
const GlobalView = withStyleContext(View);
|
|
41
|
+
|
|
42
|
+
function GlobalConsumer() {
|
|
43
|
+
const context = useStyleContext('Global');
|
|
44
|
+
return <Text testID="global-context">{JSON.stringify(context)}</Text>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
render(
|
|
48
|
+
<GlobalView context={{ theme: 'dark' }} testID="global-wrapped">
|
|
49
|
+
<GlobalConsumer />
|
|
50
|
+
</GlobalView>,
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
expect(screen.getByTestId('global-context')).toHaveTextContent('{"theme":"dark"}');
|
|
54
|
+
});
|
|
55
|
+
});
|