@destyler/checkbox 0.0.3 → 0.0.5

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.
@@ -1,7 +1,206 @@
1
- export { DestylerCheckboxIndicator, DestylerCheckboxRoot } from './index.mjs';
2
- import 'vue';
3
- import '@destyler/primitive';
4
- import '@destyler/composition';
5
- import '@destyler/shared';
6
- import '@destyler/directives';
7
- import '@destyler/presence';
1
+ import { defineComponent, toRef, computed, withDirectives, h, mergeProps } from 'vue';
2
+ import { primitiveProps, Primitive } from '@destyler/primitive';
3
+ import { useVModel, useForwardExpose, useFormControl } from '@destyler/composition';
4
+ import { createContext } from '@destyler/shared';
5
+ import { BindOnceDirective } from '@destyler/directives';
6
+ import { Presence } from '@destyler/presence';
7
+
8
+ function isIndeterminate(checked) {
9
+ return checked === "indeterminate";
10
+ }
11
+ function getState(checked) {
12
+ return isIndeterminate(checked) ? "indeterminate" : checked ? "checked" : "unchecked";
13
+ }
14
+
15
+ const checkboxRootProps = {
16
+ ...primitiveProps,
17
+ /**
18
+ * @default button
19
+ */
20
+ as: {
21
+ ...primitiveProps.as,
22
+ default: "button"
23
+ },
24
+ /**
25
+ * The checked state of the checkbox when it is initially rendered.
26
+ * Use when you do not need to control its checked state.
27
+ */
28
+ defaultChecked: {
29
+ type: Boolean,
30
+ required: false
31
+ },
32
+ /**
33
+ * The controlled checked state of the checkbox. Can be binded with `v-model`.
34
+ *
35
+ * @default undefined
36
+ */
37
+ checked: {
38
+ type: [Boolean, String],
39
+ required: false,
40
+ default: void 0
41
+ },
42
+ /**
43
+ * When `true`, prevents the user from interacting with the checkbox.
44
+ *
45
+ * @default false
46
+ */
47
+ disabled: {
48
+ type: Boolean,
49
+ required: false,
50
+ default: false
51
+ },
52
+ /**
53
+ * When `true`, indicates that the user must check the checkbox before the owning form can be submitted.
54
+ */
55
+ required: {
56
+ type: Boolean,
57
+ required: false
58
+ },
59
+ /**
60
+ * The name of the checkbox. Submitted with its owning form as part of a name/value pair.
61
+ */
62
+ name: {
63
+ type: String,
64
+ required: false
65
+ },
66
+ /**
67
+ * The value given as data when submitted with a `name`.
68
+ *
69
+ * @default on
70
+ */
71
+ value: {
72
+ type: String,
73
+ required: false,
74
+ default: "on"
75
+ },
76
+ /**
77
+ * Id of the element
78
+ */
79
+ id: {
80
+ type: String,
81
+ required: false
82
+ }
83
+ };
84
+ const checkboxRootEmits = {
85
+ /**
86
+ * Event handler called when the checked state of the checkbox changes.
87
+ */
88
+ "update:checked": (_checked) => true
89
+ };
90
+ const [injectCheckboxRootContext, provideCheckboxRootContext] = createContext("DestylerCheckboxRoot");
91
+ const CheckboxRoot = defineComponent({
92
+ name: "DestylerCheckboxRoot",
93
+ props: checkboxRootProps,
94
+ emits: checkboxRootEmits,
95
+ setup(props, { emit }) {
96
+ const disabledRef = toRef(props.disabled);
97
+ const checkedRef = useVModel(props, "checked", emit, {
98
+ defaultValue: props.defaultChecked,
99
+ passive: props.checked === void 0
100
+ });
101
+ const { forwardRef, currentElement } = useForwardExpose();
102
+ const isFormControl = useFormControl(currentElement);
103
+ const ariaLabel = computed(() => props.id && currentElement.value ? document.querySelector(`[for="${props.id}"]`)?.textContent : void 0);
104
+ provideCheckboxRootContext({
105
+ disabled: disabledRef,
106
+ state: checkedRef
107
+ });
108
+ return {
109
+ disabled: disabledRef,
110
+ checked: checkedRef,
111
+ forwardRef,
112
+ isFormControl,
113
+ ariaLabel
114
+ };
115
+ },
116
+ render() {
117
+ const useVShow = this.isFormControl;
118
+ return [
119
+ withDirectives(h(Primitive, mergeProps(this.$attrs, {
120
+ "ref": (el) => this.forwardRef(el),
121
+ "role": "checkbox",
122
+ "asChild": this.$props.asChild,
123
+ "as": this.$props.as,
124
+ "type": this.$props.as === "button" ? "button" : void 0,
125
+ "aria-checked": isIndeterminate(this.checked) ? "mixed" : this.checked,
126
+ "aria-required": false,
127
+ "aria-label": this.$attrs["aria-label"] || this.ariaLabel,
128
+ "data-state": getState(this.checked),
129
+ "data-disabled": this.disabled ? "" : void 0,
130
+ "disabled": this.disabled,
131
+ "onClick": () => {
132
+ this.checked = !this.checked;
133
+ }
134
+ }), () => this.$slots.default?.()), [
135
+ [BindOnceDirective, { id: this.$props.id }]
136
+ ]),
137
+ useVShow ? h("input", {
138
+ "type": "checkbox",
139
+ "tabindex": "-1",
140
+ "aria-hidden": "",
141
+ "value": this.$props.value,
142
+ "checked": this.checked,
143
+ "name": this.$props.name,
144
+ "disabled": this.disabled,
145
+ "required": this.$props.required,
146
+ "style": {
147
+ transform: "translateX(-100%)",
148
+ position: "absolute",
149
+ pointerEvents: "none",
150
+ opacity: 0,
151
+ margin: 0
152
+ }
153
+ }) : null
154
+ ];
155
+ }
156
+ });
157
+
158
+ const checkboxIndicatorProps = {
159
+ ...primitiveProps,
160
+ /**
161
+ * @default span
162
+ */
163
+ as: {
164
+ ...primitiveProps.as,
165
+ default: "span"
166
+ },
167
+ /**
168
+ * Used to force mounting when more control is needed.
169
+ * Useful when controlling animation with animation libraries.
170
+ *
171
+ * @default false
172
+ */
173
+ forceMount: {
174
+ type: Boolean,
175
+ required: false,
176
+ default: false
177
+ }
178
+ };
179
+ const CheckboxIndicator = defineComponent({
180
+ name: "DestylerCheckboxIndicator",
181
+ props: checkboxIndicatorProps,
182
+ setup() {
183
+ const rootContext = injectCheckboxRootContext();
184
+ const { forwardRef } = useForwardExpose();
185
+ return {
186
+ rootContext,
187
+ forwardRef
188
+ };
189
+ },
190
+ render() {
191
+ return h(Presence, {
192
+ present: this.$props.forceMount || isIndeterminate(this.rootContext.state.value) || this.rootContext.state.value === true
193
+ }, () => h(Primitive, mergeProps(this.$attrs, {
194
+ "ref": (el) => this.forwardRef(el),
195
+ "data-state": getState(this.rootContext.state.value),
196
+ "data-disabled": this.rootContext.disabled.value ? "" : void 0,
197
+ "asChild": this.$props.asChild,
198
+ "as": this.$props.as,
199
+ "style": {
200
+ pointerEvents: "none"
201
+ }
202
+ }), () => this.$slots.default?.()));
203
+ }
204
+ });
205
+
206
+ export { CheckboxIndicator, CheckboxRoot, checkboxIndicatorProps, checkboxRootEmits, checkboxRootProps };
package/dist/index.cjs CHANGED
@@ -1,173 +1,16 @@
1
1
  'use strict';
