@destyler/checkbox 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @destyler/checkbox
2
+
3
+ [![NPM version](https://img.shields.io/npm/v/@destyler/checkbox?color=a1b858&label=)](https://www.npmjs.com/package/@destyler/checkbox)
4
+
5
+ > a control that allows the user to toggle between checked and not checked.
6
+
7
+ [GitHub](https://github.com/destyler/destyler) | [Documentation](https://destyler-dev.zeabur.app/)
@@ -1,14 +1,212 @@
1
1
  'use strict';
2
2
 
3
- const index = require('./index.cjs');
4
- require('vue');
5
- require('@destyler/primitive');
6
- require('@destyler/composition');
7
- require('@destyler/shared');
8
- require('@destyler/directives');
9
- require('@destyler/presence');
3
+ const vue = require('vue');
4
+ const primitive = require('@destyler/primitive');
5
+ const composition = require('@destyler/composition');
6
+ const shared = require('@destyler/shared');
7
+ const directives = require('@destyler/directives');
8
+ const presence = require('@destyler/presence');
10
9
 
10
+ function isIndeterminate(checked) {
11
+ return checked === "indeterminate";
12
+ }
13
+ function getState(checked) {
14
+ return isIndeterminate(checked) ? "indeterminate" : checked ? "checked" : "unchecked";
15
+ }
11
16
 
17
+ const checkboxRootProps = {
18
+ ...primitive.primitiveProps,
19
+ /**
20
+ * @default button
21
+ */
22
+ as: {
23
+ ...primitive.primitiveProps.as,
24
+ default: "button"
25
+ },
26
+ /**
27
+ * The checked state of the checkbox when it is initially rendered.
28
+ * Use when you do not need to control its checked state.
29
+ */
30
+ defaultChecked: {
31
+ type: Boolean,
32
+ required: false
33
+ },
34
+ /**
35
+ * The controlled checked state of the checkbox. Can be binded with `v-model`.
36
+ *
37
+ * @default undefined
38
+ */
39
+ checked: {
40
+ type: [Boolean, String],
41
+ required: false,
42
+ default: void 0
43
+ },
44
+ /**
45
+ * When `true`, prevents the user from interacting with the checkbox.
46
+ *
47
+ * @default false
48
+ */
49
+ disabled: {
50
+ type: Boolean,
51
+ required: false,
52
+ default: false
53
+ },
54
+ /**
55
+ * When `true`, indicates that the user must check the checkbox before the owning form can be submitted.
56
+ */
57
+ required: {
58
+ type: Boolean,
59
+ required: false
60
+ },
61
+ /**
62
+ * The name of the checkbox. Submitted with its owning form as part of a name/value pair.
63
+ */
64
+ name: {
65
+ type: String,
66
+ required: false
67
+ },
68
+ /**
69
+ * The value given as data when submitted with a `name`.
70
+ *
71
+ * @default on
72
+ */
73
+ value: {
74
+ type: String,
75
+ required: false,
76
+ default: "on"
77
+ },
78
+ /**
79
+ * Id of the element
80
+ */
81
+ id: {
82
+ type: String,
83
+ required: false
84
+ }
85
+ };
86
+ const checkboxRootEmits = {
87
+ /**
88
+ * Event handler called when the checked state of the checkbox changes.
89
+ */
90
+ "update:checked": (_checked) => true
91
+ };
92
+ const [injectCheckboxRootContext, provideCheckboxRootContext] = shared.createContext("DestylerCheckboxRoot");
93
+ const CheckboxRoot = vue.defineComponent({
94
+ name: "DestylerCheckboxRoot",
95
+ props: checkboxRootProps,
96
+ emits: checkboxRootEmits,
97
+ setup(props, { emit }) {
98
+ const disabledRef = vue.toRef(props.disabled);
99
+ const checkedRef = composition.useVModel(props, "checked", emit, {
100
+ defaultValue: props.defaultChecked,
101
+ passive: props.checked === void 0
102
+ });
103
+ const { forwardRef, currentElement } = composition.useForwardExpose();
104
+ const isFormControl = composition.useFormControl(currentElement);
105
+ const ariaLabel = vue.computed(() => props.id && currentElement.value ? document.querySelector(`[for="${props.id}"]`)?.textContent : void 0);
106
+ provideCheckboxRootContext({
107
+ disabled: disabledRef,
108
+ state: checkedRef
109
+ });
110
+ return {
111
+ disabled: disabledRef,
112
+ checked: checkedRef,
113
+ forwardRef,
114
+ isFormControl,
115
+ ariaLabel
116
+ };
117
+ },
118
+ render() {
119
+ const useVShow = this.isFormControl;
120
+ return [
121
+ vue.withDirectives(vue.h(primitive.Primitive, vue.mergeProps(this.$attrs, {
122
+ "ref": (el) => this.forwardRef(el),
123
+ "role": "checkbox",
124
+ "asChild": this.$props.asChild,
125
+ "as": this.$props.as,
126
+ "type": this.$props.as === "button" ? "button" : void 0,
127
+ "aria-checked": isIndeterminate(this.checked) ? "mixed" : this.checked,
128
+ "aria-required": false,
129
+ "aria-label": this.$attrs["aria-label"] || this.ariaLabel,
130
+ "data-state": getState(this.checked),
131
+ "data-disabled": this.disabled ? "" : void 0,
132
+ "disabled": this.disabled,
133
+ "onClick": () => {
134
+ this.checked = !this.checked;
135
+ }
136
+ }), () => this.$slots.default?.()), [
137
+ [directives.BindOnceDirective, { id: this.$props.id }]
138
+ ]),
139
+ useVShow ? vue.h("input", {
140
+ "type": "checkbox",
141
+ "tabindex": "-1",
142
+ "aria-hidden": "",
143
+ "value": this.$props.value,
144
+ "checked": this.checked,
145
+ "name": this.$props.name,
146
+ "disabled": this.disabled,
147
+ "required": this.$props.required,
148
+ "style": {
149
+ transform: "translateX(-100%)",
150
+ position: "absolute",
151
+ pointerEvents: "none",
152
+ opacity: 0,
153
+ margin: 0
154
+ }
155
+ }) : null
156
+ ];
157
+ }
158
+ });
12
159
 
13
- exports.DestylerCheckboxIndicator = index.DestylerCheckboxIndicator;
14
- exports.DestylerCheckboxRoot = index.DestylerCheckboxRoot;
160
+ const checkboxIndicatorProps = {
161
+ ...primitive.primitiveProps,
162
+ /**
163
+ * @default span
164
+ */
165
+ as: {
166
+ ...primitive.primitiveProps.as,
167
+ default: "span"
168
+ },
169
+ /**
170
+ * Used to force mounting when more control is needed.
171
+ * Useful when controlling animation with animation libraries.
172
+ *
173
+ * @default false
174
+ */
175
+ forceMount: {
176
+ type: Boolean,
177
+ required: false,
178
+ default: false
179
+ }
180
+ };
181
+ const CheckboxIndicator = vue.defineComponent({
182
+ name: "DestylerCheckboxIndicator",
183
+ props: checkboxIndicatorProps,
184
+ setup() {
185
+ const rootContext = injectCheckboxRootContext();
186
+ const { forwardRef } = composition.useForwardExpose();
187
+ return {
188
+ rootContext,
189
+ forwardRef
190
+ };
191
+ },
192
+ render() {
193
+ return vue.h(presence.Presence, {
194
+ present: this.$props.forceMount || isIndeterminate(this.rootContext.state.value) || this.rootContext.state.value === true
195
+ }, () => vue.h(primitive.Primitive, vue.mergeProps(this.$attrs, {
196
+ "ref": (el) => this.forwardRef(el),
197
+ "data-state": getState(this.rootContext.state.value),
198
+ "data-disabled": this.rootContext.disabled.value ? "" : void 0,
199
+ "asChild": this.$props.asChild,
200
+ "as": this.$props.as,
201
+ "style": {
202
+ pointerEvents: "none"
203
+ }
204
+ }), () => this.$slots.default?.()));
205
+ }
206
+ });
207
+
208
+ exports.CheckboxIndicator = CheckboxIndicator;
209
+ exports.CheckboxRoot = CheckboxRoot;
210
+ exports.checkboxIndicatorProps = checkboxIndicatorProps;
211
+ exports.checkboxRootEmits = checkboxRootEmits;
212
+ exports.checkboxRootProps = checkboxRootProps;
@@ -1,4 +1,346 @@
1
- export { DestylerCheckboxIndicator, DestylerCheckboxIndicatorProps, DestylerCheckboxRoot, DestylerCheckboxRootProps } from './index.cjs';
2
- import 'vue';
3
- import '@destyler/primitive';
4
- import '@destyler/shared';
1
+ import * as vue from 'vue';
2
+ import { PropType, Ref } from 'vue';
3
+ import * as _destyler_primitive from '@destyler/primitive';
4
+ import { ExtractPublicPropTypes } from '@destyler/shared';
5
+
6
+ type CheckedState = boolean | 'indeterminate';
7
+
8
+ declare const checkboxRootProps: {
9
+ /**
10
+ * @default button
11
+ */
12
+ readonly as: {
13
+ readonly default: "button";
14
+ readonly type: PropType<_destyler_primitive.AsTag | vue.Component>;
15
+ readonly required: false;
16
+ };
17
+ /**
18
+ * The checked state of the checkbox when it is initially rendered.
19
+ * Use when you do not need to control its checked state.
20
+ */
21
+ readonly defaultChecked: {
22
+ readonly type: PropType<boolean>;
23
+ readonly required: false;
24
+ };
25
+ /**
26
+ * The controlled checked state of the checkbox. Can be binded with `v-model`.
27
+ *
28
+ * @default undefined
29
+ */
30
+ readonly checked: {
31
+ readonly type: PropType<boolean | "indeterminate">;
32
+ readonly required: false;
33
+ readonly default: undefined;
34
+ };
35
+ /**
36
+ * When `true`, prevents the user from interacting with the checkbox.
37
+ *
38
+ * @default false
39
+ */
40
+ readonly disabled: {
41
+ readonly type: PropType<boolean>;
42
+ readonly required: false;
43
+ readonly default: false;
44
+ };
45
+ /**
46
+ * When `true`, indicates that the user must check the checkbox before the owning form can be submitted.
47
+ */
48
+ readonly required: {
49
+ readonly type: PropType<boolean>;
50
+ readonly required: false;
51
+ };
52
+ /**
53
+ * The name of the checkbox. Submitted with its owning form as part of a name/value pair.
54
+ */
55
+ readonly name: {
56
+ readonly type: PropType<string>;
57
+ readonly required: false;
58
+ };
59
+ /**
60
+ * The value given as data when submitted with a `name`.
61
+ *
62
+ * @default on
63
+ */
64
+ readonly value: {
65
+ readonly type: PropType<string>;
66
+ readonly required: false;
67
+ readonly default: "on";
68
+ };
69
+ /**
70
+ * Id of the element
71
+ */
72
+ readonly id: {
73
+ readonly type: PropType<string>;
74
+ readonly required: false;
75
+ };
76
+ readonly asChild: {
77
+ readonly type: PropType<boolean>;
78
+ readonly required: false;
79
+ readonly default: false;
80
+ };
81
+ };
82
+ type CheckboxRootProps = ExtractPublicPropTypes<typeof checkboxRootProps>;
83
+ declare const checkboxRootEmits: {
84
+ /**
85
+ * Event handler called when the checked state of the checkbox changes.
86
+ */
87
+ 'update:checked': (_checked: boolean) => boolean;
88
+ };
89
+ interface CheckboxRootContext {
90
+ disabled: Ref<boolean>;
91
+ state: Ref<CheckedState>;
92
+ }
93
+ declare const CheckboxRoot: vue.DefineComponent<{
94
+ /**
95
+ * @default button
96
+ */
97
+ readonly as: {
98
+ readonly default: "button";
99
+ readonly type: PropType<_destyler_primitive.AsTag | vue.Component>;
100
+ readonly required: false;
101
+ };
102
+ /**
103
+ * The checked state of the checkbox when it is initially rendered.
104
+ * Use when you do not need to control its checked state.
105
+ */
106
+ readonly defaultChecked: {
107
+ readonly type: PropType<boolean>;
108
+ readonly required: false;
109
+ };
110
+ /**
111
+ * The controlled checked state of the checkbox. Can be binded with `v-model`.
112
+ *
113
+ * @default undefined
114
+ */
115
+ readonly checked: {
116
+ readonly type: PropType<boolean | "indeterminate">;
117
+ readonly required: false;
118
+ readonly default: undefined;
119
+ };
120
+ /**
121
+ * When `true`, prevents the user from interacting with the checkbox.
122
+ *
123
+ * @default false
124
+ */
125
+ readonly disabled: {
126
+ readonly type: PropType<boolean>;
127
+ readonly required: false;
128
+ readonly default: false;
129
+ };
130
+ /**
131
+ * When `true`, indicates that the user must check the checkbox before the owning form can be submitted.
132
+ */
133
+ readonly required: {
134
+ readonly type: PropType<boolean>;
135
+ readonly required: false;
136
+ };
137
+ /**
138
+ * The name of the checkbox. Submitted with its owning form as part of a name/value pair.
139
+ */
140
+ readonly name: {
141
+ readonly type: PropType<string>;
142
+ readonly required: false;
143
+ };
144
+ /**
145
+ * The value given as data when submitted with a `name`.
146
+ *
147
+ * @default on
148
+ */
149
+ readonly value: {
150
+ readonly type: PropType<string>;
151
+ readonly required: false;
152
+ readonly default: "on";
153
+ };
154
+ /**
155
+ * Id of the element
156
+ */
157
+ readonly id: {
158
+ readonly type: PropType<string>;
159
+ readonly required: false;
160
+ };
161
+ readonly asChild: {
162
+ readonly type: PropType<boolean>;
163
+ readonly required: false;
164
+ readonly default: false;
165
+ };
166
+ }, {
167
+ disabled: Ref<false> | Ref<true>;
168
+ checked: Ref<CheckedState>;
169
+ forwardRef: (ref: Element | vue.ComponentPublicInstance | null) => void;
170
+ isFormControl: vue.ComputedRef<boolean>;
171
+ ariaLabel: vue.ComputedRef<string | null | undefined>;
172
+ }, unknown, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {
173
+ /**
174
+ * Event handler called when the checked state of the checkbox changes.
175
+ */
176
+ 'update:checked': (_checked: boolean) => boolean;
177
+ }, string, vue.PublicProps, Readonly<vue.ExtractPropTypes<{
178
+ /**
179
+ * @default button
180
+ */
181
+ readonly as: {
182
+ readonly default: "button";
183
+ readonly type: PropType<_destyler_primitive.AsTag | vue.Component>;
184
+ readonly required: false;
185
+ };
186
+ /**
187
+ * The checked state of the checkbox when it is initially rendered.
188
+ * Use when you do not need to control its checked state.
189
+ */
190
+ readonly defaultChecked: {
191
+ readonly type: PropType<boolean>;
192
+ readonly required: false;
193
+ };
194
+ /**
195
+ * The controlled checked state of the checkbox. Can be binded with `v-model`.
196
+ *
197
+ * @default undefined
198
+ */
199
+ readonly checked: {
200
+ readonly type: PropType<boolean | "indeterminate">;
201
+ readonly required: false;
202
+ readonly default: undefined;
203
+ };
204
+ /**
205
+ * When `true`, prevents the user from interacting with the checkbox.
206
+ *
207
+ * @default false
208
+ */
209
+ readonly disabled: {
210
+ readonly type: PropType<boolean>;
211
+ readonly required: false;
212
+ readonly default: false;
213
+ };
214
+ /**
215
+ * When `true`, indicates that the user must check the checkbox before the owning form can be submitted.
216
+ */
217
+ readonly required: {
218
+ readonly type: PropType<boolean>;
219
+ readonly required: false;
220
+ };
221
+ /**
222
+ * The name of the checkbox. Submitted with its owning form as part of a name/value pair.
223
+ */
224
+ readonly name: {
225
+ readonly type: PropType<string>;
226
+ readonly required: false;
227
+ };
228
+ /**
229
+ * The value given as data when submitted with a `name`.
230
+ *
231
+ * @default on
232
+ */
233
+ readonly value: {
234
+ readonly type: PropType<string>;
235
+ readonly required: false;
236
+ readonly default: "on";
237
+ };
238
+ /**
239
+ * Id of the element
240
+ */
241
+ readonly id: {
242
+ readonly type: PropType<string>;
243
+ readonly required: false;
244
+ };
245
+ readonly asChild: {
246
+ readonly type: PropType<boolean>;
247
+ readonly required: false;
248
+ readonly default: false;
249
+ };
250
+ }>> & {
251
+ "onUpdate:checked"?: ((_checked: boolean) => any) | undefined;
252
+ }, {
253
+ readonly checked: boolean | "indeterminate";
254
+ readonly as: _destyler_primitive.AsTag | vue.Component;
255
+ readonly disabled: boolean;
256
+ readonly value: string;
257
+ readonly asChild: boolean;
258
+ }, {}>;
259
+
260
+ declare const checkboxIndicatorProps: {
261
+ /**
262
+ * @default span
263
+ */
264
+ readonly as: {
265
+ readonly default: "span";
266
+ readonly type: PropType<_destyler_primitive.AsTag | vue.Component>;
267
+ readonly required: false;
268
+ };
269
+ /**
270
+ * Used to force mounting when more control is needed.
271
+ * Useful when controlling animation with animation libraries.
272
+ *
273
+ * @default false
274
+ */
275
+ readonly forceMount: {
276
+ readonly type: PropType<boolean>;
277
+ readonly required: false;
278
+ readonly default: false;
279
+ };
280
+ readonly asChild: {
281
+ readonly type: PropType<boolean>;
282
+ readonly required: false;
283
+ readonly default: false;
284
+ };
285
+ };
286
+ type CheckboxIndicatorProps = ExtractPublicPropTypes<typeof checkboxIndicatorProps>;
287
+ declare const CheckboxIndicator: vue.DefineComponent<{
288
+ /**
289
+ * @default span
290
+ */
291
+ readonly as: {
292
+ readonly default: "span";
293
+ readonly type: PropType<_destyler_primitive.AsTag | vue.Component>;
294
+ readonly required: false;
295
+ };
296
+ /**
297
+ * Used to force mounting when more control is needed.
298
+ * Useful when controlling animation with animation libraries.
299
+ *
300
+ * @default false
301
+ */
302
+ readonly forceMount: {
303
+ readonly type: PropType<boolean>;
304
+ readonly required: false;
305
+ readonly default: false;
306
+ };
307
+ readonly asChild: {
308
+ readonly type: PropType<boolean>;
309
+ readonly required: false;
310
+ readonly default: false;
311
+ };
312
+ }, {
313
+ rootContext: CheckboxRootContext;
314
+ forwardRef: (ref: Element | vue.ComponentPublicInstance | null) => void;
315
+ }, unknown, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {}, string, vue.PublicProps, Readonly<vue.ExtractPropTypes<{
316
+ /**
317
+ * @default span
318
+ */
319
+ readonly as: {
320
+ readonly default: "span";
321
+ readonly type: PropType<_destyler_primitive.AsTag | vue.Component>;
322
+ readonly required: false;
323
+ };
324
+ /**
325
+ * Used to force mounting when more control is needed.
326
+ * Useful when controlling animation with animation libraries.
327
+ *
328
+ * @default false
329
+ */
330
+ readonly forceMount: {
331
+ readonly type: PropType<boolean>;
332
+ readonly required: false;
333
+ readonly default: false;
334
+ };
335
+ readonly asChild: {
336
+ readonly type: PropType<boolean>;
337
+ readonly required: false;
338
+ readonly default: false;
339
+ };
340
+ }>>, {
341
+ readonly as: _destyler_primitive.AsTag | vue.Component;
342
+ readonly asChild: boolean;
343
+ readonly forceMount: boolean;
344
+ }, {}>;
345
+
346
+ export { CheckboxIndicator, type CheckboxIndicatorProps, CheckboxRoot, type CheckboxRootProps, checkboxIndicatorProps, checkboxRootEmits, checkboxRootProps };