@cdx-ui/primitives 0.0.1-beta.10 → 0.0.1-beta.12
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 +1 -0
- package/lib/commonjs/index.js +12 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/radio/context.js +14 -0
- package/lib/commonjs/radio/context.js.map +1 -0
- package/lib/commonjs/radio/createRadioGroup.js +66 -0
- package/lib/commonjs/radio/createRadioGroup.js.map +1 -0
- package/lib/commonjs/radio/createRadioIndicator.js +43 -0
- package/lib/commonjs/radio/createRadioIndicator.js.map +1 -0
- package/lib/commonjs/radio/createRadioLabel.js +38 -0
- package/lib/commonjs/radio/createRadioLabel.js.map +1 -0
- package/lib/commonjs/radio/createRadioRoot.js +95 -0
- package/lib/commonjs/radio/createRadioRoot.js.map +1 -0
- package/lib/commonjs/radio/createRadioRoot.web.js +87 -0
- package/lib/commonjs/radio/createRadioRoot.web.js.map +1 -0
- package/lib/commonjs/radio/index.js +26 -0
- package/lib/commonjs/radio/index.js.map +1 -0
- package/lib/commonjs/radio/types.js +6 -0
- package/lib/commonjs/radio/types.js.map +1 -0
- package/lib/commonjs/radio/useRadioRoot.js +64 -0
- package/lib/commonjs/radio/useRadioRoot.js.map +1 -0
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/radio/context.js +7 -0
- package/lib/module/radio/context.js.map +1 -0
- package/lib/module/radio/createRadioGroup.js +61 -0
- package/lib/module/radio/createRadioGroup.js.map +1 -0
- package/lib/module/radio/createRadioIndicator.js +38 -0
- package/lib/module/radio/createRadioIndicator.js.map +1 -0
- package/lib/module/radio/createRadioLabel.js +33 -0
- package/lib/module/radio/createRadioLabel.js.map +1 -0
- package/lib/module/radio/createRadioRoot.js +90 -0
- package/lib/module/radio/createRadioRoot.js.map +1 -0
- package/lib/module/radio/createRadioRoot.web.js +82 -0
- package/lib/module/radio/createRadioRoot.web.js.map +1 -0
- package/lib/module/radio/index.js +22 -0
- package/lib/module/radio/index.js.map +1 -0
- package/lib/module/radio/types.js +4 -0
- package/lib/module/radio/types.js.map +1 -0
- package/lib/module/radio/useRadioRoot.js +60 -0
- package/lib/module/radio/useRadioRoot.js.map +1 -0
- package/lib/typescript/index.d.ts +1 -0
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/radio/context.d.ts +21 -0
- package/lib/typescript/radio/context.d.ts.map +1 -0
- package/lib/typescript/radio/createRadioGroup.d.ts +3 -0
- package/lib/typescript/radio/createRadioGroup.d.ts.map +1 -0
- package/lib/typescript/radio/createRadioIndicator.d.ts +5 -0
- package/lib/typescript/radio/createRadioIndicator.d.ts.map +1 -0
- package/lib/typescript/radio/createRadioLabel.d.ts +5 -0
- package/lib/typescript/radio/createRadioLabel.d.ts.map +1 -0
- package/lib/typescript/radio/createRadioRoot.d.ts +3 -0
- package/lib/typescript/radio/createRadioRoot.d.ts.map +1 -0
- package/lib/typescript/radio/createRadioRoot.web.d.ts +3 -0
- package/lib/typescript/radio/createRadioRoot.web.d.ts.map +1 -0
- package/lib/typescript/radio/index.d.ts +10 -0
- package/lib/typescript/radio/index.d.ts.map +1 -0
- package/lib/typescript/radio/types.d.ts +54 -0
- package/lib/typescript/radio/types.d.ts.map +1 -0
- package/lib/typescript/radio/useRadioRoot.d.ts +149 -0
- package/lib/typescript/radio/useRadioRoot.d.ts.map +1 -0
- package/package.json +5 -2
- package/src/index.ts +1 -0
- package/src/radio/context.tsx +21 -0
- package/src/radio/createRadioGroup.tsx +67 -0
- package/src/radio/createRadioIndicator.tsx +32 -0
- package/src/radio/createRadioLabel.tsx +28 -0
- package/src/radio/createRadioRoot.tsx +100 -0
- package/src/radio/createRadioRoot.web.tsx +81 -0
- package/src/radio/index.ts +37 -0
- package/src/radio/types.ts +67 -0
- package/src/radio/useRadioRoot.ts +69 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { useContext, useRef } from 'react';
|
|
2
|
+
import { mergeRefs, useFormControlContext } from '@cdx-ui/utils';
|
|
3
|
+
import { useRadio } from '@react-native-aria/radio';
|
|
4
|
+
import { useHover } from '@react-native-aria/interactions';
|
|
5
|
+
import { RadioGroupContext } from './context';
|
|
6
|
+
import type { IRadioProps } from './types';
|
|
7
|
+
|
|
8
|
+
interface UseRadioRootOptions {
|
|
9
|
+
useInputRefForAria?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function useRadioRoot(
|
|
13
|
+
props: IRadioProps,
|
|
14
|
+
ref?: React.Ref<unknown>,
|
|
15
|
+
{ useInputRefForAria = false }: UseRadioRootOptions = {},
|
|
16
|
+
) {
|
|
17
|
+
const formControlContext = useFormControlContext();
|
|
18
|
+
|
|
19
|
+
const { isInvalid, isReadOnly, ...combinedProps } = {
|
|
20
|
+
...formControlContext,
|
|
21
|
+
...props,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const radioGroupContext = useContext(RadioGroupContext);
|
|
25
|
+
|
|
26
|
+
if (!radioGroupContext) {
|
|
27
|
+
throw new Error(
|
|
28
|
+
'Radio must be rendered inside a Radio.Group. Standalone Radio is not supported.',
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const rootRef = useRef(null);
|
|
33
|
+
const inputRef = useRef<HTMLInputElement | null>(null);
|
|
34
|
+
const mergedRootRef = mergeRefs(ref as any, rootRef as any);
|
|
35
|
+
const ariaLabel = combinedProps['aria-label'] || combinedProps.value || 'Radio';
|
|
36
|
+
|
|
37
|
+
const radio = useRadio(
|
|
38
|
+
{
|
|
39
|
+
...combinedProps,
|
|
40
|
+
'aria-label': ariaLabel,
|
|
41
|
+
isReadOnly: isReadOnly || radioGroupContext.state.isReadOnly,
|
|
42
|
+
isDisabled: combinedProps.isDisabled || radioGroupContext.state.isDisabled,
|
|
43
|
+
} as any,
|
|
44
|
+
radioGroupContext.radioGroupState,
|
|
45
|
+
(useInputRefForAria ? inputRef : rootRef) as any,
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
const { inputProps, labelProps } = radio as typeof radio & {
|
|
49
|
+
labelProps?: Record<string, unknown>;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const { checked: isChecked, disabled: isDisabled } = inputProps;
|
|
53
|
+
|
|
54
|
+
const { hoverProps, isHovered } = useHover({}, rootRef);
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
combinedProps,
|
|
58
|
+
isInvalid: isInvalid || radioGroupContext.state.isInvalid,
|
|
59
|
+
isReadOnly: isReadOnly || radioGroupContext.state.isReadOnly,
|
|
60
|
+
inputProps,
|
|
61
|
+
labelProps: labelProps ?? {},
|
|
62
|
+
isChecked,
|
|
63
|
+
isDisabled,
|
|
64
|
+
isHovered,
|
|
65
|
+
hoverProps,
|
|
66
|
+
mergedRef: mergedRootRef,
|
|
67
|
+
inputRef,
|
|
68
|
+
};
|
|
69
|
+
}
|