2
2
 
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');
3
+ const index = require('./component.cjs');
4
+ require('vue');
5
+ require('@destyler/primitive');
6
+ require('@destyler/composition');
7
+ require('@destyler/shared');
8
+ require('@destyler/directives');
9
+ require('@destyler/presence');
9
10
 
10
- function isIndeterminate(checked) {
11
- return checked === "indeterminate";
12
- }
13
- function getState(checked) {
14
- return isIndeterminate(checked) ? "indeterminate" : checked ? "checked" : "unchecked";
15
- }
16
11
 
17
- const destylerCheckboxRootProps = {
18
- asChild: {
19
- type: Boolean,
20
- required: false,
21
- default: false
22
- },
23
- as: {
24
- type: [String, Object],
25
- required: false,
26
- default: "button"
27
- },
28
- defaultChecked: {
29
- type: Boolean,
30
- required: false
31
- },
32
- checked: {
33
- type: [Boolean, String],
34
- required: false,
35
- default: void 0
36
- },
37
- disabled: {
38
- type: Boolean,
39
- required: false,
40
- default: false
41
- },
42
- required: {
43
- type: Boolean,
44
- required: false
45
- },
46
- name: {
47
- type: String,
48
- required: false
49
- },
50
- value: {
51
- type: String,
52
- required: false,
53
- default: "on"
54
- },
55
- id: {
56
- type: String,
57
- required: false
58
- }
59
- };
60
- const [injectCheckboxRootContext, provideCheckboxRootContext] = shared.createContext("DestylerCheckboxRoot");
61
- const DestylerCheckboxRoot = vue.defineComponent({
62
- name: "DestylerCheckboxRoot",
63
- props: destylerCheckboxRootProps,
64
- emits: ["update:checked"],
65
- setup(props, { emit }) {
66
- const disabledRef = vue.toRef(props.disabled);
67
- const checkedRef = composition.useVModel(props, "checked", emit, {
68
- defaultValue: props.defaultChecked,
69
- passive: props.checked === void 0
70
- });
71
- const { forwardRef, currentElement } = composition.useForwardExpose();
72
- const isFormControl = composition.useFormControl(currentElement);
73
- const ariaLabel = vue.computed(() => props.id && currentElement.value ? document.querySelector(`[for="${props.id}"]`)?.textContent : void 0);
74
- provideCheckboxRootContext({
75
- disabled: disabledRef,
76
- state: checkedRef
77
- });
78
- return {
79
- disabled: disabledRef,
80
- checked: checkedRef,
81
- forwardRef,
82
- isFormControl,
83
- ariaLabel
84
- };
85
- },
86
- render() {
87
- const useVShow = this.isFormControl;
88
- return [
89
- vue.withDirectives(vue.h(primitive.DestylerPrimitive, vue.mergeProps(this.$attrs, {
90
- "ref": (el) => this.forwardRef(el),
91
- "role": "checkbox",
92
- "asChild": this.$props.asChild,
93
- "as": this.$props.as,
94
- "type": this.$props.as === "button" ? "button" : void 0,
95
- "aria-checked": isIndeterminate(this.checked) ? "mixed" : this.checked,
96
- "aria-required": false,
97
- "aria-label": this.$attrs["aria-label"] || this.ariaLabel,
98
- "data-state": getState(this.checked),
99
- "data-disabled": this.disabled ? "" : void 0,
100
- "disabled": this.disabled,
101
- "onClick": () => {
102
- this.checked = !this.checked;
103
- }
104
- }), () => this.$slots.default?.()), [
105
- [directives.BindOnceDirective, { id: this.$props.id }]
106
- ]),
107
- useVShow ? vue.h("input", {
108
- "type": "checkbox",
109
- "tabindex": "-1",
110
- "aria-hidden": "",
111
- "value": this.$props.value,
112
- "checked": this.checked,
113
- "name": this.$props.name,
114
- "disabled": this.disabled,
115
- "required": this.$props.required,
116
- "style": {
117
- transform: "translateX(-100%)",
118
- position: "absolute",
119
- pointerEvents: "none",
120
- opacity: 0,
121
- margin: 0
122
- }
123
- }) : null
124
- ];
125
- }
126
- });
127
12
 
