@yamada-ui/radio 0.1.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/LICENSE +21 -0
- package/README.md +28 -0
- package/dist/chunk-3SKWSFIA.mjs +133 -0
- package/dist/chunk-HR6MSMLG.mjs +263 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +392 -0
- package/dist/index.mjs +16 -0
- package/dist/radio-group.d.ts +48 -0
- package/dist/radio-group.js +152 -0
- package/dist/radio-group.mjs +10 -0
- package/dist/radio.d.ts +43 -0
- package/dist/radio.js +386 -0
- package/dist/radio.mjs +9 -0
- package/package.json +79 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
Radio: () => Radio,
|
|
24
|
+
RadioGroup: () => RadioGroup,
|
|
25
|
+
useRadio: () => useRadio,
|
|
26
|
+
useRadioGroup: () => useRadioGroup,
|
|
27
|
+
useRadioGroupContenxt: () => useRadioGroupContenxt
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(src_exports);
|
|
30
|
+
|
|
31
|
+
// src/radio.tsx
|
|
32
|
+
var import_core = require("@yamada-ui/core");
|
|
33
|
+
var import_form_control = require("@yamada-ui/form-control");
|
|
34
|
+
var import_use_focus_visible = require("@yamada-ui/use-focus-visible");
|
|
35
|
+
var import_utils = require("@yamada-ui/utils");
|
|
36
|
+
var import_react = require("react");
|
|
37
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
38
|
+
var useRadio = (props) => {
|
|
39
|
+
const { id, name, value, required, disabled, readOnly, ...rest } = (0, import_form_control.useFormControlProps)(props);
|
|
40
|
+
const [isFocusVisible, setIsFocusVisible] = (0, import_react.useState)(false);
|
|
41
|
+
const [isFocused, setFocused] = (0, import_react.useState)(false);
|
|
42
|
+
const [isHovered, setHovered] = (0, import_react.useState)(false);
|
|
43
|
+
const [isActive, setActive] = (0, import_react.useState)(false);
|
|
44
|
+
const [isChecked, setIsChecked] = (0, import_react.useState)(!!props.defaultChecked);
|
|
45
|
+
const isControlled = props.isChecked !== void 0;
|
|
46
|
+
const checked = isControlled ? props.isChecked : isChecked;
|
|
47
|
+
(0, import_react.useEffect)(() => {
|
|
48
|
+
return (0, import_use_focus_visible.trackFocusVisible)(setIsFocusVisible);
|
|
49
|
+
}, []);
|
|
50
|
+
const onChange = (0, import_utils.useCallbackRef)(
|
|
51
|
+
(ev) => {
|
|
52
|
+
var _a;
|
|
53
|
+
if (readOnly || disabled) {
|
|
54
|
+
ev.preventDefault();
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (!isControlled)
|
|
58
|
+
setIsChecked(ev.target.checked);
|
|
59
|
+
(_a = rest.onChange) == null ? void 0 : _a.call(rest, ev);
|
|
60
|
+
},
|
|
61
|
+
[readOnly, disabled, isControlled]
|
|
62
|
+
);
|
|
63
|
+
const onBlur = (0, import_utils.useCallbackRef)(rest.onBlur);
|
|
64
|
+
const onFocus = (0, import_utils.useCallbackRef)(rest.onFocus);
|
|
65
|
+
const onKeyDown = (0, import_react.useCallback)(
|
|
66
|
+
({ key }) => {
|
|
67
|
+
if (key === " ")
|
|
68
|
+
setActive(true);
|
|
69
|
+
},
|
|
70
|
+
[setActive]
|
|
71
|
+
);
|
|
72
|
+
const onKeyUp = (0, import_react.useCallback)(
|
|
73
|
+
({ key }) => {
|
|
74
|
+
if (key === " ")
|
|
75
|
+
setActive(false);
|
|
76
|
+
},
|
|
77
|
+
[setActive]
|
|
78
|
+
);
|
|
79
|
+
const getContainerProps = (0, import_react.useCallback)(
|
|
80
|
+
(props2 = {}, ref = null) => ({
|
|
81
|
+
...(0, import_utils.pickObject)(rest, import_form_control.formControlProperties),
|
|
82
|
+
...props2,
|
|
83
|
+
ref,
|
|
84
|
+
"data-checked": (0, import_utils.dataAttr)(checked)
|
|
85
|
+
}),
|
|
86
|
+
[checked, rest]
|
|
87
|
+
);
|
|
88
|
+
const getIconProps = (0, import_react.useCallback)(
|
|
89
|
+
(props2 = {}, ref = null) => ({
|
|
90
|
+
...(0, import_utils.pickObject)(rest, import_form_control.formControlProperties),
|
|
91
|
+
...props2,
|
|
92
|
+
ref,
|
|
93
|
+
"data-active": (0, import_utils.dataAttr)(isActive),
|
|
94
|
+
"data-hover": (0, import_utils.dataAttr)(isHovered),
|
|
95
|
+
"data-checked": (0, import_utils.dataAttr)(checked),
|
|
96
|
+
"data-focus": (0, import_utils.dataAttr)(isFocused),
|
|
97
|
+
"data-focus-visible": (0, import_utils.dataAttr)(isFocused && isFocusVisible),
|
|
98
|
+
"aria-hidden": true,
|
|
99
|
+
onMouseDown: (0, import_utils.handlerAll)(props2.onMouseDown, () => setActive(true)),
|
|
100
|
+
onMouseUp: (0, import_utils.handlerAll)(props2.onMouseUp, () => setActive(false)),
|
|
101
|
+
onMouseEnter: (0, import_utils.handlerAll)(props2.onMouseEnter, () => setHovered(true)),
|
|
102
|
+
onMouseLeave: (0, import_utils.handlerAll)(props2.onMouseLeave, () => setHovered(false))
|
|
103
|
+
}),
|
|
104
|
+
[checked, isActive, isFocused, isFocusVisible, isHovered, rest]
|
|
105
|
+
);
|
|
106
|
+
const getInputProps = (0, import_react.useCallback)(
|
|
107
|
+
(props2 = {}, ref = null) => ({
|
|
108
|
+
...(0, import_utils.pickObject)(rest, import_form_control.formControlProperties),
|
|
109
|
+
...props2,
|
|
110
|
+
ref,
|
|
111
|
+
id,
|
|
112
|
+
type: "radio",
|
|
113
|
+
name,
|
|
114
|
+
value,
|
|
115
|
+
required,
|
|
116
|
+
disabled,
|
|
117
|
+
readOnly,
|
|
118
|
+
checked,
|
|
119
|
+
style: {
|
|
120
|
+
border: "0px",
|
|
121
|
+
clip: "rect(0px, 0px, 0px, 0px)",
|
|
122
|
+
height: "1px",
|
|
123
|
+
width: "1px",
|
|
124
|
+
margin: "-1px",
|
|
125
|
+
padding: "0px",
|
|
126
|
+
overflow: "hidden",
|
|
127
|
+
whiteSpace: "nowrap",
|
|
128
|
+
position: "absolute"
|
|
129
|
+
},
|
|
130
|
+
onChange: (0, import_utils.handlerAll)(props2.onChange, onChange),
|
|
131
|
+
onBlur: (0, import_utils.handlerAll)(props2.onBlur, onBlur, () => setFocused(false)),
|
|
132
|
+
onFocus: (0, import_utils.handlerAll)(props2.onFocus, onFocus, () => setFocused(true)),
|
|
133
|
+
onKeyDown: (0, import_utils.handlerAll)(props2.onKeyDown, onKeyDown),
|
|
134
|
+
onKeyUp: (0, import_utils.handlerAll)(props2.onKeyUp, onKeyUp)
|
|
135
|
+
}),
|
|
136
|
+
[
|
|
137
|
+
rest,
|
|
138
|
+
id,
|
|
139
|
+
name,
|
|
140
|
+
value,
|
|
141
|
+
required,
|
|
142
|
+
disabled,
|
|
143
|
+
readOnly,
|
|
144
|
+
checked,
|
|
145
|
+
onChange,
|
|
146
|
+
onBlur,
|
|
147
|
+
onFocus,
|
|
148
|
+
onKeyDown,
|
|
149
|
+
onKeyUp
|
|
150
|
+
]
|
|
151
|
+
);
|
|
152
|
+
const getLabelProps = (0, import_react.useCallback)(
|
|
153
|
+
(props2 = {}, ref = null) => ({
|
|
154
|
+
...(0, import_utils.pickObject)(rest, import_form_control.formControlProperties),
|
|
155
|
+
props: props2,
|
|
156
|
+
ref,
|
|
157
|
+
onMouseDown: (0, import_utils.handlerAll)(props2.onMouseDown, (ev) => {
|
|
158
|
+
ev.preventDefault();
|
|
159
|
+
ev.stopPropagation();
|
|
160
|
+
}),
|
|
161
|
+
onTouchStart: (0, import_utils.handlerAll)(props2.onTouchStart, (ev) => {
|
|
162
|
+
ev.preventDefault();
|
|
163
|
+
ev.stopPropagation();
|
|
164
|
+
}),
|
|
165
|
+
"data-checked": (0, import_utils.dataAttr)(checked)
|
|
166
|
+
}),
|
|
167
|
+
[checked, rest]
|
|
168
|
+
);
|
|
169
|
+
return {
|
|
170
|
+
isFocusVisible,
|
|
171
|
+
isFocused,
|
|
172
|
+
isHovered,
|
|
173
|
+
isActive,
|
|
174
|
+
isChecked: checked,
|
|
175
|
+
getContainerProps,
|
|
176
|
+
getInputProps,
|
|
177
|
+
getIconProps,
|
|
178
|
+
getLabelProps
|
|
179
|
+
};
|
|
180
|
+
};
|
|
181
|
+
var Radio = (0, import_react.forwardRef)(
|
|
182
|
+
(props, ref) => {
|
|
183
|
+
var _a, _b, _c, _d;
|
|
184
|
+
const group = useRadioGroupContenxt();
|
|
185
|
+
const control = (0, import_form_control.useFormControl)(props);
|
|
186
|
+
const [styles, mergedProps] = (0, import_core.useMultiComponentStyle)("Radio", { ...group, ...props });
|
|
187
|
+
const {
|
|
188
|
+
className,
|
|
189
|
+
gap = "0.5rem",
|
|
190
|
+
isRequired = (_a = group == null ? void 0 : group.isRequired) != null ? _a : control.isRequired,
|
|
191
|
+
isReadOnly = (_b = group == null ? void 0 : group.isReadOnly) != null ? _b : control.isReadOnly,
|
|
192
|
+
isDisabled = (_c = group == null ? void 0 : group.isDisabled) != null ? _c : control.isDisabled,
|
|
193
|
+
isInvalid = (_d = group == null ? void 0 : group.isInvalid) != null ? _d : control.isInvalid,
|
|
194
|
+
iconProps,
|
|
195
|
+
inputProps,
|
|
196
|
+
labelProps,
|
|
197
|
+
children,
|
|
198
|
+
...rest
|
|
199
|
+
} = (0, import_core.omitThemeProps)(mergedProps);
|
|
200
|
+
const { getContainerProps, getInputProps, getIconProps, getLabelProps } = useRadio({
|
|
201
|
+
...rest,
|
|
202
|
+
isRequired,
|
|
203
|
+
isReadOnly,
|
|
204
|
+
isDisabled,
|
|
205
|
+
isInvalid,
|
|
206
|
+
isChecked: (group == null ? void 0 : group.value) && rest.value ? group.value === rest.value : rest.isChecked,
|
|
207
|
+
onChange: (group == null ? void 0 : group.onChange) && rest.value ? (0, import_utils.funcAll)(group.onChange, rest.onChange) : rest.onChange
|
|
208
|
+
});
|
|
209
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
210
|
+
import_core.ui.label,
|
|
211
|
+
{
|
|
212
|
+
className: (0, import_utils.cx)("ui-radio", className),
|
|
213
|
+
...getContainerProps(),
|
|
214
|
+
...(0, import_utils.omitObject)(rest, [
|
|
215
|
+
"id",
|
|
216
|
+
"name",
|
|
217
|
+
"value",
|
|
218
|
+
"defaultValue",
|
|
219
|
+
"defaultChecked",
|
|
220
|
+
"isChecked",
|
|
221
|
+
"onChange",
|
|
222
|
+
"onBlur",
|
|
223
|
+
"onFocus"
|
|
224
|
+
]),
|
|
225
|
+
__css: {
|
|
226
|
+
cursor: "pointer",
|
|
227
|
+
position: "relative",
|
|
228
|
+
display: "inline-flex",
|
|
229
|
+
alignItems: "center",
|
|
230
|
+
verticalAlign: "top",
|
|
231
|
+
gap,
|
|
232
|
+
...styles.container
|
|
233
|
+
},
|
|
234
|
+
children: [
|
|
235
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_core.ui.input, { className: "ui-radio-input", ...getInputProps(inputProps, ref) }),
|
|
236
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
237
|
+
import_core.ui.span,
|
|
238
|
+
{
|
|
239
|
+
className: "ui-radio-icon",
|
|
240
|
+
...getIconProps(iconProps),
|
|
241
|
+
__css: {
|
|
242
|
+
position: "relative",
|
|
243
|
+
display: "inline-block",
|
|
244
|
+
userSelect: "none",
|
|
245
|
+
...styles.icon
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
),
|
|
249
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
250
|
+
import_core.ui.span,
|
|
251
|
+
{
|
|
252
|
+
className: "ui-radio-label",
|
|
253
|
+
...getLabelProps(labelProps),
|
|
254
|
+
__css: { ...styles.label },
|
|
255
|
+
children
|
|
256
|
+
}
|
|
257
|
+
)
|
|
258
|
+
]
|
|
259
|
+
}
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
);
|
|
263
|
+
Radio.displayName = "Radio";
|
|
264
|
+
|
|
265
|
+
// src/radio-group.tsx
|
|
266
|
+
var import_form_control2 = require("@yamada-ui/form-control");
|
|
267
|
+
var import_layouts = require("@yamada-ui/layouts");
|
|
268
|
+
var import_use_controllable_state = require("@yamada-ui/use-controllable-state");
|
|
269
|
+
var import_utils2 = require("@yamada-ui/utils");
|
|
270
|
+
var import_react2 = require("react");
|
|
271
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
272
|
+
var isEvent = (value) => value && (0, import_utils2.isObject)(value) && (0, import_utils2.isObject)(value.target);
|
|
273
|
+
var useRadioGroup = ({
|
|
274
|
+
id,
|
|
275
|
+
name,
|
|
276
|
+
isNative,
|
|
277
|
+
...props
|
|
278
|
+
}) => {
|
|
279
|
+
id = id != null ? id : (0, import_react2.useId)();
|
|
280
|
+
name = name != null ? name : `radio-${id}`;
|
|
281
|
+
props.onChange = (0, import_utils2.useCallbackRef)(props.onChange);
|
|
282
|
+
const [value, setValue] = (0, import_use_controllable_state.useControllableState)({
|
|
283
|
+
value: props.value,
|
|
284
|
+
defaultValue: props.defaultValue,
|
|
285
|
+
onChange: props.onChange
|
|
286
|
+
});
|
|
287
|
+
const containerRef = (0, import_react2.useRef)(null);
|
|
288
|
+
const onFocus = (0, import_react2.useCallback)(() => {
|
|
289
|
+
const container = containerRef.current;
|
|
290
|
+
if (!container)
|
|
291
|
+
return;
|
|
292
|
+
let query = `input:not(:disabled):checked`;
|
|
293
|
+
let firstInput = container.querySelector(query);
|
|
294
|
+
if (firstInput) {
|
|
295
|
+
firstInput.focus();
|
|
296
|
+
} else {
|
|
297
|
+
query = `input:not(:disabled)`;
|
|
298
|
+
firstInput = container.querySelector(query);
|
|
299
|
+
firstInput == null ? void 0 : firstInput.focus();
|
|
300
|
+
}
|
|
301
|
+
}, []);
|
|
302
|
+
const onChange = (0, import_react2.useCallback)(
|
|
303
|
+
(evOrValue) => {
|
|
304
|
+
const nextValue = isEvent(evOrValue) ? evOrValue.target.value : evOrValue;
|
|
305
|
+
setValue(nextValue);
|
|
306
|
+
},
|
|
307
|
+
[setValue]
|
|
308
|
+
);
|
|
309
|
+
const getContainerProps = (0, import_react2.useCallback)(
|
|
310
|
+
(props2 = {}, ref = null) => ({
|
|
311
|
+
...props2,
|
|
312
|
+
ref: (0, import_utils2.mergeRefs)(ref, containerRef),
|
|
313
|
+
role: "group"
|
|
314
|
+
}),
|
|
315
|
+
[]
|
|
316
|
+
);
|
|
317
|
+
const getRadioProps = (0, import_react2.useCallback)(
|
|
318
|
+
(props2 = {}, ref = null) => ({
|
|
319
|
+
...props2,
|
|
320
|
+
ref,
|
|
321
|
+
name,
|
|
322
|
+
[isNative ? "checked" : "isChecked"]: value != null ? props2.value === value : void 0,
|
|
323
|
+
onChange
|
|
324
|
+
}),
|
|
325
|
+
[name, value, onChange, isNative]
|
|
326
|
+
);
|
|
327
|
+
return {
|
|
328
|
+
name,
|
|
329
|
+
value,
|
|
330
|
+
setValue,
|
|
331
|
+
onChange,
|
|
332
|
+
onFocus,
|
|
333
|
+
getContainerProps,
|
|
334
|
+
getRadioProps
|
|
335
|
+
};
|
|
336
|
+
};
|
|
337
|
+
var [RadioGroupProvider, useRadioGroupContenxt] = (0, import_utils2.createContext)({
|
|
338
|
+
strict: false,
|
|
339
|
+
name: "RadioGroupContext"
|
|
340
|
+
});
|
|
341
|
+
var RadioGroup = (0, import_react2.forwardRef)(
|
|
342
|
+
({
|
|
343
|
+
className,
|
|
344
|
+
size,
|
|
345
|
+
variant,
|
|
346
|
+
colorScheme,
|
|
347
|
+
children,
|
|
348
|
+
direction = "column",
|
|
349
|
+
gap,
|
|
350
|
+
...props
|
|
351
|
+
}, ref) => {
|
|
352
|
+
const { name, value, onChange, getContainerProps } = useRadioGroup(props);
|
|
353
|
+
const { isRequired, isReadOnly, isDisabled, isInvalid } = (0, import_form_control2.useFormControl)(props);
|
|
354
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
355
|
+
RadioGroupProvider,
|
|
356
|
+
{
|
|
357
|
+
value: {
|
|
358
|
+
size,
|
|
359
|
+
variant,
|
|
360
|
+
colorScheme,
|
|
361
|
+
isRequired,
|
|
362
|
+
isReadOnly,
|
|
363
|
+
isDisabled,
|
|
364
|
+
isInvalid,
|
|
365
|
+
name,
|
|
366
|
+
value,
|
|
367
|
+
onChange
|
|
368
|
+
},
|
|
369
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
370
|
+
import_layouts.Flex,
|
|
371
|
+
{
|
|
372
|
+
ref,
|
|
373
|
+
className: (0, import_utils2.cx)("ui-radio-group", className),
|
|
374
|
+
direction,
|
|
375
|
+
gap: gap != null ? gap : direction === "row" ? "1rem" : void 0,
|
|
376
|
+
...getContainerProps((0, import_utils2.omitObject)(props, ["onChange"])),
|
|
377
|
+
children
|
|
378
|
+
}
|
|
379
|
+
)
|
|
380
|
+
}
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
);
|
|
384
|
+
RadioGroup.displayName = "RadioGroup";
|
|
385
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
386
|
+
0 && (module.exports = {
|
|
387
|
+
Radio,
|
|
388
|
+
RadioGroup,
|
|
389
|
+
useRadio,
|
|
390
|
+
useRadioGroup,
|
|
391
|
+
useRadioGroupContenxt
|
|
392
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Radio,
|
|
3
|
+
useRadio
|
|
4
|
+
} from "./chunk-HR6MSMLG.mjs";
|
|
5
|
+
import {
|
|
6
|
+
RadioGroup,
|
|
7
|
+
useRadioGroup,
|
|
8
|
+
useRadioGroupContenxt
|
|
9
|
+
} from "./chunk-3SKWSFIA.mjs";
|
|
10
|
+
export {
|
|
11
|
+
Radio,
|
|
12
|
+
RadioGroup,
|
|
13
|
+
useRadio,
|
|
14
|
+
useRadioGroup,
|
|
15
|
+
useRadioGroupContenxt
|
|
16
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ChangeEvent, Ref } from 'react';
|
|
3
|
+
import { ThemeProps, ComponentArgs } from '@yamada-ui/core';
|
|
4
|
+
import { FormControlOptions } from '@yamada-ui/form-control';
|
|
5
|
+
import { FlexProps } from '@yamada-ui/layouts';
|
|
6
|
+
import { PropGetter, DOMAttributes } from '@yamada-ui/utils';
|
|
7
|
+
|
|
8
|
+
type UseRadioGroupProps<Y extends string | number = string> = {
|
|
9
|
+
id?: string;
|
|
10
|
+
name?: string;
|
|
11
|
+
value?: Y;
|
|
12
|
+
defaultValue?: Y;
|
|
13
|
+
onChange?: (value: Y) => void;
|
|
14
|
+
isNative?: boolean;
|
|
15
|
+
};
|
|
16
|
+
declare const useRadioGroup: <Y extends string | number = string>({ id, name, isNative, ...props }: UseRadioGroupProps<Y>) => {
|
|
17
|
+
name: string;
|
|
18
|
+
value: Y;
|
|
19
|
+
setValue: react.Dispatch<react.SetStateAction<Y>>;
|
|
20
|
+
onChange: (evOrValue: ChangeEvent<HTMLInputElement> | Y) => void;
|
|
21
|
+
onFocus: () => void;
|
|
22
|
+
getContainerProps: PropGetter<Record<string, unknown>, DOMAttributes<Element & HTMLOrSVGElement>>;
|
|
23
|
+
getRadioProps: PropGetter<react.AriaAttributes & react.DOMAttributes<HTMLInputElement> & {
|
|
24
|
+
[dataAttr: string]: any;
|
|
25
|
+
} & {
|
|
26
|
+
id?: string | undefined;
|
|
27
|
+
role?: react.AriaRole | undefined;
|
|
28
|
+
tabIndex?: number | undefined;
|
|
29
|
+
style?: react.CSSProperties | undefined;
|
|
30
|
+
} & {
|
|
31
|
+
isChecked?: boolean | undefined;
|
|
32
|
+
}, Omit<DOMAttributes<HTMLInputElement>, "onChange"> & {
|
|
33
|
+
onChange: (ev: ChangeEvent<HTMLInputElement> | Y) => void;
|
|
34
|
+
}>;
|
|
35
|
+
};
|
|
36
|
+
type RadioGroupProps<Y extends string | number = string> = ThemeProps<'Radio'> & Omit<FlexProps, 'onChange'> & UseRadioGroupProps<Y> & FormControlOptions;
|
|
37
|
+
type RadioGroupContext = ThemeProps<'Radio'> & FormControlOptions & {
|
|
38
|
+
name: string;
|
|
39
|
+
value: string | number;
|
|
40
|
+
onChange: (evOrValue: ChangeEvent<HTMLInputElement> | string | number) => void;
|
|
41
|
+
};
|
|
42
|
+
declare const useRadioGroupContenxt: () => RadioGroupContext | undefined;
|
|
43
|
+
|
|
44
|
+
declare const RadioGroup: (<Y extends string | number = string>(props: ThemeProps<"Radio"> & Omit<FlexProps, "onChange"> & UseRadioGroupProps<Y> & FormControlOptions & {
|
|
45
|
+
ref?: Ref<HTMLDivElement> | undefined;
|
|
46
|
+
}) => JSX.Element) & ComponentArgs;
|
|
47
|
+
|
|
48
|
+
export { RadioGroup, RadioGroupProps, UseRadioGroupProps, useRadioGroup, useRadioGroupContenxt };
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/radio-group.tsx
|
|
21
|
+
var radio_group_exports = {};
|
|
22
|
+
__export(radio_group_exports, {
|
|
23
|
+
RadioGroup: () => RadioGroup,
|
|
24
|
+
useRadioGroup: () => useRadioGroup,
|
|
25
|
+
useRadioGroupContenxt: () => useRadioGroupContenxt
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(radio_group_exports);
|
|
28
|
+
var import_form_control = require("@yamada-ui/form-control");
|
|
29
|
+
var import_layouts = require("@yamada-ui/layouts");
|
|
30
|
+
var import_use_controllable_state = require("@yamada-ui/use-controllable-state");
|
|
31
|
+
var import_utils = require("@yamada-ui/utils");
|
|
32
|
+
var import_react = require("react");
|
|
33
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
34
|
+
var isEvent = (value) => value && (0, import_utils.isObject)(value) && (0, import_utils.isObject)(value.target);
|
|
35
|
+
var useRadioGroup = ({
|
|
36
|
+
id,
|
|
37
|
+
name,
|
|
38
|
+
isNative,
|
|
39
|
+
...props
|
|
40
|
+
}) => {
|
|
41
|
+
id = id != null ? id : (0, import_react.useId)();
|
|
42
|
+
name = name != null ? name : `radio-${id}`;
|
|
43
|
+
props.onChange = (0, import_utils.useCallbackRef)(props.onChange);
|
|
44
|
+
const [value, setValue] = (0, import_use_controllable_state.useControllableState)({
|
|
45
|
+
value: props.value,
|
|
46
|
+
defaultValue: props.defaultValue,
|
|
47
|
+
onChange: props.onChange
|
|
48
|
+
});
|
|
49
|
+
const containerRef = (0, import_react.useRef)(null);
|
|
50
|
+
const onFocus = (0, import_react.useCallback)(() => {
|
|
51
|
+
const container = containerRef.current;
|
|
52
|
+
if (!container)
|
|
53
|
+
return;
|
|
54
|
+
let query = `input:not(:disabled):checked`;
|
|
55
|
+
let firstInput = container.querySelector(query);
|
|
56
|
+
if (firstInput) {
|
|
57
|
+
firstInput.focus();
|
|
58
|
+
} else {
|
|
59
|
+
query = `input:not(:disabled)`;
|
|
60
|
+
firstInput = container.querySelector(query);
|
|
61
|
+
firstInput == null ? void 0 : firstInput.focus();
|
|
62
|
+
}
|
|
63
|
+
}, []);
|
|
64
|
+
const onChange = (0, import_react.useCallback)(
|
|
65
|
+
(evOrValue) => {
|
|
66
|
+
const nextValue = isEvent(evOrValue) ? evOrValue.target.value : evOrValue;
|
|
67
|
+
setValue(nextValue);
|
|
68
|
+
},
|
|
69
|
+
[setValue]
|
|
70
|
+
);
|
|
71
|
+
const getContainerProps = (0, import_react.useCallback)(
|
|
72
|
+
(props2 = {}, ref = null) => ({
|
|
73
|
+
...props2,
|
|
74
|
+
ref: (0, import_utils.mergeRefs)(ref, containerRef),
|
|
75
|
+
role: "group"
|
|
76
|
+
}),
|
|
77
|
+
[]
|
|
78
|
+
);
|
|
79
|
+
const getRadioProps = (0, import_react.useCallback)(
|
|
80
|
+
(props2 = {}, ref = null) => ({
|
|
81
|
+
...props2,
|
|
82
|
+
ref,
|
|
83
|
+
name,
|
|
84
|
+
[isNative ? "checked" : "isChecked"]: value != null ? props2.value === value : void 0,
|
|
85
|
+
onChange
|
|
86
|
+
}),
|
|
87
|
+
[name, value, onChange, isNative]
|
|
88
|
+
);
|
|
89
|
+
return {
|
|
90
|
+
name,
|
|
91
|
+
value,
|
|
92
|
+
setValue,
|
|
93
|
+
onChange,
|
|
94
|
+
onFocus,
|
|
95
|
+
getContainerProps,
|
|
96
|
+
getRadioProps
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
var [RadioGroupProvider, useRadioGroupContenxt] = (0, import_utils.createContext)({
|
|
100
|
+
strict: false,
|
|
101
|
+
name: "RadioGroupContext"
|
|
102
|
+
});
|
|
103
|
+
var RadioGroup = (0, import_react.forwardRef)(
|
|
104
|
+
({
|
|
105
|
+
className,
|
|
106
|
+
size,
|
|
107
|
+
variant,
|
|
108
|
+
colorScheme,
|
|
109
|
+
children,
|
|
110
|
+
direction = "column",
|
|
111
|
+
gap,
|
|
112
|
+
...props
|
|
113
|
+
}, ref) => {
|
|
114
|
+
const { name, value, onChange, getContainerProps } = useRadioGroup(props);
|
|
115
|
+
const { isRequired, isReadOnly, isDisabled, isInvalid } = (0, import_form_control.useFormControl)(props);
|
|
116
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
117
|
+
RadioGroupProvider,
|
|
118
|
+
{
|
|
119
|
+
value: {
|
|
120
|
+
size,
|
|
121
|
+
variant,
|
|
122
|
+
colorScheme,
|
|
123
|
+
isRequired,
|
|
124
|
+
isReadOnly,
|
|
125
|
+
isDisabled,
|
|
126
|
+
isInvalid,
|
|
127
|
+
name,
|
|
128
|
+
value,
|
|
129
|
+
onChange
|
|
130
|
+
},
|
|
131
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
132
|
+
import_layouts.Flex,
|
|
133
|
+
{
|
|
134
|
+
ref,
|
|
135
|
+
className: (0, import_utils.cx)("ui-radio-group", className),
|
|
136
|
+
direction,
|
|
137
|
+
gap: gap != null ? gap : direction === "row" ? "1rem" : void 0,
|
|
138
|
+
...getContainerProps((0, import_utils.omitObject)(props, ["onChange"])),
|
|
139
|
+
children
|
|
140
|
+
}
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
);
|
|
146
|
+
RadioGroup.displayName = "RadioGroup";
|
|
147
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
148
|
+
0 && (module.exports = {
|
|
149
|
+
RadioGroup,
|
|
150
|
+
useRadioGroup,
|
|
151
|
+
useRadioGroupContenxt
|
|
152
|
+
});
|
package/dist/radio.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import * as _yamada_ui_utils from '@yamada-ui/utils';
|
|
2
|
+
import { PropGetter } from '@yamada-ui/utils';
|
|
3
|
+
import { HTMLUIProps, ThemeProps, ComponentArgs } from '@yamada-ui/core';
|
|
4
|
+
import { FormControlOptions } from '@yamada-ui/form-control';
|
|
5
|
+
import { ChangeEventHandler, Ref, InputHTMLAttributes } from 'react';
|
|
6
|
+
|
|
7
|
+
type UseRadioProps<Y extends string | number = string> = FormControlOptions & {
|
|
8
|
+
id?: string;
|
|
9
|
+
name?: string;
|
|
10
|
+
value?: Y;
|
|
11
|
+
defaultChecked?: boolean;
|
|
12
|
+
isChecked?: boolean;
|
|
13
|
+
onChange?: ChangeEventHandler<HTMLInputElement>;
|
|
14
|
+
};
|
|
15
|
+
declare const useRadio: <Y extends string | number = string>(props: UseRadioProps<Y>) => {
|
|
16
|
+
isFocusVisible: boolean;
|
|
17
|
+
isFocused: boolean;
|
|
18
|
+
isHovered: boolean;
|
|
19
|
+
isActive: boolean;
|
|
20
|
+
isChecked: boolean;
|
|
21
|
+
getContainerProps: PropGetter<Record<string, unknown>, _yamada_ui_utils.DOMAttributes<Element & HTMLOrSVGElement>>;
|
|
22
|
+
getInputProps: PropGetter<Record<string, unknown>, _yamada_ui_utils.DOMAttributes<Element & HTMLOrSVGElement>>;
|
|
23
|
+
getIconProps: PropGetter<Record<string, unknown>, _yamada_ui_utils.DOMAttributes<Element & HTMLOrSVGElement>>;
|
|
24
|
+
getLabelProps: PropGetter<Record<string, unknown>, _yamada_ui_utils.DOMAttributes<Element & HTMLOrSVGElement>>;
|
|
25
|
+
};
|
|
26
|
+
type RadioOptions = {
|
|
27
|
+
iconProps?: HTMLUIProps<'span'>;
|
|
28
|
+
inputProps?: InputHTMLAttributes<HTMLInputElement>;
|
|
29
|
+
labelProps?: HTMLUIProps<'span'>;
|
|
30
|
+
};
|
|
31
|
+
type RadioProps<Y extends string | number = string> = Omit<HTMLUIProps<'label'>, keyof UseRadioProps> & ThemeProps<'Radio'> & UseRadioProps<Y> & RadioOptions;
|
|
32
|
+
declare const Radio: (<Y extends string | number = string>(props: Omit<HTMLUIProps<"label">, "value" | "name" | "id" | "onChange" | "defaultChecked" | keyof FormControlOptions | "isChecked"> & ThemeProps<"Radio"> & FormControlOptions & {
|
|
33
|
+
id?: string | undefined;
|
|
34
|
+
name?: string | undefined;
|
|
35
|
+
value?: Y | undefined;
|
|
36
|
+
defaultChecked?: boolean | undefined;
|
|
37
|
+
isChecked?: boolean | undefined;
|
|
38
|
+
onChange?: ChangeEventHandler<HTMLInputElement> | undefined;
|
|
39
|
+
} & RadioOptions & {
|
|
40
|
+
ref?: Ref<HTMLInputElement> | undefined;
|
|
41
|
+
}) => JSX.Element) & ComponentArgs;
|
|
42
|
+
|
|
43
|
+
export { Radio, RadioProps, UseRadioProps, useRadio };
|