@yamada-ui/radio 0.0.0-dev-20230603042803

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