@choice-ui/react 1.8.8 → 1.8.9
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/dist/components/checkbox/dist/index.d.ts +10 -1
- package/dist/components/checkbox/dist/index.js +49 -5
- package/dist/components/checkbox/src/checkbox-icon.d.ts +8 -0
- package/dist/components/checkbox/src/checkbox-icon.js +41 -0
- package/dist/components/checkbox/src/checkbox.d.ts +2 -0
- package/dist/components/checkbox/src/checkbox.js +18 -5
- package/dist/components/checkbox/src/index.d.ts +2 -0
- package/dist/components/radio/dist/index.d.ts +9 -1
- package/dist/components/radio/dist/index.js +50 -6
- package/dist/components/radio/src/context.d.ts +2 -0
- package/dist/components/radio/src/index.d.ts +2 -0
- package/dist/components/radio/src/radio-icon.d.ts +7 -0
- package/dist/components/radio/src/radio-icon.js +41 -0
- package/dist/components/radio/src/radio.d.ts +2 -0
- package/dist/components/radio/src/radio.js +19 -6
- package/package.json +1 -1
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { HTMLProps, ReactNode } from 'react';
|
|
2
2
|
import * as react from 'react';
|
|
3
3
|
|
|
4
|
+
interface CheckboxIconProps extends Omit<HTMLProps<HTMLDivElement>, "children"> {
|
|
5
|
+
children?: ReactNode | ((props: {
|
|
6
|
+
value?: boolean;
|
|
7
|
+
mixed?: boolean;
|
|
8
|
+
}) => ReactNode);
|
|
9
|
+
}
|
|
10
|
+
declare const CheckboxIcon: react.MemoExoticComponent<react.ForwardRefExoticComponent<Omit<CheckboxIconProps, "ref"> & react.RefAttributes<HTMLDivElement>>>;
|
|
11
|
+
|
|
4
12
|
interface CheckboxLabelProps extends Omit<HTMLProps<HTMLLabelElement>, "htmlFor" | "id" | "disabled"> {
|
|
5
13
|
children: ReactNode;
|
|
6
14
|
}
|
|
@@ -20,9 +28,10 @@ interface CheckboxType {
|
|
|
20
28
|
(props: CheckboxProps & {
|
|
21
29
|
ref?: React.Ref<HTMLInputElement>;
|
|
22
30
|
}): JSX.Element;
|
|
31
|
+
Icon: typeof CheckboxIcon;
|
|
23
32
|
Label: typeof CheckboxLabel;
|
|
24
33
|
displayName?: string;
|
|
25
34
|
}
|
|
26
35
|
declare const Checkbox: CheckboxType;
|
|
27
36
|
|
|
28
|
-
export { Checkbox, type CheckboxProps };
|
|
37
|
+
export { Checkbox, type CheckboxIconProps, type CheckboxLabelProps, type CheckboxProps };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Indeterminate, Check } from "@choiceform/icons-react";
|
|
2
|
-
import { memo, forwardRef, useId, createContext, useContext } from "react";
|
|
2
|
+
import { memo, forwardRef, useId, Children, isValidElement, createContext, useContext } from "react";
|
|
3
3
|
import { useEventCallback } from "usehooks-ts";
|
|
4
4
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
5
5
|
import { tcv, tcx } from "../../../shared/utils/tcx/tcx.js";
|
|
@@ -137,6 +137,38 @@ var checkboxTv = tcv({
|
|
|
137
137
|
focused: false
|
|
138
138
|
}
|
|
139
139
|
});
|
|
140
|
+
var CheckboxIcon = memo(
|
|
141
|
+
forwardRef(function CheckboxIcon2(props, ref) {
|
|
142
|
+
const { className, children, ...rest } = props;
|
|
143
|
+
const { value, mixed, disabled, variant } = useCheckboxContext();
|
|
144
|
+
const tv = checkboxTv({
|
|
145
|
+
type: "checkbox",
|
|
146
|
+
variant,
|
|
147
|
+
disabled,
|
|
148
|
+
checked: value || mixed
|
|
149
|
+
});
|
|
150
|
+
const renderIcon = () => {
|
|
151
|
+
if (typeof children === "function") {
|
|
152
|
+
return children({ value, mixed });
|
|
153
|
+
}
|
|
154
|
+
if (children !== void 0) {
|
|
155
|
+
return children;
|
|
156
|
+
}
|
|
157
|
+
return mixed ? /* @__PURE__ */ jsx(Indeterminate, {}) : value ? /* @__PURE__ */ jsx(Check, {}) : null;
|
|
158
|
+
};
|
|
159
|
+
return /* @__PURE__ */ jsx(
|
|
160
|
+
"div",
|
|
161
|
+
{
|
|
162
|
+
ref,
|
|
163
|
+
className: tcx(tv.box(), className),
|
|
164
|
+
"data-active": value,
|
|
165
|
+
...rest,
|
|
166
|
+
children: renderIcon()
|
|
167
|
+
}
|
|
168
|
+
);
|
|
169
|
+
})
|
|
170
|
+
);
|
|
171
|
+
CheckboxIcon.displayName = "Checkbox.Icon";
|
|
140
172
|
var CheckboxLabel = memo(
|
|
141
173
|
forwardRef(function CheckboxLabel2(props, ref) {
|
|
142
174
|
const { children, className, ...rest } = props;
|
|
@@ -195,12 +227,23 @@ var CheckboxBase = forwardRef(function Checkbox(props, ref) {
|
|
|
195
227
|
}
|
|
196
228
|
onKeyDown == null ? void 0 : onKeyDown(e);
|
|
197
229
|
});
|
|
230
|
+
const isIconElement = (child) => {
|
|
231
|
+
var _a;
|
|
232
|
+
return isValidElement(child) && (child.type === CheckboxIcon || ((_a = child.type) == null ? void 0 : _a.displayName) === "Checkbox.Icon");
|
|
233
|
+
};
|
|
234
|
+
const childArray = Children.toArray(children);
|
|
235
|
+
const iconChild = childArray.find(isIconElement);
|
|
236
|
+
const otherChildren = childArray.filter((child) => !isIconElement(child));
|
|
198
237
|
const renderChildren = () => {
|
|
199
|
-
if (
|
|
200
|
-
|
|
238
|
+
if (otherChildren.length === 1) {
|
|
239
|
+
const child = otherChildren[0];
|
|
240
|
+
if (typeof child === "string" || typeof child === "number") {
|
|
241
|
+
return /* @__PURE__ */ jsx(CheckboxLabel, { children: child });
|
|
242
|
+
}
|
|
201
243
|
}
|
|
202
|
-
return
|
|
244
|
+
return otherChildren;
|
|
203
245
|
};
|
|
246
|
+
const renderDefaultIcon = () => /* @__PURE__ */ jsx("div", { className: tv.box(), children: mixed ? /* @__PURE__ */ jsx(Indeterminate, {}) : value ? /* @__PURE__ */ jsx(Check, {}) : null });
|
|
204
247
|
return /* @__PURE__ */ jsx(
|
|
205
248
|
CheckboxContext.Provider,
|
|
206
249
|
{
|
|
@@ -234,7 +277,7 @@ var CheckboxBase = forwardRef(function Checkbox(props, ref) {
|
|
|
234
277
|
...rest
|
|
235
278
|
}
|
|
236
279
|
),
|
|
237
|
-
|
|
280
|
+
iconChild ?? renderDefaultIcon()
|
|
238
281
|
] }),
|
|
239
282
|
renderChildren()
|
|
240
283
|
] })
|
|
@@ -243,6 +286,7 @@ var CheckboxBase = forwardRef(function Checkbox(props, ref) {
|
|
|
243
286
|
});
|
|
244
287
|
var MemoizedCheckbox = memo(CheckboxBase);
|
|
245
288
|
var Checkbox2 = MemoizedCheckbox;
|
|
289
|
+
Checkbox2.Icon = CheckboxIcon;
|
|
246
290
|
Checkbox2.Label = CheckboxLabel;
|
|
247
291
|
Checkbox2.displayName = "Checkbox";
|
|
248
292
|
export {
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { HTMLProps, ReactNode } from 'react';
|
|
2
|
+
export interface CheckboxIconProps extends Omit<HTMLProps<HTMLDivElement>, "children"> {
|
|
3
|
+
children?: ReactNode | ((props: {
|
|
4
|
+
value?: boolean;
|
|
5
|
+
mixed?: boolean;
|
|
6
|
+
}) => ReactNode);
|
|
7
|
+
}
|
|
8
|
+
export declare const CheckboxIcon: import('react').MemoExoticComponent<import('react').ForwardRefExoticComponent<Omit<CheckboxIconProps, "ref"> & import('react').RefAttributes<HTMLDivElement>>>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Indeterminate, Check } from "@choiceform/icons-react";
|
|
3
|
+
import { memo, forwardRef } from "react";
|
|
4
|
+
import { useCheckboxContext } from "./context.js";
|
|
5
|
+
import { checkboxTv } from "./tv.js";
|
|
6
|
+
import { tcx } from "../../../shared/utils/tcx/tcx.js";
|
|
7
|
+
const CheckboxIcon = memo(
|
|
8
|
+
forwardRef(function CheckboxIcon2(props, ref) {
|
|
9
|
+
const { className, children, ...rest } = props;
|
|
10
|
+
const { value, mixed, disabled, variant } = useCheckboxContext();
|
|
11
|
+
const tv = checkboxTv({
|
|
12
|
+
type: "checkbox",
|
|
13
|
+
variant,
|
|
14
|
+
disabled,
|
|
15
|
+
checked: value || mixed
|
|
16
|
+
});
|
|
17
|
+
const renderIcon = () => {
|
|
18
|
+
if (typeof children === "function") {
|
|
19
|
+
return children({ value, mixed });
|
|
20
|
+
}
|
|
21
|
+
if (children !== void 0) {
|
|
22
|
+
return children;
|
|
23
|
+
}
|
|
24
|
+
return mixed ? /* @__PURE__ */ jsx(Indeterminate, {}) : value ? /* @__PURE__ */ jsx(Check, {}) : null;
|
|
25
|
+
};
|
|
26
|
+
return /* @__PURE__ */ jsx(
|
|
27
|
+
"div",
|
|
28
|
+
{
|
|
29
|
+
ref,
|
|
30
|
+
className: tcx(tv.box(), className),
|
|
31
|
+
"data-active": value,
|
|
32
|
+
...rest,
|
|
33
|
+
children: renderIcon()
|
|
34
|
+
}
|
|
35
|
+
);
|
|
36
|
+
})
|
|
37
|
+
);
|
|
38
|
+
CheckboxIcon.displayName = "Checkbox.Icon";
|
|
39
|
+
export {
|
|
40
|
+
CheckboxIcon
|
|
41
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { HTMLProps, ReactNode } from 'react';
|
|
2
|
+
import { CheckboxIcon } from './checkbox-icon';
|
|
2
3
|
import { CheckboxLabel } from './checkbox-label';
|
|
3
4
|
export interface CheckboxProps extends Omit<HTMLProps<HTMLInputElement>, "value" | "onChange"> {
|
|
4
5
|
children?: ReactNode;
|
|
@@ -14,6 +15,7 @@ interface CheckboxType {
|
|
|
14
15
|
(props: CheckboxProps & {
|
|
15
16
|
ref?: React.Ref<HTMLInputElement>;
|
|
16
17
|
}): JSX.Element;
|
|
18
|
+
Icon: typeof CheckboxIcon;
|
|
17
19
|
Label: typeof CheckboxLabel;
|
|
18
20
|
displayName?: string;
|
|
19
21
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { Indeterminate, Check } from "@choiceform/icons-react";
|
|
3
|
-
import { memo, forwardRef, useId } from "react";
|
|
3
|
+
import { memo, forwardRef, useId, Children, isValidElement } from "react";
|
|
4
4
|
import { useEventCallback } from "usehooks-ts";
|
|
5
|
+
import { CheckboxIcon } from "./checkbox-icon.js";
|
|
5
6
|
import { CheckboxLabel } from "./checkbox-label.js";
|
|
6
7
|
import { CheckboxContext } from "./context.js";
|
|
7
8
|
import { checkboxTv } from "./tv.js";
|
|
@@ -45,12 +46,23 @@ const CheckboxBase = forwardRef(function Checkbox2(props, ref) {
|
|
|
45
46
|
}
|
|
46
47
|
onKeyDown == null ? void 0 : onKeyDown(e);
|
|
47
48
|
});
|
|
49
|
+
const isIconElement = (child) => {
|
|
50
|
+
var _a;
|
|
51
|
+
return isValidElement(child) && (child.type === CheckboxIcon || ((_a = child.type) == null ? void 0 : _a.displayName) === "Checkbox.Icon");
|
|
52
|
+
};
|
|
53
|
+
const childArray = Children.toArray(children);
|
|
54
|
+
const iconChild = childArray.find(isIconElement);
|
|
55
|
+
const otherChildren = childArray.filter((child) => !isIconElement(child));
|
|
48
56
|
const renderChildren = () => {
|
|
49
|
-
if (
|
|
50
|
-
|
|
57
|
+
if (otherChildren.length === 1) {
|
|
58
|
+
const child = otherChildren[0];
|
|
59
|
+
if (typeof child === "string" || typeof child === "number") {
|
|
60
|
+
return /* @__PURE__ */ jsx(CheckboxLabel, { children: child });
|
|
61
|
+
}
|
|
51
62
|
}
|
|
52
|
-
return
|
|
63
|
+
return otherChildren;
|
|
53
64
|
};
|
|
65
|
+
const renderDefaultIcon = () => /* @__PURE__ */ jsx("div", { className: tv.box(), children: mixed ? /* @__PURE__ */ jsx(Indeterminate, {}) : value ? /* @__PURE__ */ jsx(Check, {}) : null });
|
|
54
66
|
return /* @__PURE__ */ jsx(
|
|
55
67
|
CheckboxContext.Provider,
|
|
56
68
|
{
|
|
@@ -84,7 +96,7 @@ const CheckboxBase = forwardRef(function Checkbox2(props, ref) {
|
|
|
84
96
|
...rest
|
|
85
97
|
}
|
|
86
98
|
),
|
|
87
|
-
|
|
99
|
+
iconChild ?? renderDefaultIcon()
|
|
88
100
|
] }),
|
|
89
101
|
renderChildren()
|
|
90
102
|
] })
|
|
@@ -93,6 +105,7 @@ const CheckboxBase = forwardRef(function Checkbox2(props, ref) {
|
|
|
93
105
|
});
|
|
94
106
|
const MemoizedCheckbox = memo(CheckboxBase);
|
|
95
107
|
const Checkbox = MemoizedCheckbox;
|
|
108
|
+
Checkbox.Icon = CheckboxIcon;
|
|
96
109
|
Checkbox.Label = CheckboxLabel;
|
|
97
110
|
Checkbox.displayName = "Checkbox";
|
|
98
111
|
export {
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { HTMLProps, ReactNode } from 'react';
|
|
2
2
|
import * as react from 'react';
|
|
3
3
|
|
|
4
|
+
interface RadioIconProps extends Omit<HTMLProps<HTMLDivElement>, "children"> {
|
|
5
|
+
children?: ReactNode | ((props: {
|
|
6
|
+
value?: boolean;
|
|
7
|
+
}) => ReactNode);
|
|
8
|
+
}
|
|
9
|
+
declare const RadioIcon: react.MemoExoticComponent<react.ForwardRefExoticComponent<Omit<RadioIconProps, "ref"> & react.RefAttributes<HTMLDivElement>>>;
|
|
10
|
+
|
|
4
11
|
interface RadioLabelProps extends Omit<HTMLProps<HTMLLabelElement>, "htmlFor" | "id" | "disabled"> {
|
|
5
12
|
children: ReactNode;
|
|
6
13
|
}
|
|
@@ -19,6 +26,7 @@ interface RadioType {
|
|
|
19
26
|
(props: RadioProps & {
|
|
20
27
|
ref?: React.Ref<HTMLInputElement>;
|
|
21
28
|
}): JSX.Element;
|
|
29
|
+
Icon: typeof RadioIcon;
|
|
22
30
|
Label: typeof RadioLabel;
|
|
23
31
|
displayName?: string;
|
|
24
32
|
}
|
|
@@ -50,4 +58,4 @@ interface RadioGroupType {
|
|
|
50
58
|
}
|
|
51
59
|
declare const RadioGroup: RadioGroupType;
|
|
52
60
|
|
|
53
|
-
export { Radio, RadioGroup, type RadioGroupProps, type RadioProps };
|
|
61
|
+
export { Radio, RadioGroup, type RadioGroupProps, type RadioIconProps, type RadioLabelProps, type RadioProps };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Dot } from "@choiceform/icons-react";
|
|
2
|
-
import { memo, forwardRef, useId, useMemo, createContext, useContext } from "react";
|
|
2
|
+
import { memo, forwardRef, useId, useMemo, createContext, useContext, Children, isValidElement } from "react";
|
|
3
3
|
import { useEventCallback } from "usehooks-ts";
|
|
4
4
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
5
5
|
import { tcx, tcv } from "../../../shared/utils/tcx/tcx.js";
|
|
@@ -145,6 +145,38 @@ var radioTv = tcv({
|
|
|
145
145
|
focused: false
|
|
146
146
|
}
|
|
147
147
|
});
|
|
148
|
+
var RadioIcon = memo(
|
|
149
|
+
forwardRef(function RadioIcon2(props, ref) {
|
|
150
|
+
const { className, children, ...rest } = props;
|
|
151
|
+
const { value, disabled, variant } = useRadioContext();
|
|
152
|
+
const tv = radioTv({
|
|
153
|
+
type: "radio",
|
|
154
|
+
variant,
|
|
155
|
+
disabled,
|
|
156
|
+
checked: value
|
|
157
|
+
});
|
|
158
|
+
const renderIcon = () => {
|
|
159
|
+
if (typeof children === "function") {
|
|
160
|
+
return children({ value });
|
|
161
|
+
}
|
|
162
|
+
if (children !== void 0) {
|
|
163
|
+
return children;
|
|
164
|
+
}
|
|
165
|
+
return value ? /* @__PURE__ */ jsx(Dot, {}) : null;
|
|
166
|
+
};
|
|
167
|
+
return /* @__PURE__ */ jsx(
|
|
168
|
+
"div",
|
|
169
|
+
{
|
|
170
|
+
ref,
|
|
171
|
+
className: tcx(tv.box(), className),
|
|
172
|
+
"data-active": value,
|
|
173
|
+
...rest,
|
|
174
|
+
children: renderIcon()
|
|
175
|
+
}
|
|
176
|
+
);
|
|
177
|
+
})
|
|
178
|
+
);
|
|
179
|
+
RadioIcon.displayName = "Radio.Icon";
|
|
148
180
|
var RadioLabel = memo(
|
|
149
181
|
forwardRef(function RadioLabel2(props, ref) {
|
|
150
182
|
const { children, className, ...rest } = props;
|
|
@@ -201,13 +233,24 @@ var RadioBase = forwardRef(function Radio(props, ref) {
|
|
|
201
233
|
}
|
|
202
234
|
onKeyDown == null ? void 0 : onKeyDown(e);
|
|
203
235
|
});
|
|
236
|
+
const isIconElement = (child) => {
|
|
237
|
+
var _a;
|
|
238
|
+
return isValidElement(child) && (child.type === RadioIcon || ((_a = child.type) == null ? void 0 : _a.displayName) === "Radio.Icon");
|
|
239
|
+
};
|
|
240
|
+
const childArray = Children.toArray(children);
|
|
241
|
+
const iconChild = childArray.find(isIconElement);
|
|
242
|
+
const otherChildren = childArray.filter((child) => !isIconElement(child));
|
|
204
243
|
const renderChildren = () => {
|
|
205
|
-
if (
|
|
206
|
-
|
|
244
|
+
if (otherChildren.length === 1) {
|
|
245
|
+
const child = otherChildren[0];
|
|
246
|
+
if (typeof child === "string" || typeof child === "number") {
|
|
247
|
+
return /* @__PURE__ */ jsx(RadioLabel, { children: child });
|
|
248
|
+
}
|
|
207
249
|
}
|
|
208
|
-
return
|
|
250
|
+
return otherChildren;
|
|
209
251
|
};
|
|
210
|
-
|
|
252
|
+
const renderDefaultIcon = () => /* @__PURE__ */ jsx("div", { className: tv.box(), children: value && /* @__PURE__ */ jsx(Dot, {}) });
|
|
253
|
+
return /* @__PURE__ */ jsx(RadioContext.Provider, { value: { id, descriptionId, disabled, value, variant }, children: /* @__PURE__ */ jsxs("div", { className: tcx(tv.root(), className), children: [
|
|
211
254
|
/* @__PURE__ */ jsxs("div", { className: "pointer-events-none relative", children: [
|
|
212
255
|
/* @__PURE__ */ jsx(
|
|
213
256
|
"input",
|
|
@@ -229,13 +272,14 @@ var RadioBase = forwardRef(function Radio(props, ref) {
|
|
|
229
272
|
...rest
|
|
230
273
|
}
|
|
231
274
|
),
|
|
232
|
-
|
|
275
|
+
iconChild ?? renderDefaultIcon()
|
|
233
276
|
] }),
|
|
234
277
|
renderChildren()
|
|
235
278
|
] }) });
|
|
236
279
|
});
|
|
237
280
|
var MemoizedRadio = memo(RadioBase);
|
|
238
281
|
var Radio2 = MemoizedRadio;
|
|
282
|
+
Radio2.Icon = RadioIcon;
|
|
239
283
|
Radio2.Label = RadioLabel;
|
|
240
284
|
Radio2.displayName = "Radio";
|
|
241
285
|
var RadioGroupItem = memo(
|
|
@@ -2,6 +2,8 @@ export interface RadioContextType {
|
|
|
2
2
|
descriptionId: string;
|
|
3
3
|
disabled?: boolean;
|
|
4
4
|
id: string;
|
|
5
|
+
value?: boolean;
|
|
6
|
+
variant?: "default" | "accent" | "outline";
|
|
5
7
|
}
|
|
6
8
|
export declare const RadioContext: import('react').Context<RadioContextType | null>;
|
|
7
9
|
export declare function useRadioContext(): RadioContextType;
|
|
@@ -2,3 +2,5 @@ export { Radio } from './radio';
|
|
|
2
2
|
export { RadioGroup } from './radio-group';
|
|
3
3
|
export type { RadioProps } from './radio';
|
|
4
4
|
export type { RadioGroupProps } from './radio-group';
|
|
5
|
+
export type { RadioIconProps } from './radio-icon';
|
|
6
|
+
export type { RadioLabelProps } from './radio-label';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { HTMLProps, ReactNode } from 'react';
|
|
2
|
+
export interface RadioIconProps extends Omit<HTMLProps<HTMLDivElement>, "children"> {
|
|
3
|
+
children?: ReactNode | ((props: {
|
|
4
|
+
value?: boolean;
|
|
5
|
+
}) => ReactNode);
|
|
6
|
+
}
|
|
7
|
+
export declare const RadioIcon: import('react').MemoExoticComponent<import('react').ForwardRefExoticComponent<Omit<RadioIconProps, "ref"> & import('react').RefAttributes<HTMLDivElement>>>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Dot } from "@choiceform/icons-react";
|
|
3
|
+
import { memo, forwardRef } from "react";
|
|
4
|
+
import { useRadioContext } from "./context.js";
|
|
5
|
+
import { radioTv } from "./tv.js";
|
|
6
|
+
import { tcx } from "../../../shared/utils/tcx/tcx.js";
|
|
7
|
+
const RadioIcon = memo(
|
|
8
|
+
forwardRef(function RadioIcon2(props, ref) {
|
|
9
|
+
const { className, children, ...rest } = props;
|
|
10
|
+
const { value, disabled, variant } = useRadioContext();
|
|
11
|
+
const tv = radioTv({
|
|
12
|
+
type: "radio",
|
|
13
|
+
variant,
|
|
14
|
+
disabled,
|
|
15
|
+
checked: value
|
|
16
|
+
});
|
|
17
|
+
const renderIcon = () => {
|
|
18
|
+
if (typeof children === "function") {
|
|
19
|
+
return children({ value });
|
|
20
|
+
}
|
|
21
|
+
if (children !== void 0) {
|
|
22
|
+
return children;
|
|
23
|
+
}
|
|
24
|
+
return value ? /* @__PURE__ */ jsx(Dot, {}) : null;
|
|
25
|
+
};
|
|
26
|
+
return /* @__PURE__ */ jsx(
|
|
27
|
+
"div",
|
|
28
|
+
{
|
|
29
|
+
ref,
|
|
30
|
+
className: tcx(tv.box(), className),
|
|
31
|
+
"data-active": value,
|
|
32
|
+
...rest,
|
|
33
|
+
children: renderIcon()
|
|
34
|
+
}
|
|
35
|
+
);
|
|
36
|
+
})
|
|
37
|
+
);
|
|
38
|
+
RadioIcon.displayName = "Radio.Icon";
|
|
39
|
+
export {
|
|
40
|
+
RadioIcon
|
|
41
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { HTMLProps, ReactNode } from 'react';
|
|
2
|
+
import { RadioIcon } from './radio-icon';
|
|
2
3
|
import { RadioLabel } from './radio-label';
|
|
3
4
|
export interface RadioProps extends Omit<HTMLProps<HTMLInputElement>, "value" | "onChange"> {
|
|
4
5
|
children?: ReactNode;
|
|
@@ -13,6 +14,7 @@ interface RadioType {
|
|
|
13
14
|
(props: RadioProps & {
|
|
14
15
|
ref?: React.Ref<HTMLInputElement>;
|
|
15
16
|
}): JSX.Element;
|
|
17
|
+
Icon: typeof RadioIcon;
|
|
16
18
|
Label: typeof RadioLabel;
|
|
17
19
|
displayName?: string;
|
|
18
20
|
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { Dot } from "@choiceform/icons-react";
|
|
3
|
-
import { memo, forwardRef, useId } from "react";
|
|
3
|
+
import { memo, forwardRef, useId, Children, isValidElement } from "react";
|
|
4
4
|
import { useEventCallback } from "usehooks-ts";
|
|
5
5
|
import { RadioContext } from "./context.js";
|
|
6
|
+
import { RadioIcon } from "./radio-icon.js";
|
|
6
7
|
import { RadioLabel } from "./radio-label.js";
|
|
7
8
|
import { radioTv } from "./tv.js";
|
|
8
9
|
import { tcx } from "../../../shared/utils/tcx/tcx.js";
|
|
@@ -43,13 +44,24 @@ const RadioBase = forwardRef(function Radio2(props, ref) {
|
|
|
43
44
|
}
|
|
44
45
|
onKeyDown == null ? void 0 : onKeyDown(e);
|
|
45
46
|
});
|
|
47
|
+
const isIconElement = (child) => {
|
|
48
|
+
var _a;
|
|
49
|
+
return isValidElement(child) && (child.type === RadioIcon || ((_a = child.type) == null ? void 0 : _a.displayName) === "Radio.Icon");
|
|
50
|
+
};
|
|
51
|
+
const childArray = Children.toArray(children);
|
|
52
|
+
const iconChild = childArray.find(isIconElement);
|
|
53
|
+
const otherChildren = childArray.filter((child) => !isIconElement(child));
|
|
46
54
|
const renderChildren = () => {
|
|
47
|
-
if (
|
|
48
|
-
|
|
55
|
+
if (otherChildren.length === 1) {
|
|
56
|
+
const child = otherChildren[0];
|
|
57
|
+
if (typeof child === "string" || typeof child === "number") {
|
|
58
|
+
return /* @__PURE__ */ jsx(RadioLabel, { children: child });
|
|
59
|
+
}
|
|
49
60
|
}
|
|
50
|
-
return
|
|
61
|
+
return otherChildren;
|
|
51
62
|
};
|
|
52
|
-
|
|
63
|
+
const renderDefaultIcon = () => /* @__PURE__ */ jsx("div", { className: tv.box(), children: value && /* @__PURE__ */ jsx(Dot, {}) });
|
|
64
|
+
return /* @__PURE__ */ jsx(RadioContext.Provider, { value: { id, descriptionId, disabled, value, variant }, children: /* @__PURE__ */ jsxs("div", { className: tcx(tv.root(), className), children: [
|
|
53
65
|
/* @__PURE__ */ jsxs("div", { className: "pointer-events-none relative", children: [
|
|
54
66
|
/* @__PURE__ */ jsx(
|
|
55
67
|
"input",
|
|
@@ -71,13 +83,14 @@ const RadioBase = forwardRef(function Radio2(props, ref) {
|
|
|
71
83
|
...rest
|
|
72
84
|
}
|
|
73
85
|
),
|
|
74
|
-
|
|
86
|
+
iconChild ?? renderDefaultIcon()
|
|
75
87
|
] }),
|
|
76
88
|
renderChildren()
|
|
77
89
|
] }) });
|
|
78
90
|
});
|
|
79
91
|
const MemoizedRadio = memo(RadioBase);
|
|
80
92
|
const Radio = MemoizedRadio;
|
|
93
|
+
Radio.Icon = RadioIcon;
|
|
81
94
|
Radio.Label = RadioLabel;
|
|
82
95
|
Radio.displayName = "Radio";
|
|
83
96
|
export {
|
package/package.json
CHANGED