128
- const destylerCheckboxIndicatorProps = {
129
- asChild: {
130
- type: Boolean,
131
- required: false,
132
- default: false
133
- },
134
- as: {
135
- type: [String, Object],
136
- required: false,
137
- default: "span"
138
- },
139
- forceMount: {
140
- type: Boolean,
141
- required: false,
142
- default: false
143
- }
144
- };
145
- const DestylerCheckboxIndicator = vue.defineComponent({
146
- name: "DestylerCheckboxIndicator",
147
- props: destylerCheckboxIndicatorProps,
148
- setup() {
149
- const rootContext = injectCheckboxRootContext();
150
- const { forwardRef } = composition.useForwardExpose();
151
- return {
152
- rootContext,
153
- forwardRef
154
- };
155
- },
156
- render() {
157
- return vue.h(presence.DestylerPresence, {
158
- present: this.$props.forceMount || isIndeterminate(this.rootContext.state.value) || this.rootContext.state.value === true
159
- }, () => vue.h(primitive.DestylerPrimitive, vue.mergeProps(this.$attrs, {
160
- "ref": (el) => this.forwardRef(el),
161
- "data-state": getState(this.rootContext.state.value),
162
- "data-disabled": this.rootContext.disabled.value ? "" : void 0,
163
- "asChild": this.$props.asChild,
164
- "as": this.$props.as,
165
- "style": {
166
- pointerEvents: "none"
167
- }
168
- }), () => vue.renderSlot(this.$slots, "default")));
169
- }
170
- });
171
-
172
- exports.DestylerCheckboxIndicator = DestylerCheckboxIndicator;
173
- exports.DestylerCheckboxRoot = DestylerCheckboxRoot;
13
+ exports.CheckboxIndicator = index.CheckboxIndicator;
14
+ exports.CheckboxRoot = index.CheckboxRoot;
15
+ exports.checkboxIndicatorProps = index.checkboxIndicatorProps;
16
+ exports.checkboxRootProps = index.checkboxRootProps;
package/dist/index.d.cts CHANGED
@@ -1,215 +1,4 @@
1
- import * as vue from 'vue';
2
- import { PropType, Component, Ref } from 'vue';
3
- import { AsTag } from '@destyler/primitive';
4
- import { ExtractPublicPropTypes } from '@destyler/shared';
5
-
6
- type CheckedState = boolean | 'indeterminate';
7
-
8
- declare const destylerCheckboxRootProps: {
9
- readonly asChild: {
10
- readonly type: PropType<boolean>;
11
- readonly required: false;
12
- readonly default: false;
13
- };
14
- readonly as: {
15
- readonly type: PropType<AsTag | Component>;
16
- readonly required: false;
17
- readonly default: "button";
18
- };
19
- readonly defaultChecked: {
20
- readonly type: PropType<boolean>;
21
- readonly required: false;
22
- };
23
- readonly checked: {
24
- readonly type: PropType<boolean | "indeterminate">;
25
- readonly required: false;
26
- readonly default: undefined;
27
- };
28
- readonly disabled: {
29
- readonly type: PropType<boolean>;
30
- readonly required: false;
31
- readonly default: false;
32
- };
33
- readonly required: {
34
- readonly type: PropType<boolean>;
35
- readonly required: false;
36
- };
37
- readonly name: {
38
- readonly type: PropType<string>;
39
- readonly required: false;
40
- };
41
- readonly value: {
42
- readonly type: PropType<string>;
43
- readonly required: false;
44
- readonly default: "on";
45
- };
46
- readonly id: {
47
- readonly type: PropType<string>;
48
- readonly required: false;
49
- };
50
- };
51
- type DestylerCheckboxRootProps = ExtractPublicPropTypes<typeof destylerCheckboxRootProps>;
52
- interface CheckboxRootContext {
53
- disabled: Ref<boolean>;
54
- state: Ref<CheckedState>;
55
- }
56
- declare const DestylerCheckboxRoot: vue.DefineComponent<{
57
- readonly asChild: {
58
- readonly type: PropType<boolean>;
59
- readonly required: false;
60
- readonly default: false;
61
- };
62
- readonly as: {
63
- readonly type: PropType<AsTag | Component>;
64
- readonly required: false;
65
- readonly default: "button";
66
- };
67
- readonly defaultChecked: {
68
- readonly type: PropType<boolean>;
69
- readonly required: false;
70
- };
71
- readonly checked: {
72
- readonly type: PropType<boolean | "indeterminate">;
73
- readonly required: false;
74
- readonly default: undefined;
75
- };
76
- readonly disabled: {
77
- readonly type: PropType<boolean>;
78
- readonly required: false;
79
- readonly default: false;
80
- };
81
- readonly required: {
82
- readonly type: PropType<boolean>;
83
- readonly required: false;
84
- };
85
- readonly name: {
86
- readonly type: PropType<string>;
87
- readonly required: false;
88
- };
89
- readonly value: {
90
- readonly type: PropType<string>;
91
- readonly required: false;
92
- readonly default: "on";
93
- };
94
- readonly id: {
95
- readonly type: PropType<string>;
96
- readonly required: false;
97
- };
98
- }, {
99
- disabled: Ref<false> | Ref<true>;
100
- checked: Ref<CheckedState>;
101
- forwardRef: (ref: Element | vue.ComponentPublicInstance | null) => void;
102
- isFormControl: vue.ComputedRef<boolean>;
103
- ariaLabel: vue.ComputedRef<string | null | undefined>;
104
- }, unknown, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, "update:checked"[], "update:checked", vue.PublicProps, Readonly<vue.ExtractPropTypes<{
105
- readonly asChild: {
106
- readonly type: PropType<boolean>;
107
- readonly required: false;
108
- readonly default: false;
109
- };
110
- readonly as: {
111
- readonly type: PropType<AsTag | Component>;
112
- readonly required: false;
113
- readonly default: "button";
114
- };
115
- readonly defaultChecked: {
116
- readonly type: PropType<boolean>;
117
- readonly required: false;
118
- };
119
- readonly checked: {
120
- readonly type: PropType<boolean | "indeterminate">;
121
- readonly required: false;
122
- readonly default: undefined;
123
- };
124
- readonly disabled: {
125
- readonly type: PropType<boolean>;
126
- readonly required: false;
127
- readonly default: false;
128
- };
129
- readonly required: {
130
- readonly type: PropType<boolean>;
131
- readonly required: false;
132
- };
133
- readonly name: {
134
- readonly type: PropType<string>;
135
- readonly required: false;
136
- };
137
- readonly value: {
138
- readonly type: PropType<string>;
139
- readonly required: false;
140
- readonly default: "on";
141
- };
142
- readonly id: {
143
- readonly type: PropType<string>;
144
- readonly required: false;
145
- };
146
- }>> & {
147
- "onUpdate:checked"?: ((...args: any[]) => any) | undefined;
148
- }, {
149
- readonly checked: boolean | "indeterminate";
150
- readonly asChild: boolean;
151
- readonly as: AsTag | Component;
152
- readonly disabled: boolean;
153
- readonly value: string;
154
- }, {}>;
155
-
156
- declare const destylerCheckboxIndicatorProps: {
157
- readonly asChild: {
158
- readonly type: PropType<boolean>;
159
- readonly required: false;
160
- readonly default: false;
161
- };
162
- readonly as: {
163
- readonly type: PropType<AsTag | Component>;
164
- readonly required: false;
165
- readonly default: "span";
166
- };
167
- readonly forceMount: {
168
- readonly type: PropType<boolean>;
169
- readonly required: false;
170
- readonly default: false;
171
- };
172
- };
173
- type DestylerCheckboxIndicatorProps = ExtractPublicPropTypes<typeof destylerCheckboxIndicatorProps>;
174
- declare const DestylerCheckboxIndicator: vue.DefineComponent<{
175
- readonly asChild: {
176
- readonly type: PropType<boolean>;
177
- readonly required: false;
178
- readonly default: false;
179
- };
180
- readonly as: {
181
- readonly type: PropType<AsTag | Component>;
182
- readonly required: false;
183
- readonly default: "span";
184
- };
185
- readonly forceMount: {
186
- readonly type: PropType<boolean>;
187
- readonly required: false;
188
- readonly default: false;
189
- };
190
- }, {
191
- rootContext: CheckboxRootContext;
192
- forwardRef: (ref: Element | vue.ComponentPublicInstance | null) => void;
193
- }, unknown, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {}, string, vue.PublicProps, Readonly<vue.ExtractPropTypes<{
194
- readonly asChild: {
195
- readonly type: PropType<boolean>;
196
- readonly required: false;
197
- readonly default: false;
198
- };
199
- readonly as: {
200
- readonly type: PropType<AsTag | Component>;
201
- readonly required: false;
202
- readonly default: "span";
203
- };
204
- readonly forceMount: {
205
- readonly type: PropType<boolean>;
206
- readonly required: false;
207
- readonly default: false;
208
- };
209
- }>>, {
210
- readonly asChild: boolean;
211
- readonly as: AsTag | Component;
212
- readonly forceMount: boolean;
213
- }, {}>;
214
-
215
- export { DestylerCheckboxIndicator, type DestylerCheckboxIndicatorProps, DestylerCheckboxRoot, type DestylerCheckboxRootProps };
1
+ export { CheckboxIndicator, CheckboxIndicatorProps, CheckboxRoot, CheckboxRootProps, checkboxIndicatorProps, checkboxRootProps } from './component.cjs';
2
+ import 'vue';
3
+ import '@destyler/primitive';
4
+ import '@destyler/shared';