hoci 0.0.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Chizuki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,371 @@
1
+ 'use strict';
2
+
3
+ const core = require('@vueuse/core');
4
+ const vue = require('vue');
5
+
6
+ function defineHookProps(props) {
7
+ return props;
8
+ }
9
+ function defineHookEmits(emits) {
10
+ return emits;
11
+ }
12
+ function defineHookComponent(options) {
13
+ return (props, context) => {
14
+ const p = props instanceof Function ? core.reactiveComputed(() => props()) : props;
15
+ return options.setup(p, context);
16
+ };
17
+ }
18
+
19
+ const valuePropType = [String, Number, Object, Boolean, null];
20
+ const classPropType = [String, Array];
21
+ const labelPropType = [String, Function];
22
+
23
+ const InitSymbol = Symbol("[hi-selection]init");
24
+ const ActiveClassSymbol = Symbol("[hi-selection]active-class");
25
+ const ItemClassSymbol = Symbol("[hi-selection]item-class");
26
+ const UnactiveSymbol = Symbol("[hi-selection]unactive-class");
27
+ const ItemLabelSymbol = Symbol("[hi-selection]label");
28
+ const ActivateEventSymbol = Symbol("[hi-selection]activate-event");
29
+ const ChangeActiveSymbol = Symbol("[hi-selection]change-active");
30
+ const IsActiveSymbol = Symbol("[hi-selection]is-active");
31
+
32
+ const selectionItemProps = defineHookProps({
33
+ value: {
34
+ type: valuePropType,
35
+ default() {
36
+ return Math.random().toString(16).slice(2);
37
+ }
38
+ },
39
+ label: {
40
+ type: [Function, String]
41
+ },
42
+ keepAlive: {
43
+ type: Boolean,
44
+ default: () => true
45
+ },
46
+ key: {
47
+ type: [String, Number, Symbol]
48
+ },
49
+ activateEvent: {
50
+ type: String
51
+ }
52
+ });
53
+ const useSelectionItem = defineHookComponent({
54
+ props: selectionItemProps,
55
+ setup(props, { slots }) {
56
+ const parentLabel = core.resolveRef(vue.inject(ItemLabelSymbol));
57
+ function render() {
58
+ return vue.renderSlot(slots, "default", {}, () => {
59
+ let label = props.label ?? parentLabel.value;
60
+ if (label) {
61
+ if (typeof label == "function") {
62
+ label = label(props.value);
63
+ }
64
+ }
65
+ return Array.isArray(label) ? label : [label];
66
+ });
67
+ }
68
+ let remove = () => {
69
+ };
70
+ const init = vue.inject(InitSymbol);
71
+ if (init) {
72
+ vue.watch(
73
+ () => props.value,
74
+ (value) => {
75
+ remove();
76
+ remove = init({
77
+ id: Math.random().toString(16).slice(2),
78
+ label: typeof props.label == "string" ? props.label : void 0,
79
+ value,
80
+ render
81
+ });
82
+ },
83
+ { immediate: true }
84
+ );
85
+ vue.onDeactivated(() => remove());
86
+ }
87
+ const isActive = vue.inject(IsActiveSymbol, () => false);
88
+ const changeActive = vue.inject(ChangeActiveSymbol, () => false);
89
+ const activeClass = vue.computed(() => vue.inject(ActiveClassSymbol)?.value ?? "active");
90
+ const unactiveClass = vue.computed(() => vue.inject(UnactiveSymbol)?.value ?? "unactive");
91
+ const itemClass = vue.computed(() => {
92
+ return [vue.inject(ItemClassSymbol)?.value ?? ""].concat(isActive(props.value) ? activeClass.value : unactiveClass.value);
93
+ });
94
+ const activateEvent = core.resolveRef(() => {
95
+ const event = vue.inject(ActivateEventSymbol);
96
+ return props.activateEvent ?? event?.value ?? "click";
97
+ });
98
+ return {
99
+ activate() {
100
+ changeActive(props.value);
101
+ },
102
+ render,
103
+ isActive,
104
+ activeClass,
105
+ unactiveClass,
106
+ itemClass,
107
+ activateEvent
108
+ };
109
+ }
110
+ });
111
+ const HiItem = vue.defineComponent({
112
+ name: "HiItem",
113
+ props: selectionItemProps,
114
+ setup(props, context) {
115
+ const { render, activate, itemClass, activateEvent } = useSelectionItem(props, context);
116
+ return () => vue.h("div", {
117
+ class: itemClass.value,
118
+ [`on${vue.capitalize(activateEvent.value)}`]: activate
119
+ }, render());
120
+ }
121
+ });
122
+
123
+ const selectionListProps = defineHookProps({
124
+ tag: {
125
+ type: String,
126
+ default: "div"
127
+ },
128
+ modelValue: {
129
+ type: valuePropType
130
+ },
131
+ activeClass: {
132
+ type: classPropType,
133
+ default: ""
134
+ },
135
+ itemClass: {
136
+ type: classPropType,
137
+ default: ""
138
+ },
139
+ unactiveClass: {
140
+ type: classPropType,
141
+ default: ""
142
+ },
143
+ label: {
144
+ type: labelPropType
145
+ },
146
+ multiple: {
147
+ type: [Boolean, Number],
148
+ default: () => false
149
+ },
150
+ defaultValue: {
151
+ type: valuePropType
152
+ },
153
+ activateEvent: {
154
+ type: String,
155
+ default: () => "click"
156
+ }
157
+ });
158
+ const selectionListEmits = defineHookEmits(["update:modelValue", "change", "load", "unload"]);
159
+ const useSelectionList = defineHookComponent({
160
+ props: selectionListProps,
161
+ emits: selectionListEmits,
162
+ setup(props, { slots, emit }) {
163
+ const options = vue.reactive([]);
164
+ function toArray(value) {
165
+ if (value === void 0) {
166
+ return [];
167
+ }
168
+ if (props.multiple && Array.isArray(value)) {
169
+ return value.filter((v) => v != null || v !== void 0);
170
+ }
171
+ return [value];
172
+ }
173
+ const actives = vue.reactive([...toArray(props.modelValue ?? props.defaultValue)]);
174
+ const currentValue = vue.computed({
175
+ get() {
176
+ return props.multiple ? actives : actives[0];
177
+ },
178
+ set(val) {
179
+ if (props.multiple) {
180
+ actives.splice(0, actives.length, ...toArray(val));
181
+ } else {
182
+ actives.splice(0, actives.length, val);
183
+ }
184
+ }
185
+ });
186
+ const modelValue = vue.computed({
187
+ get() {
188
+ return props.modelValue ?? props.defaultValue;
189
+ },
190
+ set(val) {
191
+ emit("update:modelValue", val);
192
+ }
193
+ });
194
+ core.syncRef(currentValue, modelValue, { immediate: true, deep: true });
195
+ vue.watch(currentValue, (val) => {
196
+ emit("change", val);
197
+ }, { deep: true });
198
+ vue.provide(
199
+ ActiveClassSymbol,
200
+ vue.computed(() => props.activeClass)
201
+ );
202
+ vue.provide(
203
+ UnactiveSymbol,
204
+ vue.computed(() => props.unactiveClass)
205
+ );
206
+ vue.provide(
207
+ ItemClassSymbol,
208
+ vue.computed(() => props.itemClass)
209
+ );
210
+ vue.provide(ItemLabelSymbol, vue.computed(() => props.label));
211
+ vue.provide(ActivateEventSymbol, vue.computed(() => props.activateEvent));
212
+ function isActive(value) {
213
+ return actives.includes(value);
214
+ }
215
+ function changeActive(option) {
216
+ if (isActive(option)) {
217
+ if (props.multiple) {
218
+ actives.splice(actives.indexOf(option), 1);
219
+ }
220
+ } else {
221
+ if (props.multiple) {
222
+ const limit = typeof props.multiple === "number" ? props.multiple : Infinity;
223
+ if (actives.length < limit) {
224
+ actives.push(option);
225
+ }
226
+ } else {
227
+ actives.splice(0, actives.length, option);
228
+ }
229
+ }
230
+ }
231
+ vue.provide(IsActiveSymbol, isActive);
232
+ vue.provide(ChangeActiveSymbol, changeActive);
233
+ vue.provide(InitSymbol, (option) => {
234
+ function remove() {
235
+ const index = options.findIndex((e) => e.id === option.id);
236
+ if (index > -1) {
237
+ options.splice(index, 1);
238
+ emit("unload", option);
239
+ }
240
+ }
241
+ for (let i = 0; i < options.length; i++) {
242
+ if (options[i].value === option.value) {
243
+ options.splice(i, 1);
244
+ i--;
245
+ }
246
+ }
247
+ options.push(option);
248
+ emit("load", option);
249
+ return remove;
250
+ });
251
+ function renderItem() {
252
+ const children = options.filter((e) => actives.includes(e.value)).map((e) => e.render());
253
+ return props.multiple ? children : children[0];
254
+ }
255
+ function render() {
256
+ return vue.h(props.tag, {}, vue.renderSlot(slots, "default", {}));
257
+ }
258
+ return {
259
+ options,
260
+ actives,
261
+ isActive,
262
+ changeActive,
263
+ renderItem,
264
+ render
265
+ };
266
+ }
267
+ });
268
+ const HiSelection = vue.defineComponent({
269
+ name: "HiSelection",
270
+ props: selectionListProps,
271
+ emits: selectionListEmits,
272
+ setup(props, context) {
273
+ const { render } = useSelectionList(props, context);
274
+ return () => render();
275
+ }
276
+ });
277
+
278
+ const switchProps = defineHookProps({
279
+ modelValue: {
280
+ type: Boolean,
281
+ default: false
282
+ },
283
+ class: {
284
+ type: classPropType,
285
+ default: ""
286
+ },
287
+ activeClass: {
288
+ type: classPropType,
289
+ default: "checked"
290
+ },
291
+ unactiveClass: {
292
+ type: classPropType,
293
+ default: "unchecked"
294
+ },
295
+ activateEvent: {
296
+ type: String,
297
+ default: "click"
298
+ },
299
+ tag: {
300
+ type: String,
301
+ default: "div"
302
+ }
303
+ });
304
+ const switchEmits = defineHookEmits(["update:modelValue", "change"]);
305
+ const useSwitch = defineHookComponent({
306
+ props: switchProps,
307
+ emits: switchEmits,
308
+ setup(props, { emit }) {
309
+ const checked = vue.ref(!!props.modelValue);
310
+ vue.watch(
311
+ () => props.modelValue,
312
+ (val) => {
313
+ checked.value = !!val;
314
+ }
315
+ );
316
+ const modelValue = vue.computed({
317
+ get() {
318
+ return !!(props.modelValue ?? checked.value);
319
+ },
320
+ set(val) {
321
+ checked.value = val;
322
+ emit("update:modelValue", val);
323
+ emit("change", val);
324
+ }
325
+ });
326
+ const toggle = function(value) {
327
+ if (typeof value === "boolean") {
328
+ modelValue.value = value;
329
+ } else {
330
+ modelValue.value = !modelValue.value;
331
+ }
332
+ };
333
+ const switchClass = vue.computed(() => {
334
+ return [props.class, modelValue.value ? props.activeClass : props.unactiveClass];
335
+ });
336
+ return {
337
+ toggle,
338
+ modelValue,
339
+ switchClass
340
+ };
341
+ }
342
+ });
343
+ const HiSwitch = vue.defineComponent({
344
+ name: "ISwitch",
345
+ props: switchProps,
346
+ emits: switchEmits,
347
+ setup(props, context) {
348
+ const { slots } = context;
349
+ const { switchClass, toggle } = useSwitch(props, context);
350
+ return () => {
351
+ return vue.h(props.tag, {
352
+ class: switchClass.value,
353
+ [`on${vue.capitalize(props.activateEvent)}`]: toggle
354
+ }, vue.renderSlot(slots, "default"));
355
+ };
356
+ }
357
+ });
358
+
359
+ exports.HiItem = HiItem;
360
+ exports.HiSelection = HiSelection;
361
+ exports.HiSwitch = HiSwitch;
362
+ exports.defineHookComponent = defineHookComponent;
363
+ exports.defineHookEmits = defineHookEmits;
364
+ exports.defineHookProps = defineHookProps;
365
+ exports.selectionItemProps = selectionItemProps;
366
+ exports.selectionListEmits = selectionListEmits;
367
+ exports.selectionListProps = selectionListProps;
368
+ exports.switchProps = switchProps;
369
+ exports.useSelectionItem = useSelectionItem;
370
+ exports.useSelectionList = useSelectionList;
371
+ exports.useSwitch = useSwitch;
@@ -0,0 +1,491 @@
1
+ import * as vue from 'vue';
2
+ import { EmitsOptions, ComponentPropsOptions, ExtractPropTypes, SetupContext, PropType } from 'vue';
3
+ import { MaybeFunction } from 'maybe-types';
4
+
5
+ interface HookComponentOptions<R, E = EmitsOptions, EE extends string = string, P = ComponentPropsOptions, D extends Record<string, unknown> = ExtractPropTypes<P>> {
6
+ props?: P;
7
+ emits?: E | EE[];
8
+ setup: (props: D, context: SetupContext<E>) => R;
9
+ }
10
+ type HookComponent<R, E = EmitsOptions, P = ComponentPropsOptions, D extends Record<string, unknown> = ExtractPropTypes<P>> = (props: MaybeFunction<D>, context: SetupContext<E>) => R;
11
+ declare function defineHookProps<P = ComponentPropsOptions>(props: P): P;
12
+ declare function defineHookEmits<E, EE extends string = string>(emits: E | EE[]): E | EE[];
13
+ declare function defineHookComponent<R, E = EmitsOptions, EE extends string = string, P = ComponentPropsOptions, D extends Record<string, unknown> = ExtractPropTypes<P>>(options: HookComponentOptions<R, E, EE, P, D>): HookComponent<R, E, P, D>;
14
+
15
+ type ClassType = string | string[];
16
+ type ActivateEvent = "click" | "mouseenter" | "mousedown" | "mouseup" | "dblclick" | "contextmenu" | "touchstart" | "touchend";
17
+ type ElementLike = JSX.Element | string | ElementLike[];
18
+
19
+ declare const selectionItemProps: {
20
+ value: {
21
+ type: PropType<any>;
22
+ default(): string;
23
+ };
24
+ label: {
25
+ type: PropType<ElementLike | ((val: any) => string) | null>;
26
+ };
27
+ keepAlive: {
28
+ type: BooleanConstructor;
29
+ default: () => true;
30
+ };
31
+ key: {
32
+ type: PropType<string | number | symbol>;
33
+ };
34
+ activateEvent: {
35
+ type: PropType<"click" | "mouseenter" | "mousedown" | "mouseup" | "dblclick" | "contextmenu">;
36
+ };
37
+ };
38
+ declare const useSelectionItem: HookComponent<{
39
+ activate(): void;
40
+ render: () => vue.VNode<vue.RendererNode, vue.RendererElement, {
41
+ [key: string]: any;
42
+ }>;
43
+ isActive: (_: any) => boolean;
44
+ activeClass: vue.ComputedRef<ClassType>;
45
+ unactiveClass: vue.ComputedRef<ClassType>;
46
+ itemClass: vue.ComputedRef<ClassType[]>;
47
+ activateEvent: vue.ComputedRef<ActivateEvent>;
48
+ }, vue.EmitsOptions, {
49
+ value: {
50
+ type: PropType<any>;
51
+ default(): string;
52
+ };
53
+ label: {
54
+ type: PropType<ElementLike | ((val: any) => string) | null>;
55
+ };
56
+ keepAlive: {
57
+ type: BooleanConstructor;
58
+ default: () => true;
59
+ };
60
+ key: {
61
+ type: PropType<string | number | symbol>;
62
+ };
63
+ activateEvent: {
64
+ type: PropType<"click" | "mouseenter" | "mousedown" | "mouseup" | "dblclick" | "contextmenu">;
65
+ };
66
+ }, vue.ExtractPropTypes<{
67
+ value: {
68
+ type: PropType<any>;
69
+ default(): string;
70
+ };
71
+ label: {
72
+ type: PropType<ElementLike | ((val: any) => string) | null>;
73
+ };
74
+ keepAlive: {
75
+ type: BooleanConstructor;
76
+ default: () => true;
77
+ };
78
+ key: {
79
+ type: PropType<string | number | symbol>;
80
+ };
81
+ activateEvent: {
82
+ type: PropType<"click" | "mouseenter" | "mousedown" | "mouseup" | "dblclick" | "contextmenu">;
83
+ };
84
+ }>>;
85
+ declare const HiItem: vue.DefineComponent<{
86
+ value: {
87
+ type: PropType<any>;
88
+ default(): string;
89
+ };
90
+ label: {
91
+ type: PropType<ElementLike | ((val: any) => string) | null>;
92
+ };
93
+ keepAlive: {
94
+ type: BooleanConstructor;
95
+ default: () => true;
96
+ };
97
+ key: {
98
+ type: PropType<string | number | symbol>;
99
+ };
100
+ activateEvent: {
101
+ type: PropType<"click" | "mouseenter" | "mousedown" | "mouseup" | "dblclick" | "contextmenu">;
102
+ };
103
+ }, () => vue.VNode<vue.RendererNode, vue.RendererElement, {
104
+ [key: string]: any;
105
+ }>, unknown, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {}, string, vue.VNodeProps & vue.AllowedComponentProps & vue.ComponentCustomProps, Readonly<vue.ExtractPropTypes<{
106
+ value: {
107
+ type: PropType<any>;
108
+ default(): string;
109
+ };
110
+ label: {
111
+ type: PropType<ElementLike | ((val: any) => string) | null>;
112
+ };
113
+ keepAlive: {
114
+ type: BooleanConstructor;
115
+ default: () => true;
116
+ };
117
+ key: {
118
+ type: PropType<string | number | symbol>;
119
+ };
120
+ activateEvent: {
121
+ type: PropType<"click" | "mouseenter" | "mousedown" | "mouseup" | "dblclick" | "contextmenu">;
122
+ };
123
+ }>>, {
124
+ keepAlive: boolean;
125
+ value: any;
126
+ }>;
127
+
128
+ declare const selectionListProps: {
129
+ tag: {
130
+ type: StringConstructor;
131
+ default: string;
132
+ };
133
+ modelValue: {
134
+ type: PropType<any>;
135
+ };
136
+ activeClass: {
137
+ type: PropType<string | string[]>;
138
+ default: string;
139
+ };
140
+ itemClass: {
141
+ type: PropType<string | string[]>;
142
+ default: string;
143
+ };
144
+ unactiveClass: {
145
+ type: PropType<string | string[]>;
146
+ default: string;
147
+ };
148
+ label: {
149
+ type: PropType<string | ((val?: any) => string) | null>;
150
+ };
151
+ /**
152
+ * 多选模式
153
+ */
154
+ multiple: {
155
+ type: (BooleanConstructor | NumberConstructor)[];
156
+ default: () => false;
157
+ };
158
+ defaultValue: {
159
+ type: PropType<any>;
160
+ };
161
+ activateEvent: {
162
+ type: PropType<ActivateEvent>;
163
+ default: () => "click";
164
+ };
165
+ };
166
+ declare const selectionListEmits: ("update:modelValue" | "change" | "load" | "unload")[];
167
+ declare const useSelectionList: HookComponent<{
168
+ options: {
169
+ id?: string | undefined;
170
+ label?: string | undefined;
171
+ value: any;
172
+ render: () => ElementLike;
173
+ }[];
174
+ actives: any[];
175
+ isActive: (value: any) => boolean;
176
+ changeActive: (option: any) => void;
177
+ renderItem: () => ElementLike;
178
+ render: () => vue.VNode<vue.RendererNode, vue.RendererElement, {
179
+ [key: string]: any;
180
+ }>;
181
+ }, ("update:modelValue" | "change" | "load" | "unload")[], {
182
+ tag: {
183
+ type: StringConstructor;
184
+ default: string;
185
+ };
186
+ modelValue: {
187
+ type: PropType<any>;
188
+ };
189
+ activeClass: {
190
+ type: PropType<string | string[]>;
191
+ default: string;
192
+ };
193
+ itemClass: {
194
+ type: PropType<string | string[]>;
195
+ default: string;
196
+ };
197
+ unactiveClass: {
198
+ type: PropType<string | string[]>;
199
+ default: string;
200
+ };
201
+ label: {
202
+ type: PropType<string | ((val?: any) => string) | null>;
203
+ };
204
+ /**
205
+ * 多选模式
206
+ */
207
+ multiple: {
208
+ type: (BooleanConstructor | NumberConstructor)[];
209
+ default: () => false;
210
+ };
211
+ defaultValue: {
212
+ type: PropType<any>;
213
+ };
214
+ activateEvent: {
215
+ type: PropType<ActivateEvent>;
216
+ default: () => "click";
217
+ };
218
+ }, vue.ExtractPropTypes<{
219
+ tag: {
220
+ type: StringConstructor;
221
+ default: string;
222
+ };
223
+ modelValue: {
224
+ type: PropType<any>;
225
+ };
226
+ activeClass: {
227
+ type: PropType<string | string[]>;
228
+ default: string;
229
+ };
230
+ itemClass: {
231
+ type: PropType<string | string[]>;
232
+ default: string;
233
+ };
234
+ unactiveClass: {
235
+ type: PropType<string | string[]>;
236
+ default: string;
237
+ };
238
+ label: {
239
+ type: PropType<string | ((val?: any) => string) | null>;
240
+ };
241
+ /**
242
+ * 多选模式
243
+ */
244
+ multiple: {
245
+ type: (BooleanConstructor | NumberConstructor)[];
246
+ default: () => false;
247
+ };
248
+ defaultValue: {
249
+ type: PropType<any>;
250
+ };
251
+ activateEvent: {
252
+ type: PropType<ActivateEvent>;
253
+ default: () => "click";
254
+ };
255
+ }>>;
256
+ declare const HiSelection: vue.DefineComponent<{
257
+ tag: {
258
+ type: StringConstructor;
259
+ default: string;
260
+ };
261
+ modelValue: {
262
+ type: PropType<any>;
263
+ };
264
+ activeClass: {
265
+ type: PropType<string | string[]>;
266
+ default: string;
267
+ };
268
+ itemClass: {
269
+ type: PropType<string | string[]>;
270
+ default: string;
271
+ };
272
+ unactiveClass: {
273
+ type: PropType<string | string[]>;
274
+ default: string;
275
+ };
276
+ label: {
277
+ type: PropType<string | ((val?: any) => string) | null>;
278
+ };
279
+ /**
280
+ * 多选模式
281
+ */
282
+ multiple: {
283
+ type: (BooleanConstructor | NumberConstructor)[];
284
+ default: () => false;
285
+ };
286
+ defaultValue: {
287
+ type: PropType<any>;
288
+ };
289
+ activateEvent: {
290
+ type: PropType<ActivateEvent>;
291
+ default: () => "click";
292
+ };
293
+ }, () => vue.VNode<vue.RendererNode, vue.RendererElement, {
294
+ [key: string]: any;
295
+ }>, unknown, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, ("update:modelValue" | "change" | "load" | "unload")[], "update:modelValue" | "change" | "load" | "unload", vue.VNodeProps & vue.AllowedComponentProps & vue.ComponentCustomProps, Readonly<vue.ExtractPropTypes<{
296
+ tag: {
297
+ type: StringConstructor;
298
+ default: string;
299
+ };
300
+ modelValue: {
301
+ type: PropType<any>;
302
+ };
303
+ activeClass: {
304
+ type: PropType<string | string[]>;
305
+ default: string;
306
+ };
307
+ itemClass: {
308
+ type: PropType<string | string[]>;
309
+ default: string;
310
+ };
311
+ unactiveClass: {
312
+ type: PropType<string | string[]>;
313
+ default: string;
314
+ };
315
+ label: {
316
+ type: PropType<string | ((val?: any) => string) | null>;
317
+ };
318
+ /**
319
+ * 多选模式
320
+ */
321
+ multiple: {
322
+ type: (BooleanConstructor | NumberConstructor)[];
323
+ default: () => false;
324
+ };
325
+ defaultValue: {
326
+ type: PropType<any>;
327
+ };
328
+ activateEvent: {
329
+ type: PropType<ActivateEvent>;
330
+ default: () => "click";
331
+ };
332
+ }>> & {
333
+ "onUpdate:modelValue"?: ((...args: any[]) => any) | undefined;
334
+ onChange?: ((...args: any[]) => any) | undefined;
335
+ onLoad?: ((...args: any[]) => any) | undefined;
336
+ onUnload?: ((...args: any[]) => any) | undefined;
337
+ }, {
338
+ activateEvent: ActivateEvent;
339
+ itemClass: string | string[];
340
+ multiple: number | boolean;
341
+ tag: string;
342
+ activeClass: string | string[];
343
+ unactiveClass: string | string[];
344
+ }>;
345
+
346
+ declare const switchProps: {
347
+ modelValue: {
348
+ type: BooleanConstructor;
349
+ default: boolean;
350
+ };
351
+ class: {
352
+ type: PropType<string | string[]>;
353
+ default: string;
354
+ };
355
+ activeClass: {
356
+ type: PropType<string | string[]>;
357
+ default: string;
358
+ };
359
+ unactiveClass: {
360
+ type: PropType<string | string[]>;
361
+ default: string;
362
+ };
363
+ activateEvent: {
364
+ type: PropType<ActivateEvent>;
365
+ default: string;
366
+ };
367
+ tag: {
368
+ type: StringConstructor;
369
+ default: string;
370
+ };
371
+ };
372
+ declare const useSwitch: HookComponent<{
373
+ toggle: (value?: any) => void;
374
+ modelValue: vue.WritableComputedRef<boolean>;
375
+ switchClass: vue.ComputedRef<(string | string[])[]>;
376
+ }, ("update:modelValue" | "change")[], {
377
+ modelValue: {
378
+ type: BooleanConstructor;
379
+ default: boolean;
380
+ };
381
+ class: {
382
+ type: PropType<string | string[]>;
383
+ default: string;
384
+ };
385
+ activeClass: {
386
+ type: PropType<string | string[]>;
387
+ default: string;
388
+ };
389
+ unactiveClass: {
390
+ type: PropType<string | string[]>;
391
+ default: string;
392
+ };
393
+ activateEvent: {
394
+ type: PropType<ActivateEvent>;
395
+ default: string;
396
+ };
397
+ tag: {
398
+ type: StringConstructor;
399
+ default: string;
400
+ };
401
+ }, vue.ExtractPropTypes<{
402
+ modelValue: {
403
+ type: BooleanConstructor;
404
+ default: boolean;
405
+ };
406
+ class: {
407
+ type: PropType<string | string[]>;
408
+ default: string;
409
+ };
410
+ activeClass: {
411
+ type: PropType<string | string[]>;
412
+ default: string;
413
+ };
414
+ unactiveClass: {
415
+ type: PropType<string | string[]>;
416
+ default: string;
417
+ };
418
+ activateEvent: {
419
+ type: PropType<ActivateEvent>;
420
+ default: string;
421
+ };
422
+ tag: {
423
+ type: StringConstructor;
424
+ default: string;
425
+ };
426
+ }>>;
427
+ declare const HiSwitch: vue.DefineComponent<{
428
+ modelValue: {
429
+ type: BooleanConstructor;
430
+ default: boolean;
431
+ };
432
+ class: {
433
+ type: PropType<string | string[]>;
434
+ default: string;
435
+ };
436
+ activeClass: {
437
+ type: PropType<string | string[]>;
438
+ default: string;
439
+ };
440
+ unactiveClass: {
441
+ type: PropType<string | string[]>;
442
+ default: string;
443
+ };
444
+ activateEvent: {
445
+ type: PropType<ActivateEvent>;
446
+ default: string;
447
+ };
448
+ tag: {
449
+ type: StringConstructor;
450
+ default: string;
451
+ };
452
+ }, () => vue.VNode<vue.RendererNode, vue.RendererElement, {
453
+ [key: string]: any;
454
+ }>, unknown, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, ("update:modelValue" | "change")[], "update:modelValue" | "change", vue.VNodeProps & vue.AllowedComponentProps & vue.ComponentCustomProps, Readonly<vue.ExtractPropTypes<{
455
+ modelValue: {
456
+ type: BooleanConstructor;
457
+ default: boolean;
458
+ };
459
+ class: {
460
+ type: PropType<string | string[]>;
461
+ default: string;
462
+ };
463
+ activeClass: {
464
+ type: PropType<string | string[]>;
465
+ default: string;
466
+ };
467
+ unactiveClass: {
468
+ type: PropType<string | string[]>;
469
+ default: string;
470
+ };
471
+ activateEvent: {
472
+ type: PropType<ActivateEvent>;
473
+ default: string;
474
+ };
475
+ tag: {
476
+ type: StringConstructor;
477
+ default: string;
478
+ };
479
+ }>> & {
480
+ "onUpdate:modelValue"?: ((...args: any[]) => any) | undefined;
481
+ onChange?: ((...args: any[]) => any) | undefined;
482
+ }, {
483
+ activateEvent: ActivateEvent;
484
+ class: string | string[];
485
+ tag: string;
486
+ modelValue: boolean;
487
+ activeClass: string | string[];
488
+ unactiveClass: string | string[];
489
+ }>;
490
+
491
+ export { HiItem, HiSelection, HiSwitch, HookComponent, HookComponentOptions, defineHookComponent, defineHookEmits, defineHookProps, selectionItemProps, selectionListEmits, selectionListProps, switchProps, useSelectionItem, useSelectionList, useSwitch };
package/dist/index.mjs ADDED
@@ -0,0 +1,357 @@
1
+ import { reactiveComputed, resolveRef, syncRef } from '@vueuse/core';
2
+ import { defineComponent, h, capitalize, inject, watch, onDeactivated, computed, renderSlot, reactive, provide, ref } from 'vue';
3
+
4
+ function defineHookProps(props) {
5
+ return props;
6
+ }
7
+ function defineHookEmits(emits) {
8
+ return emits;
9
+ }
10
+ function defineHookComponent(options) {
11
+ return (props, context) => {
12
+ const p = props instanceof Function ? reactiveComputed(() => props()) : props;
13
+ return options.setup(p, context);
14
+ };
15
+ }
16
+
17
+ const valuePropType = [String, Number, Object, Boolean, null];
18
+ const classPropType = [String, Array];
19
+ const labelPropType = [String, Function];
20
+
21
+ const InitSymbol = Symbol("[hi-selection]init");
22
+ const ActiveClassSymbol = Symbol("[hi-selection]active-class");
23
+ const ItemClassSymbol = Symbol("[hi-selection]item-class");
24
+ const UnactiveSymbol = Symbol("[hi-selection]unactive-class");
25
+ const ItemLabelSymbol = Symbol("[hi-selection]label");
26
+ const ActivateEventSymbol = Symbol("[hi-selection]activate-event");
27
+ const ChangeActiveSymbol = Symbol("[hi-selection]change-active");
28
+ const IsActiveSymbol = Symbol("[hi-selection]is-active");
29
+
30
+ const selectionItemProps = defineHookProps({
31
+ value: {
32
+ type: valuePropType,
33
+ default() {
34
+ return Math.random().toString(16).slice(2);
35
+ }
36
+ },
37
+ label: {
38
+ type: [Function, String]
39
+ },
40
+ keepAlive: {
41
+ type: Boolean,
42
+ default: () => true
43
+ },
44
+ key: {
45
+ type: [String, Number, Symbol]
46
+ },
47
+ activateEvent: {
48
+ type: String
49
+ }
50
+ });
51
+ const useSelectionItem = defineHookComponent({
52
+ props: selectionItemProps,
53
+ setup(props, { slots }) {
54
+ const parentLabel = resolveRef(inject(ItemLabelSymbol));
55
+ function render() {
56
+ return renderSlot(slots, "default", {}, () => {
57
+ let label = props.label ?? parentLabel.value;
58
+ if (label) {
59
+ if (typeof label == "function") {
60
+ label = label(props.value);
61
+ }
62
+ }
63
+ return Array.isArray(label) ? label : [label];
64
+ });
65
+ }
66
+ let remove = () => {
67
+ };
68
+ const init = inject(InitSymbol);
69
+ if (init) {
70
+ watch(
71
+ () => props.value,
72
+ (value) => {
73
+ remove();
74
+ remove = init({
75
+ id: Math.random().toString(16).slice(2),
76
+ label: typeof props.label == "string" ? props.label : void 0,
77
+ value,
78
+ render
79
+ });
80
+ },
81
+ { immediate: true }
82
+ );
83
+ onDeactivated(() => remove());
84
+ }
85
+ const isActive = inject(IsActiveSymbol, () => false);
86
+ const changeActive = inject(ChangeActiveSymbol, () => false);
87
+ const activeClass = computed(() => inject(ActiveClassSymbol)?.value ?? "active");
88
+ const unactiveClass = computed(() => inject(UnactiveSymbol)?.value ?? "unactive");
89
+ const itemClass = computed(() => {
90
+ return [inject(ItemClassSymbol)?.value ?? ""].concat(isActive(props.value) ? activeClass.value : unactiveClass.value);
91
+ });
92
+ const activateEvent = resolveRef(() => {
93
+ const event = inject(ActivateEventSymbol);
94
+ return props.activateEvent ?? event?.value ?? "click";
95
+ });
96
+ return {
97
+ activate() {
98
+ changeActive(props.value);
99
+ },
100
+ render,
101
+ isActive,
102
+ activeClass,
103
+ unactiveClass,
104
+ itemClass,
105
+ activateEvent
106
+ };
107
+ }
108
+ });
109
+ const HiItem = defineComponent({
110
+ name: "HiItem",
111
+ props: selectionItemProps,
112
+ setup(props, context) {
113
+ const { render, activate, itemClass, activateEvent } = useSelectionItem(props, context);
114
+ return () => h("div", {
115
+ class: itemClass.value,
116
+ [`on${capitalize(activateEvent.value)}`]: activate
117
+ }, render());
118
+ }
119
+ });
120
+
121
+ const selectionListProps = defineHookProps({
122
+ tag: {
123
+ type: String,
124
+ default: "div"
125
+ },
126
+ modelValue: {
127
+ type: valuePropType
128
+ },
129
+ activeClass: {
130
+ type: classPropType,
131
+ default: ""
132
+ },
133
+ itemClass: {
134
+ type: classPropType,
135
+ default: ""
136
+ },
137
+ unactiveClass: {
138
+ type: classPropType,
139
+ default: ""
140
+ },
141
+ label: {
142
+ type: labelPropType
143
+ },
144
+ multiple: {
145
+ type: [Boolean, Number],
146
+ default: () => false
147
+ },
148
+ defaultValue: {
149
+ type: valuePropType
150
+ },
151
+ activateEvent: {
152
+ type: String,
153
+ default: () => "click"
154
+ }
155
+ });
156
+ const selectionListEmits = defineHookEmits(["update:modelValue", "change", "load", "unload"]);
157
+ const useSelectionList = defineHookComponent({
158
+ props: selectionListProps,
159
+ emits: selectionListEmits,
160
+ setup(props, { slots, emit }) {
161
+ const options = reactive([]);
162
+ function toArray(value) {
163
+ if (value === void 0) {
164
+ return [];
165
+ }
166
+ if (props.multiple && Array.isArray(value)) {
167
+ return value.filter((v) => v != null || v !== void 0);
168
+ }
169
+ return [value];
170
+ }
171
+ const actives = reactive([...toArray(props.modelValue ?? props.defaultValue)]);
172
+ const currentValue = computed({
173
+ get() {
174
+ return props.multiple ? actives : actives[0];
175
+ },
176
+ set(val) {
177
+ if (props.multiple) {
178
+ actives.splice(0, actives.length, ...toArray(val));
179
+ } else {
180
+ actives.splice(0, actives.length, val);
181
+ }
182
+ }
183
+ });
184
+ const modelValue = computed({
185
+ get() {
186
+ return props.modelValue ?? props.defaultValue;
187
+ },
188
+ set(val) {
189
+ emit("update:modelValue", val);
190
+ }
191
+ });
192
+ syncRef(currentValue, modelValue, { immediate: true, deep: true });
193
+ watch(currentValue, (val) => {
194
+ emit("change", val);
195
+ }, { deep: true });
196
+ provide(
197
+ ActiveClassSymbol,
198
+ computed(() => props.activeClass)
199
+ );
200
+ provide(
201
+ UnactiveSymbol,
202
+ computed(() => props.unactiveClass)
203
+ );
204
+ provide(
205
+ ItemClassSymbol,
206
+ computed(() => props.itemClass)
207
+ );
208
+ provide(ItemLabelSymbol, computed(() => props.label));
209
+ provide(ActivateEventSymbol, computed(() => props.activateEvent));
210
+ function isActive(value) {
211
+ return actives.includes(value);
212
+ }
213
+ function changeActive(option) {
214
+ if (isActive(option)) {
215
+ if (props.multiple) {
216
+ actives.splice(actives.indexOf(option), 1);
217
+ }
218
+ } else {
219
+ if (props.multiple) {
220
+ const limit = typeof props.multiple === "number" ? props.multiple : Infinity;
221
+ if (actives.length < limit) {
222
+ actives.push(option);
223
+ }
224
+ } else {
225
+ actives.splice(0, actives.length, option);
226
+ }
227
+ }
228
+ }
229
+ provide(IsActiveSymbol, isActive);
230
+ provide(ChangeActiveSymbol, changeActive);
231
+ provide(InitSymbol, (option) => {
232
+ function remove() {
233
+ const index = options.findIndex((e) => e.id === option.id);
234
+ if (index > -1) {
235
+ options.splice(index, 1);
236
+ emit("unload", option);
237
+ }
238
+ }
239
+ for (let i = 0; i < options.length; i++) {
240
+ if (options[i].value === option.value) {
241
+ options.splice(i, 1);
242
+ i--;
243
+ }
244
+ }
245
+ options.push(option);
246
+ emit("load", option);
247
+ return remove;
248
+ });
249
+ function renderItem() {
250
+ const children = options.filter((e) => actives.includes(e.value)).map((e) => e.render());
251
+ return props.multiple ? children : children[0];
252
+ }
253
+ function render() {
254
+ return h(props.tag, {}, renderSlot(slots, "default", {}));
255
+ }
256
+ return {
257
+ options,
258
+ actives,
259
+ isActive,
260
+ changeActive,
261
+ renderItem,
262
+ render
263
+ };
264
+ }
265
+ });
266
+ const HiSelection = defineComponent({
267
+ name: "HiSelection",
268
+ props: selectionListProps,
269
+ emits: selectionListEmits,
270
+ setup(props, context) {
271
+ const { render } = useSelectionList(props, context);
272
+ return () => render();
273
+ }
274
+ });
275
+
276
+ const switchProps = defineHookProps({
277
+ modelValue: {
278
+ type: Boolean,
279
+ default: false
280
+ },
281
+ class: {
282
+ type: classPropType,
283
+ default: ""
284
+ },
285
+ activeClass: {
286
+ type: classPropType,
287
+ default: "checked"
288
+ },
289
+ unactiveClass: {
290
+ type: classPropType,
291
+ default: "unchecked"
292
+ },
293
+ activateEvent: {
294
+ type: String,
295
+ default: "click"
296
+ },
297
+ tag: {
298
+ type: String,
299
+ default: "div"
300
+ }
301
+ });
302
+ const switchEmits = defineHookEmits(["update:modelValue", "change"]);
303
+ const useSwitch = defineHookComponent({
304
+ props: switchProps,
305
+ emits: switchEmits,
306
+ setup(props, { emit }) {
307
+ const checked = ref(!!props.modelValue);
308
+ watch(
309
+ () => props.modelValue,
310
+ (val) => {
311
+ checked.value = !!val;
312
+ }
313
+ );
314
+ const modelValue = computed({
315
+ get() {
316
+ return !!(props.modelValue ?? checked.value);
317
+ },
318
+ set(val) {
319
+ checked.value = val;
320
+ emit("update:modelValue", val);
321
+ emit("change", val);
322
+ }
323
+ });
324
+ const toggle = function(value) {
325
+ if (typeof value === "boolean") {
326
+ modelValue.value = value;
327
+ } else {
328
+ modelValue.value = !modelValue.value;
329
+ }
330
+ };
331
+ const switchClass = computed(() => {
332
+ return [props.class, modelValue.value ? props.activeClass : props.unactiveClass];
333
+ });
334
+ return {
335
+ toggle,
336
+ modelValue,
337
+ switchClass
338
+ };
339
+ }
340
+ });
341
+ const HiSwitch = defineComponent({
342
+ name: "ISwitch",
343
+ props: switchProps,
344
+ emits: switchEmits,
345
+ setup(props, context) {
346
+ const { slots } = context;
347
+ const { switchClass, toggle } = useSwitch(props, context);
348
+ return () => {
349
+ return h(props.tag, {
350
+ class: switchClass.value,
351
+ [`on${capitalize(props.activateEvent)}`]: toggle
352
+ }, renderSlot(slots, "default"));
353
+ };
354
+ }
355
+ });
356
+
357
+ export { HiItem, HiSelection, HiSwitch, defineHookComponent, defineHookEmits, defineHookProps, selectionItemProps, selectionListEmits, selectionListProps, switchProps, useSelectionItem, useSelectionList, useSwitch };
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "hoci",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "main": "dist/index.cjs",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist/"
10
+ ],
11
+ "author": "chizuki",
12
+ "license": "MIT",
13
+ "dependencies": {
14
+ "@vueuse/core": "^9.5.0",
15
+ "maybe-types": "^0.0.3",
16
+ "vue": "^3.2.45"
17
+ },
18
+ "scripts": {
19
+ "build": "unbuild",
20
+ "stub": "unbuild --stub"
21
+ }
22
+ }