@seed-design/react-checkbox 0.0.2 → 0.0.3
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/lib/Checkbox-12s-BeV5Lkaw.js +184 -0
- package/lib/Checkbox-12s-Y3jZEb8V.cjs +187 -0
- package/lib/index.cjs +8 -188
- package/lib/index.js +3 -182
- package/package.json +1 -1
- package/src/Checkbox.tsx +2 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx } from 'react/jsx-runtime';
|
|
3
|
+
import { elementProps, dataAttr, inputProps, labelProps, visuallyHidden, mergeProps } from '@seed-design/dom-utils';
|
|
4
|
+
import { Primitive } from '@seed-design/react-primitive';
|
|
5
|
+
import { useState, useRef, useEffect, createContext, useContext, forwardRef } from 'react';
|
|
6
|
+
import { useControllableState } from '@radix-ui/react-use-controllable-state';
|
|
7
|
+
import { composeRefs } from '@radix-ui/react-compose-refs';
|
|
8
|
+
|
|
9
|
+
function useCheckboxState(props) {
|
|
10
|
+
const [isChecked = false, setIsChecked] = useControllableState({
|
|
11
|
+
prop: props.checked,
|
|
12
|
+
defaultProp: props.defaultChecked,
|
|
13
|
+
onChange: props.onCheckedChange
|
|
14
|
+
});
|
|
15
|
+
const [isHovered, setIsHovered] = useState(false);
|
|
16
|
+
const [isActive, setIsActive] = useState(false);
|
|
17
|
+
const [isFocused, setIsFocused] = useState(false);
|
|
18
|
+
const [isFocusVisible, setIsFocusVisible] = useState(false);
|
|
19
|
+
const inputRef = useRef(null);
|
|
20
|
+
const initialCheckedRef = useRef(isChecked);
|
|
21
|
+
useEffect(()=>{
|
|
22
|
+
const form = inputRef.current?.form;
|
|
23
|
+
if (form) {
|
|
24
|
+
const reset = ()=>setIsChecked(initialCheckedRef.current);
|
|
25
|
+
form.addEventListener("reset", reset);
|
|
26
|
+
return ()=>form.removeEventListener("reset", reset);
|
|
27
|
+
}
|
|
28
|
+
}, [
|
|
29
|
+
setIsChecked
|
|
30
|
+
]);
|
|
31
|
+
useEffect(()=>{
|
|
32
|
+
if (!inputRef.current) return;
|
|
33
|
+
inputRef.current.indeterminate = props.indeterminate ?? false;
|
|
34
|
+
}, [
|
|
35
|
+
props.indeterminate
|
|
36
|
+
]);
|
|
37
|
+
return {
|
|
38
|
+
refs: {
|
|
39
|
+
input: inputRef
|
|
40
|
+
},
|
|
41
|
+
isIndeterminate: props.indeterminate ?? false,
|
|
42
|
+
isChecked,
|
|
43
|
+
setIsChecked,
|
|
44
|
+
isHovered,
|
|
45
|
+
setIsHovered,
|
|
46
|
+
isActive,
|
|
47
|
+
setIsActive,
|
|
48
|
+
isFocused,
|
|
49
|
+
setIsFocused,
|
|
50
|
+
isFocusVisible,
|
|
51
|
+
setIsFocusVisible
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function useCheckbox(props) {
|
|
55
|
+
const { refs, isIndeterminate, setIsChecked, isChecked, setIsHovered, isHovered, setIsActive, isActive, setIsFocused, isFocused, setIsFocusVisible, isFocusVisible } = useCheckboxState(props);
|
|
56
|
+
const stateProps = elementProps({
|
|
57
|
+
"data-checked": dataAttr(isChecked),
|
|
58
|
+
"data-indeterminate": dataAttr(isIndeterminate),
|
|
59
|
+
"data-hover": dataAttr(isHovered),
|
|
60
|
+
"data-active": dataAttr(isActive),
|
|
61
|
+
"data-focus": dataAttr(isFocused),
|
|
62
|
+
"data-focus-visible": dataAttr(isFocusVisible),
|
|
63
|
+
"data-disabled": dataAttr(props.disabled),
|
|
64
|
+
"data-invalid": dataAttr(props.invalid),
|
|
65
|
+
"data-required": dataAttr(props.required)
|
|
66
|
+
});
|
|
67
|
+
const isControlled = props.checked != null;
|
|
68
|
+
return {
|
|
69
|
+
indeterminate: isIndeterminate,
|
|
70
|
+
checked: isChecked,
|
|
71
|
+
setChecked: setIsChecked,
|
|
72
|
+
focused: isFocused,
|
|
73
|
+
setFocused: setIsFocused,
|
|
74
|
+
focusVisible: isFocusVisible,
|
|
75
|
+
setFocusVisible: setIsFocusVisible,
|
|
76
|
+
refs,
|
|
77
|
+
stateProps,
|
|
78
|
+
rootProps: labelProps({
|
|
79
|
+
...stateProps,
|
|
80
|
+
onPointerMove () {
|
|
81
|
+
setIsHovered(true);
|
|
82
|
+
},
|
|
83
|
+
onPointerDown () {
|
|
84
|
+
setIsActive(true);
|
|
85
|
+
},
|
|
86
|
+
onPointerUp () {
|
|
87
|
+
setIsActive(false);
|
|
88
|
+
},
|
|
89
|
+
onPointerLeave () {
|
|
90
|
+
setIsHovered(false);
|
|
91
|
+
setIsActive(false);
|
|
92
|
+
}
|
|
93
|
+
}),
|
|
94
|
+
controlProps: elementProps({
|
|
95
|
+
...stateProps,
|
|
96
|
+
"aria-hidden": true
|
|
97
|
+
}),
|
|
98
|
+
hiddenInputProps: inputProps({
|
|
99
|
+
type: "checkbox",
|
|
100
|
+
role: "checkbox",
|
|
101
|
+
checked: isControlled ? isChecked : undefined,
|
|
102
|
+
defaultChecked: !isControlled ? isChecked : undefined,
|
|
103
|
+
disabled: props.disabled,
|
|
104
|
+
required: props.required,
|
|
105
|
+
"aria-invalid": props.invalid,
|
|
106
|
+
style: visuallyHidden,
|
|
107
|
+
...stateProps,
|
|
108
|
+
onChange (event) {
|
|
109
|
+
setIsChecked(event.currentTarget.checked);
|
|
110
|
+
setIsFocusVisible(event.target.matches(":focus-visible"));
|
|
111
|
+
},
|
|
112
|
+
onFocus (event) {
|
|
113
|
+
setIsFocused(true);
|
|
114
|
+
setIsFocusVisible(event.target.matches(":focus-visible"));
|
|
115
|
+
},
|
|
116
|
+
onBlur () {
|
|
117
|
+
setIsFocused(false);
|
|
118
|
+
setIsFocusVisible(false);
|
|
119
|
+
},
|
|
120
|
+
onKeyDown (event) {
|
|
121
|
+
if (event.key === " ") {
|
|
122
|
+
setIsActive(true);
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
onKeyUp (event) {
|
|
126
|
+
if (event.key === " ") {
|
|
127
|
+
setIsActive(false);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
})
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const CheckboxContext = /*#__PURE__*/ createContext(null);
|
|
135
|
+
const CheckboxProvider = CheckboxContext.Provider;
|
|
136
|
+
function useCheckboxContext({ strict = true } = {}) {
|
|
137
|
+
const context = useContext(CheckboxContext);
|
|
138
|
+
if (!context && strict) {
|
|
139
|
+
throw new Error("useCheckboxContext must be used within a Checkbox");
|
|
140
|
+
}
|
|
141
|
+
return context;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const CheckboxRoot = /*#__PURE__*/ forwardRef((props, ref)=>{
|
|
145
|
+
const { checked, defaultChecked, onCheckedChange, indeterminate, disabled, invalid, required, ...otherProps } = props;
|
|
146
|
+
const api = useCheckbox({
|
|
147
|
+
checked,
|
|
148
|
+
defaultChecked,
|
|
149
|
+
onCheckedChange,
|
|
150
|
+
indeterminate,
|
|
151
|
+
disabled,
|
|
152
|
+
invalid,
|
|
153
|
+
required
|
|
154
|
+
});
|
|
155
|
+
const mergedProps = mergeProps(api.rootProps, otherProps);
|
|
156
|
+
return /*#__PURE__*/ jsx(CheckboxProvider, {
|
|
157
|
+
value: api,
|
|
158
|
+
children: /*#__PURE__*/ jsx(Primitive.label, {
|
|
159
|
+
ref: ref,
|
|
160
|
+
...mergedProps
|
|
161
|
+
})
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
CheckboxRoot.displayName = "CheckboxRoot";
|
|
165
|
+
const CheckboxControl = /*#__PURE__*/ forwardRef((props, ref)=>{
|
|
166
|
+
const { controlProps } = useCheckboxContext();
|
|
167
|
+
const mergedProps = mergeProps(controlProps, props);
|
|
168
|
+
return /*#__PURE__*/ jsx(Primitive.div, {
|
|
169
|
+
ref: ref,
|
|
170
|
+
...mergedProps
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
CheckboxControl.displayName = "CheckboxControl";
|
|
174
|
+
const CheckboxHiddenInput = /*#__PURE__*/ forwardRef((props, ref)=>{
|
|
175
|
+
const { refs, hiddenInputProps } = useCheckboxContext();
|
|
176
|
+
const mergedProps = mergeProps(hiddenInputProps, props);
|
|
177
|
+
return /*#__PURE__*/ jsx(Primitive.input, {
|
|
178
|
+
ref: composeRefs(refs.input, ref),
|
|
179
|
+
...mergedProps
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
CheckboxHiddenInput.displayName = "CheckboxHiddenInput";
|
|
183
|
+
|
|
184
|
+
export { CheckboxControl as C, CheckboxHiddenInput as a, CheckboxRoot as b, useCheckboxContext as u };
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
3
|
+
var domUtils = require('@seed-design/dom-utils');
|
|
4
|
+
var reactPrimitive = require('@seed-design/react-primitive');
|
|
5
|
+
var react = require('react');
|
|
6
|
+
var reactUseControllableState = require('@radix-ui/react-use-controllable-state');
|
|
7
|
+
var reactComposeRefs = require('@radix-ui/react-compose-refs');
|
|
8
|
+
|
|
9
|
+
function useCheckboxState(props) {
|
|
10
|
+
const [isChecked = false, setIsChecked] = reactUseControllableState.useControllableState({
|
|
11
|
+
prop: props.checked,
|
|
12
|
+
defaultProp: props.defaultChecked,
|
|
13
|
+
onChange: props.onCheckedChange
|
|
14
|
+
});
|
|
15
|
+
const [isHovered, setIsHovered] = react.useState(false);
|
|
16
|
+
const [isActive, setIsActive] = react.useState(false);
|
|
17
|
+
const [isFocused, setIsFocused] = react.useState(false);
|
|
18
|
+
const [isFocusVisible, setIsFocusVisible] = react.useState(false);
|
|
19
|
+
const inputRef = react.useRef(null);
|
|
20
|
+
const initialCheckedRef = react.useRef(isChecked);
|
|
21
|
+
react.useEffect(()=>{
|
|
22
|
+
const form = inputRef.current?.form;
|
|
23
|
+
if (form) {
|
|
24
|
+
const reset = ()=>setIsChecked(initialCheckedRef.current);
|
|
25
|
+
form.addEventListener("reset", reset);
|
|
26
|
+
return ()=>form.removeEventListener("reset", reset);
|
|
27
|
+
}
|
|
28
|
+
}, [
|
|
29
|
+
setIsChecked
|
|
30
|
+
]);
|
|
31
|
+
react.useEffect(()=>{
|
|
32
|
+
if (!inputRef.current) return;
|
|
33
|
+
inputRef.current.indeterminate = props.indeterminate ?? false;
|
|
34
|
+
}, [
|
|
35
|
+
props.indeterminate
|
|
36
|
+
]);
|
|
37
|
+
return {
|
|
38
|
+
refs: {
|
|
39
|
+
input: inputRef
|
|
40
|
+
},
|
|
41
|
+
isIndeterminate: props.indeterminate ?? false,
|
|
42
|
+
isChecked,
|
|
43
|
+
setIsChecked,
|
|
44
|
+
isHovered,
|
|
45
|
+
setIsHovered,
|
|
46
|
+
isActive,
|
|
47
|
+
setIsActive,
|
|
48
|
+
isFocused,
|
|
49
|
+
setIsFocused,
|
|
50
|
+
isFocusVisible,
|
|
51
|
+
setIsFocusVisible
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function useCheckbox(props) {
|
|
55
|
+
const { refs, isIndeterminate, setIsChecked, isChecked, setIsHovered, isHovered, setIsActive, isActive, setIsFocused, isFocused, setIsFocusVisible, isFocusVisible } = useCheckboxState(props);
|
|
56
|
+
const stateProps = domUtils.elementProps({
|
|
57
|
+
"data-checked": domUtils.dataAttr(isChecked),
|
|
58
|
+
"data-indeterminate": domUtils.dataAttr(isIndeterminate),
|
|
59
|
+
"data-hover": domUtils.dataAttr(isHovered),
|
|
60
|
+
"data-active": domUtils.dataAttr(isActive),
|
|
61
|
+
"data-focus": domUtils.dataAttr(isFocused),
|
|
62
|
+
"data-focus-visible": domUtils.dataAttr(isFocusVisible),
|
|
63
|
+
"data-disabled": domUtils.dataAttr(props.disabled),
|
|
64
|
+
"data-invalid": domUtils.dataAttr(props.invalid),
|
|
65
|
+
"data-required": domUtils.dataAttr(props.required)
|
|
66
|
+
});
|
|
67
|
+
const isControlled = props.checked != null;
|
|
68
|
+
return {
|
|
69
|
+
indeterminate: isIndeterminate,
|
|
70
|
+
checked: isChecked,
|
|
71
|
+
setChecked: setIsChecked,
|
|
72
|
+
focused: isFocused,
|
|
73
|
+
setFocused: setIsFocused,
|
|
74
|
+
focusVisible: isFocusVisible,
|
|
75
|
+
setFocusVisible: setIsFocusVisible,
|
|
76
|
+
refs,
|
|
77
|
+
stateProps,
|
|
78
|
+
rootProps: domUtils.labelProps({
|
|
79
|
+
...stateProps,
|
|
80
|
+
onPointerMove () {
|
|
81
|
+
setIsHovered(true);
|
|
82
|
+
},
|
|
83
|
+
onPointerDown () {
|
|
84
|
+
setIsActive(true);
|
|
85
|
+
},
|
|
86
|
+
onPointerUp () {
|
|
87
|
+
setIsActive(false);
|
|
88
|
+
},
|
|
89
|
+
onPointerLeave () {
|
|
90
|
+
setIsHovered(false);
|
|
91
|
+
setIsActive(false);
|
|
92
|
+
}
|
|
93
|
+
}),
|
|
94
|
+
controlProps: domUtils.elementProps({
|
|
95
|
+
...stateProps,
|
|
96
|
+
"aria-hidden": true
|
|
97
|
+
}),
|
|
98
|
+
hiddenInputProps: domUtils.inputProps({
|
|
99
|
+
type: "checkbox",
|
|
100
|
+
role: "checkbox",
|
|
101
|
+
checked: isControlled ? isChecked : undefined,
|
|
102
|
+
defaultChecked: !isControlled ? isChecked : undefined,
|
|
103
|
+
disabled: props.disabled,
|
|
104
|
+
required: props.required,
|
|
105
|
+
"aria-invalid": props.invalid,
|
|
106
|
+
style: domUtils.visuallyHidden,
|
|
107
|
+
...stateProps,
|
|
108
|
+
onChange (event) {
|
|
109
|
+
setIsChecked(event.currentTarget.checked);
|
|
110
|
+
setIsFocusVisible(event.target.matches(":focus-visible"));
|
|
111
|
+
},
|
|
112
|
+
onFocus (event) {
|
|
113
|
+
setIsFocused(true);
|
|
114
|
+
setIsFocusVisible(event.target.matches(":focus-visible"));
|
|
115
|
+
},
|
|
116
|
+
onBlur () {
|
|
117
|
+
setIsFocused(false);
|
|
118
|
+
setIsFocusVisible(false);
|
|
119
|
+
},
|
|
120
|
+
onKeyDown (event) {
|
|
121
|
+
if (event.key === " ") {
|
|
122
|
+
setIsActive(true);
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
onKeyUp (event) {
|
|
126
|
+
if (event.key === " ") {
|
|
127
|
+
setIsActive(false);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
})
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const CheckboxContext = /*#__PURE__*/ react.createContext(null);
|
|
135
|
+
const CheckboxProvider = CheckboxContext.Provider;
|
|
136
|
+
function useCheckboxContext({ strict = true } = {}) {
|
|
137
|
+
const context = react.useContext(CheckboxContext);
|
|
138
|
+
if (!context && strict) {
|
|
139
|
+
throw new Error("useCheckboxContext must be used within a Checkbox");
|
|
140
|
+
}
|
|
141
|
+
return context;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const CheckboxRoot = /*#__PURE__*/ react.forwardRef((props, ref)=>{
|
|
145
|
+
const { checked, defaultChecked, onCheckedChange, indeterminate, disabled, invalid, required, ...otherProps } = props;
|
|
146
|
+
const api = useCheckbox({
|
|
147
|
+
checked,
|
|
148
|
+
defaultChecked,
|
|
149
|
+
onCheckedChange,
|
|
150
|
+
indeterminate,
|
|
151
|
+
disabled,
|
|
152
|
+
invalid,
|
|
153
|
+
required
|
|
154
|
+
});
|
|
155
|
+
const mergedProps = domUtils.mergeProps(api.rootProps, otherProps);
|
|
156
|
+
return /*#__PURE__*/ jsxRuntime.jsx(CheckboxProvider, {
|
|
157
|
+
value: api,
|
|
158
|
+
children: /*#__PURE__*/ jsxRuntime.jsx(reactPrimitive.Primitive.label, {
|
|
159
|
+
ref: ref,
|
|
160
|
+
...mergedProps
|
|
161
|
+
})
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
CheckboxRoot.displayName = "CheckboxRoot";
|
|
165
|
+
const CheckboxControl = /*#__PURE__*/ react.forwardRef((props, ref)=>{
|
|
166
|
+
const { controlProps } = useCheckboxContext();
|
|
167
|
+
const mergedProps = domUtils.mergeProps(controlProps, props);
|
|
168
|
+
return /*#__PURE__*/ jsxRuntime.jsx(reactPrimitive.Primitive.div, {
|
|
169
|
+
ref: ref,
|
|
170
|
+
...mergedProps
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
CheckboxControl.displayName = "CheckboxControl";
|
|
174
|
+
const CheckboxHiddenInput = /*#__PURE__*/ react.forwardRef((props, ref)=>{
|
|
175
|
+
const { refs, hiddenInputProps } = useCheckboxContext();
|
|
176
|
+
const mergedProps = domUtils.mergeProps(hiddenInputProps, props);
|
|
177
|
+
return /*#__PURE__*/ jsxRuntime.jsx(reactPrimitive.Primitive.input, {
|
|
178
|
+
ref: reactComposeRefs.composeRefs(refs.input, ref),
|
|
179
|
+
...mergedProps
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
CheckboxHiddenInput.displayName = "CheckboxHiddenInput";
|
|
183
|
+
|
|
184
|
+
exports.CheckboxControl = CheckboxControl;
|
|
185
|
+
exports.CheckboxHiddenInput = CheckboxHiddenInput;
|
|
186
|
+
exports.CheckboxRoot = CheckboxRoot;
|
|
187
|
+
exports.useCheckboxContext = useCheckboxContext;
|
package/lib/index.cjs
CHANGED
|
@@ -1,194 +1,14 @@
|
|
|
1
|
-
var
|
|
2
|
-
var domUtils = require('@seed-design/dom-utils');
|
|
3
|
-
var reactPrimitive = require('@seed-design/react-primitive');
|
|
4
|
-
var react = require('react');
|
|
5
|
-
var reactUseControllableState = require('@radix-ui/react-use-controllable-state');
|
|
6
|
-
var reactComposeRefs = require('@radix-ui/react-compose-refs');
|
|
7
|
-
|
|
8
|
-
function useCheckboxState(props) {
|
|
9
|
-
const [isChecked = false, setIsChecked] = reactUseControllableState.useControllableState({
|
|
10
|
-
prop: props.checked,
|
|
11
|
-
defaultProp: props.defaultChecked,
|
|
12
|
-
onChange: props.onCheckedChange
|
|
13
|
-
});
|
|
14
|
-
const [isHovered, setIsHovered] = react.useState(false);
|
|
15
|
-
const [isActive, setIsActive] = react.useState(false);
|
|
16
|
-
const [isFocused, setIsFocused] = react.useState(false);
|
|
17
|
-
const [isFocusVisible, setIsFocusVisible] = react.useState(false);
|
|
18
|
-
const inputRef = react.useRef(null);
|
|
19
|
-
const initialCheckedRef = react.useRef(isChecked);
|
|
20
|
-
react.useEffect(()=>{
|
|
21
|
-
const form = inputRef.current?.form;
|
|
22
|
-
if (form) {
|
|
23
|
-
const reset = ()=>setIsChecked(initialCheckedRef.current);
|
|
24
|
-
form.addEventListener("reset", reset);
|
|
25
|
-
return ()=>form.removeEventListener("reset", reset);
|
|
26
|
-
}
|
|
27
|
-
}, [
|
|
28
|
-
setIsChecked
|
|
29
|
-
]);
|
|
30
|
-
react.useEffect(()=>{
|
|
31
|
-
if (!inputRef.current) return;
|
|
32
|
-
inputRef.current.indeterminate = props.indeterminate ?? false;
|
|
33
|
-
}, [
|
|
34
|
-
props.indeterminate
|
|
35
|
-
]);
|
|
36
|
-
return {
|
|
37
|
-
refs: {
|
|
38
|
-
input: inputRef
|
|
39
|
-
},
|
|
40
|
-
isIndeterminate: props.indeterminate ?? false,
|
|
41
|
-
isChecked,
|
|
42
|
-
setIsChecked,
|
|
43
|
-
isHovered,
|
|
44
|
-
setIsHovered,
|
|
45
|
-
isActive,
|
|
46
|
-
setIsActive,
|
|
47
|
-
isFocused,
|
|
48
|
-
setIsFocused,
|
|
49
|
-
isFocusVisible,
|
|
50
|
-
setIsFocusVisible
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
function useCheckbox(props) {
|
|
54
|
-
const { refs, isIndeterminate, setIsChecked, isChecked, setIsHovered, isHovered, setIsActive, isActive, setIsFocused, isFocused, setIsFocusVisible, isFocusVisible } = useCheckboxState(props);
|
|
55
|
-
const stateProps = domUtils.elementProps({
|
|
56
|
-
"data-checked": domUtils.dataAttr(isChecked),
|
|
57
|
-
"data-indeterminate": domUtils.dataAttr(isIndeterminate),
|
|
58
|
-
"data-hover": domUtils.dataAttr(isHovered),
|
|
59
|
-
"data-active": domUtils.dataAttr(isActive),
|
|
60
|
-
"data-focus": domUtils.dataAttr(isFocused),
|
|
61
|
-
"data-focus-visible": domUtils.dataAttr(isFocusVisible),
|
|
62
|
-
"data-disabled": domUtils.dataAttr(props.disabled),
|
|
63
|
-
"data-invalid": domUtils.dataAttr(props.invalid),
|
|
64
|
-
"data-required": domUtils.dataAttr(props.required)
|
|
65
|
-
});
|
|
66
|
-
const isControlled = props.checked != null;
|
|
67
|
-
return {
|
|
68
|
-
indeterminate: isIndeterminate,
|
|
69
|
-
checked: isChecked,
|
|
70
|
-
setChecked: setIsChecked,
|
|
71
|
-
focused: isFocused,
|
|
72
|
-
setFocused: setIsFocused,
|
|
73
|
-
focusVisible: isFocusVisible,
|
|
74
|
-
setFocusVisible: setIsFocusVisible,
|
|
75
|
-
refs,
|
|
76
|
-
stateProps,
|
|
77
|
-
rootProps: domUtils.labelProps({
|
|
78
|
-
...stateProps,
|
|
79
|
-
onPointerMove () {
|
|
80
|
-
setIsHovered(true);
|
|
81
|
-
},
|
|
82
|
-
onPointerDown () {
|
|
83
|
-
setIsActive(true);
|
|
84
|
-
},
|
|
85
|
-
onPointerUp () {
|
|
86
|
-
setIsActive(false);
|
|
87
|
-
},
|
|
88
|
-
onPointerLeave () {
|
|
89
|
-
setIsHovered(false);
|
|
90
|
-
setIsActive(false);
|
|
91
|
-
}
|
|
92
|
-
}),
|
|
93
|
-
controlProps: domUtils.elementProps({
|
|
94
|
-
...stateProps,
|
|
95
|
-
"aria-hidden": true
|
|
96
|
-
}),
|
|
97
|
-
hiddenInputProps: domUtils.inputProps({
|
|
98
|
-
type: "checkbox",
|
|
99
|
-
role: "checkbox",
|
|
100
|
-
checked: isControlled ? isChecked : undefined,
|
|
101
|
-
defaultChecked: !isControlled ? isChecked : undefined,
|
|
102
|
-
disabled: props.disabled,
|
|
103
|
-
required: props.required,
|
|
104
|
-
"aria-invalid": props.invalid,
|
|
105
|
-
style: domUtils.visuallyHidden,
|
|
106
|
-
...stateProps,
|
|
107
|
-
onChange (event) {
|
|
108
|
-
setIsChecked(event.currentTarget.checked);
|
|
109
|
-
setIsFocusVisible(event.target.matches(":focus-visible"));
|
|
110
|
-
},
|
|
111
|
-
onFocus (event) {
|
|
112
|
-
setIsFocused(true);
|
|
113
|
-
setIsFocusVisible(event.target.matches(":focus-visible"));
|
|
114
|
-
},
|
|
115
|
-
onBlur () {
|
|
116
|
-
setIsFocused(false);
|
|
117
|
-
setIsFocusVisible(false);
|
|
118
|
-
},
|
|
119
|
-
onKeyDown (event) {
|
|
120
|
-
if (event.key === " ") {
|
|
121
|
-
setIsActive(true);
|
|
122
|
-
}
|
|
123
|
-
},
|
|
124
|
-
onKeyUp (event) {
|
|
125
|
-
if (event.key === " ") {
|
|
126
|
-
setIsActive(false);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
})
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const CheckboxContext = /*#__PURE__*/ react.createContext(null);
|
|
134
|
-
const CheckboxProvider = CheckboxContext.Provider;
|
|
135
|
-
function useCheckboxContext({ strict = true } = {}) {
|
|
136
|
-
const context = react.useContext(CheckboxContext);
|
|
137
|
-
if (!context && strict) {
|
|
138
|
-
throw new Error("useCheckboxContext must be used within a Checkbox");
|
|
139
|
-
}
|
|
140
|
-
return context;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const CheckboxRoot = /*#__PURE__*/ react.forwardRef((props, ref)=>{
|
|
144
|
-
const { checked, defaultChecked, onCheckedChange, indeterminate, disabled, invalid, required, ...otherProps } = props;
|
|
145
|
-
const api = useCheckbox({
|
|
146
|
-
checked,
|
|
147
|
-
defaultChecked,
|
|
148
|
-
onCheckedChange,
|
|
149
|
-
indeterminate,
|
|
150
|
-
disabled,
|
|
151
|
-
invalid,
|
|
152
|
-
required
|
|
153
|
-
});
|
|
154
|
-
const mergedProps = domUtils.mergeProps(api.rootProps, otherProps);
|
|
155
|
-
return /*#__PURE__*/ jsxRuntime.jsx(CheckboxProvider, {
|
|
156
|
-
value: api,
|
|
157
|
-
children: /*#__PURE__*/ jsxRuntime.jsx(reactPrimitive.Primitive.label, {
|
|
158
|
-
ref: ref,
|
|
159
|
-
...mergedProps
|
|
160
|
-
})
|
|
161
|
-
});
|
|
162
|
-
});
|
|
163
|
-
CheckboxRoot.displayName = "CheckboxRoot";
|
|
164
|
-
const CheckboxControl = /*#__PURE__*/ react.forwardRef((props, ref)=>{
|
|
165
|
-
const { controlProps } = useCheckboxContext();
|
|
166
|
-
const mergedProps = domUtils.mergeProps(controlProps, props);
|
|
167
|
-
return /*#__PURE__*/ jsxRuntime.jsx(reactPrimitive.Primitive.div, {
|
|
168
|
-
ref: ref,
|
|
169
|
-
...mergedProps
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
CheckboxControl.displayName = "CheckboxControl";
|
|
173
|
-
const CheckboxHiddenInput = /*#__PURE__*/ react.forwardRef((props, ref)=>{
|
|
174
|
-
const { refs, hiddenInputProps } = useCheckboxContext();
|
|
175
|
-
const mergedProps = domUtils.mergeProps(hiddenInputProps, props);
|
|
176
|
-
return /*#__PURE__*/ jsxRuntime.jsx(reactPrimitive.Primitive.input, {
|
|
177
|
-
ref: reactComposeRefs.composeRefs(refs.input, ref),
|
|
178
|
-
...mergedProps
|
|
179
|
-
});
|
|
180
|
-
});
|
|
181
|
-
CheckboxHiddenInput.displayName = "CheckboxHiddenInput";
|
|
1
|
+
var Checkbox12s = require('./Checkbox-12s-Y3jZEb8V.cjs');
|
|
182
2
|
|
|
183
3
|
var Checkbox_namespace = {
|
|
184
4
|
__proto__: null,
|
|
185
|
-
Control: CheckboxControl,
|
|
186
|
-
HiddenInput: CheckboxHiddenInput,
|
|
187
|
-
Root: CheckboxRoot
|
|
5
|
+
Control: Checkbox12s.CheckboxControl,
|
|
6
|
+
HiddenInput: Checkbox12s.CheckboxHiddenInput,
|
|
7
|
+
Root: Checkbox12s.CheckboxRoot
|
|
188
8
|
};
|
|
189
9
|
|
|
10
|
+
exports.CheckboxControl = Checkbox12s.CheckboxControl;
|
|
11
|
+
exports.CheckboxHiddenInput = Checkbox12s.CheckboxHiddenInput;
|
|
12
|
+
exports.CheckboxRoot = Checkbox12s.CheckboxRoot;
|
|
13
|
+
exports.useCheckboxContext = Checkbox12s.useCheckboxContext;
|
|
190
14
|
exports.Checkbox = Checkbox_namespace;
|
|
191
|
-
exports.CheckboxControl = CheckboxControl;
|
|
192
|
-
exports.CheckboxHiddenInput = CheckboxHiddenInput;
|
|
193
|
-
exports.CheckboxRoot = CheckboxRoot;
|
|
194
|
-
exports.useCheckboxContext = useCheckboxContext;
|
package/lib/index.js
CHANGED
|
@@ -1,184 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import { Primitive } from '@seed-design/react-primitive';
|
|
4
|
-
import { useState, useRef, useEffect, createContext, useContext, forwardRef } from 'react';
|
|
5
|
-
import { useControllableState } from '@radix-ui/react-use-controllable-state';
|
|
6
|
-
import { composeRefs } from '@radix-ui/react-compose-refs';
|
|
7
|
-
|
|
8
|
-
function useCheckboxState(props) {
|
|
9
|
-
const [isChecked = false, setIsChecked] = useControllableState({
|
|
10
|
-
prop: props.checked,
|
|
11
|
-
defaultProp: props.defaultChecked,
|
|
12
|
-
onChange: props.onCheckedChange
|
|
13
|
-
});
|
|
14
|
-
const [isHovered, setIsHovered] = useState(false);
|
|
15
|
-
const [isActive, setIsActive] = useState(false);
|
|
16
|
-
const [isFocused, setIsFocused] = useState(false);
|
|
17
|
-
const [isFocusVisible, setIsFocusVisible] = useState(false);
|
|
18
|
-
const inputRef = useRef(null);
|
|
19
|
-
const initialCheckedRef = useRef(isChecked);
|
|
20
|
-
useEffect(()=>{
|
|
21
|
-
const form = inputRef.current?.form;
|
|
22
|
-
if (form) {
|
|
23
|
-
const reset = ()=>setIsChecked(initialCheckedRef.current);
|
|
24
|
-
form.addEventListener("reset", reset);
|
|
25
|
-
return ()=>form.removeEventListener("reset", reset);
|
|
26
|
-
}
|
|
27
|
-
}, [
|
|
28
|
-
setIsChecked
|
|
29
|
-
]);
|
|
30
|
-
useEffect(()=>{
|
|
31
|
-
if (!inputRef.current) return;
|
|
32
|
-
inputRef.current.indeterminate = props.indeterminate ?? false;
|
|
33
|
-
}, [
|
|
34
|
-
props.indeterminate
|
|
35
|
-
]);
|
|
36
|
-
return {
|
|
37
|
-
refs: {
|
|
38
|
-
input: inputRef
|
|
39
|
-
},
|
|
40
|
-
isIndeterminate: props.indeterminate ?? false,
|
|
41
|
-
isChecked,
|
|
42
|
-
setIsChecked,
|
|
43
|
-
isHovered,
|
|
44
|
-
setIsHovered,
|
|
45
|
-
isActive,
|
|
46
|
-
setIsActive,
|
|
47
|
-
isFocused,
|
|
48
|
-
setIsFocused,
|
|
49
|
-
isFocusVisible,
|
|
50
|
-
setIsFocusVisible
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
function useCheckbox(props) {
|
|
54
|
-
const { refs, isIndeterminate, setIsChecked, isChecked, setIsHovered, isHovered, setIsActive, isActive, setIsFocused, isFocused, setIsFocusVisible, isFocusVisible } = useCheckboxState(props);
|
|
55
|
-
const stateProps = elementProps({
|
|
56
|
-
"data-checked": dataAttr(isChecked),
|
|
57
|
-
"data-indeterminate": dataAttr(isIndeterminate),
|
|
58
|
-
"data-hover": dataAttr(isHovered),
|
|
59
|
-
"data-active": dataAttr(isActive),
|
|
60
|
-
"data-focus": dataAttr(isFocused),
|
|
61
|
-
"data-focus-visible": dataAttr(isFocusVisible),
|
|
62
|
-
"data-disabled": dataAttr(props.disabled),
|
|
63
|
-
"data-invalid": dataAttr(props.invalid),
|
|
64
|
-
"data-required": dataAttr(props.required)
|
|
65
|
-
});
|
|
66
|
-
const isControlled = props.checked != null;
|
|
67
|
-
return {
|
|
68
|
-
indeterminate: isIndeterminate,
|
|
69
|
-
checked: isChecked,
|
|
70
|
-
setChecked: setIsChecked,
|
|
71
|
-
focused: isFocused,
|
|
72
|
-
setFocused: setIsFocused,
|
|
73
|
-
focusVisible: isFocusVisible,
|
|
74
|
-
setFocusVisible: setIsFocusVisible,
|
|
75
|
-
refs,
|
|
76
|
-
stateProps,
|
|
77
|
-
rootProps: labelProps({
|
|
78
|
-
...stateProps,
|
|
79
|
-
onPointerMove () {
|
|
80
|
-
setIsHovered(true);
|
|
81
|
-
},
|
|
82
|
-
onPointerDown () {
|
|
83
|
-
setIsActive(true);
|
|
84
|
-
},
|
|
85
|
-
onPointerUp () {
|
|
86
|
-
setIsActive(false);
|
|
87
|
-
},
|
|
88
|
-
onPointerLeave () {
|
|
89
|
-
setIsHovered(false);
|
|
90
|
-
setIsActive(false);
|
|
91
|
-
}
|
|
92
|
-
}),
|
|
93
|
-
controlProps: elementProps({
|
|
94
|
-
...stateProps,
|
|
95
|
-
"aria-hidden": true
|
|
96
|
-
}),
|
|
97
|
-
hiddenInputProps: inputProps({
|
|
98
|
-
type: "checkbox",
|
|
99
|
-
role: "checkbox",
|
|
100
|
-
checked: isControlled ? isChecked : undefined,
|
|
101
|
-
defaultChecked: !isControlled ? isChecked : undefined,
|
|
102
|
-
disabled: props.disabled,
|
|
103
|
-
required: props.required,
|
|
104
|
-
"aria-invalid": props.invalid,
|
|
105
|
-
style: visuallyHidden,
|
|
106
|
-
...stateProps,
|
|
107
|
-
onChange (event) {
|
|
108
|
-
setIsChecked(event.currentTarget.checked);
|
|
109
|
-
setIsFocusVisible(event.target.matches(":focus-visible"));
|
|
110
|
-
},
|
|
111
|
-
onFocus (event) {
|
|
112
|
-
setIsFocused(true);
|
|
113
|
-
setIsFocusVisible(event.target.matches(":focus-visible"));
|
|
114
|
-
},
|
|
115
|
-
onBlur () {
|
|
116
|
-
setIsFocused(false);
|
|
117
|
-
setIsFocusVisible(false);
|
|
118
|
-
},
|
|
119
|
-
onKeyDown (event) {
|
|
120
|
-
if (event.key === " ") {
|
|
121
|
-
setIsActive(true);
|
|
122
|
-
}
|
|
123
|
-
},
|
|
124
|
-
onKeyUp (event) {
|
|
125
|
-
if (event.key === " ") {
|
|
126
|
-
setIsActive(false);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
})
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const CheckboxContext = /*#__PURE__*/ createContext(null);
|
|
134
|
-
const CheckboxProvider = CheckboxContext.Provider;
|
|
135
|
-
function useCheckboxContext({ strict = true } = {}) {
|
|
136
|
-
const context = useContext(CheckboxContext);
|
|
137
|
-
if (!context && strict) {
|
|
138
|
-
throw new Error("useCheckboxContext must be used within a Checkbox");
|
|
139
|
-
}
|
|
140
|
-
return context;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const CheckboxRoot = /*#__PURE__*/ forwardRef((props, ref)=>{
|
|
144
|
-
const { checked, defaultChecked, onCheckedChange, indeterminate, disabled, invalid, required, ...otherProps } = props;
|
|
145
|
-
const api = useCheckbox({
|
|
146
|
-
checked,
|
|
147
|
-
defaultChecked,
|
|
148
|
-
onCheckedChange,
|
|
149
|
-
indeterminate,
|
|
150
|
-
disabled,
|
|
151
|
-
invalid,
|
|
152
|
-
required
|
|
153
|
-
});
|
|
154
|
-
const mergedProps = mergeProps(api.rootProps, otherProps);
|
|
155
|
-
return /*#__PURE__*/ jsx(CheckboxProvider, {
|
|
156
|
-
value: api,
|
|
157
|
-
children: /*#__PURE__*/ jsx(Primitive.label, {
|
|
158
|
-
ref: ref,
|
|
159
|
-
...mergedProps
|
|
160
|
-
})
|
|
161
|
-
});
|
|
162
|
-
});
|
|
163
|
-
CheckboxRoot.displayName = "CheckboxRoot";
|
|
164
|
-
const CheckboxControl = /*#__PURE__*/ forwardRef((props, ref)=>{
|
|
165
|
-
const { controlProps } = useCheckboxContext();
|
|
166
|
-
const mergedProps = mergeProps(controlProps, props);
|
|
167
|
-
return /*#__PURE__*/ jsx(Primitive.div, {
|
|
168
|
-
ref: ref,
|
|
169
|
-
...mergedProps
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
CheckboxControl.displayName = "CheckboxControl";
|
|
173
|
-
const CheckboxHiddenInput = /*#__PURE__*/ forwardRef((props, ref)=>{
|
|
174
|
-
const { refs, hiddenInputProps } = useCheckboxContext();
|
|
175
|
-
const mergedProps = mergeProps(hiddenInputProps, props);
|
|
176
|
-
return /*#__PURE__*/ jsx(Primitive.input, {
|
|
177
|
-
ref: composeRefs(refs.input, ref),
|
|
178
|
-
...mergedProps
|
|
179
|
-
});
|
|
180
|
-
});
|
|
181
|
-
CheckboxHiddenInput.displayName = "CheckboxHiddenInput";
|
|
1
|
+
import { C as CheckboxControl, a as CheckboxHiddenInput, b as CheckboxRoot } from './Checkbox-12s-BeV5Lkaw.js';
|
|
2
|
+
export { u as useCheckboxContext } from './Checkbox-12s-BeV5Lkaw.js';
|
|
182
3
|
|
|
183
4
|
var Checkbox_namespace = {
|
|
184
5
|
__proto__: null,
|
|
@@ -187,4 +8,4 @@ var Checkbox_namespace = {
|
|
|
187
8
|
Root: CheckboxRoot
|
|
188
9
|
};
|
|
189
10
|
|
|
190
|
-
export { Checkbox_namespace as Checkbox, CheckboxControl, CheckboxHiddenInput, CheckboxRoot
|
|
11
|
+
export { Checkbox_namespace as Checkbox, CheckboxControl, CheckboxHiddenInput, CheckboxRoot };
|
package/package.json
CHANGED