@v-c/collapse 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.
@@ -0,0 +1,94 @@
1
+ import { createVNode as A, mergeProps as K } from "vue";
2
+ import { toArray as v } from "@v-c/util/dist/Children/toArray";
3
+ import { isEmptyElement as w } from "@v-c/util/dist/props-util";
4
+ import { cloneElement as E } from "@v-c/util/dist/vnode";
5
+ import P from "../Panel.js";
6
+ function h(e, a) {
7
+ const {
8
+ prefixCls: s,
9
+ accordion: n,
10
+ collapsible: t,
11
+ onItemClick: y,
12
+ activeKey: p,
13
+ openMotion: m,
14
+ expandIcon: C,
15
+ classNames: d,
16
+ styles: u
17
+ } = a;
18
+ return e.map((b, r) => {
19
+ const {
20
+ label: k,
21
+ key: I,
22
+ collapsible: N,
23
+ onItemClick: g,
24
+ ...c
25
+ } = b, o = String(I ?? r), f = N ?? t, i = (x) => {
26
+ f !== "disabled" && (y?.(x), g?.(x));
27
+ };
28
+ let l = !1;
29
+ return n ? l = p?.[0] === o : l = p.includes(o), A(P, K(c, {
30
+ classNames: d,
31
+ styles: u,
32
+ prefixCls: s,
33
+ key: o,
34
+ panelKey: o,
35
+ isActive: l,
36
+ accordion: n,
37
+ openMotion: m,
38
+ expandIcon: C,
39
+ header: k,
40
+ collapsible: f,
41
+ onItemClick: i
42
+ }), null);
43
+ });
44
+ }
45
+ function M(e, a, s) {
46
+ if (w(e) || !e) return null;
47
+ if (typeof e == "boolean" || typeof e == "number" || typeof e == "string")
48
+ return e;
49
+ const {
50
+ prefixCls: n,
51
+ accordion: t,
52
+ collapsible: y,
53
+ onItemClick: p,
54
+ activeKey: m,
55
+ openMotion: C,
56
+ expandIcon: d,
57
+ classNames: u,
58
+ styles: b
59
+ } = s, r = e.key || String(a), {
60
+ header: k,
61
+ headerClass: I,
62
+ collapsible: N,
63
+ onItemClick: g
64
+ } = e.props || {};
65
+ let c = !1;
66
+ t ? c = m[0] === r : c = m.includes(r);
67
+ const o = N ?? y, f = (l) => {
68
+ o !== "disabled" && (p?.(l), g?.(l));
69
+ }, i = {
70
+ key: r,
71
+ panelKey: r,
72
+ header: k,
73
+ headerClass: I,
74
+ classNames: u,
75
+ styles: b,
76
+ isActive: c,
77
+ prefixCls: n,
78
+ openMotion: C,
79
+ accordion: t,
80
+ children: e.children?.default?.(),
81
+ onItemClick: f,
82
+ expandIcon: d,
83
+ collapsible: o
84
+ };
85
+ return Object.keys(i).forEach((l) => {
86
+ typeof i[l] > "u" && delete i[l];
87
+ }), E(e, i);
88
+ }
89
+ function q(e, a, s) {
90
+ return Array.isArray(e) ? h(e, s) : v(a?.()).map((n, t) => M(n, t, s));
91
+ }
92
+ export {
93
+ q as useItems
94
+ };
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("./Collapse.cjs"),l=require("./Panel.cjs");exports.default=e.default;exports.Panel=l.default;
@@ -0,0 +1,5 @@
1
+ import { default as Collapse } from './Collapse';
2
+ import { default as Panel } from './Panel';
3
+ export type { CollapsePanelProps, CollapseProps } from './interface';
4
+ export default Collapse;
5
+ export { Panel };
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ import e from "./Collapse.js";
2
+ import { default as r } from "./Panel.js";
3
+ export {
4
+ r as Panel,
5
+ e as default
6
+ };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function e(){return{id:String,header:[String,Object],prefixCls:String,headerClass:String,showArrow:{type:Boolean,default:!0},className:String,classNames:{type:Object,default:()=>({})},style:Object,styles:{type:Object,default:()=>({})},isActive:Boolean,openMotion:Object,destroyInactivePanel:Boolean,accordion:Boolean,forceRender:Boolean,onItemClick:Function,extra:[String,Object],panelKey:[String,Number],collapsible:String,expandIcon:Function,role:String,children:[Object,String]}}function t(){return{prefixCls:String,activeKey:[String,Number,Array],defaultActiveKey:[String,Number,Array],openMotion:Object,onChange:Function,accordion:Boolean,className:String,classNames:Object,style:Object,styles:Object,destroyInactivePanel:Boolean,expandIcon:Function,collapsible:String,items:Array}}function n(){return{isActive:Boolean,prefixCls:String,className:String,classNames:Object,style:Object,styles:Object,role:String,forceRender:Boolean}}exports.generatorCollapsePanelContentProps=n;exports.generatorCollapsePanelProps=e;exports.generatorCollapseProps=t;
@@ -0,0 +1,118 @@
1
+ import { Key, VueNode } from '../../util/src/type';
2
+ import { CSSProperties, PropType, Ref, TransitionProps, VNode } from 'vue';
3
+ export type CollapsibleType = 'header' | 'icon' | 'disabled';
4
+ export type SemanticName = 'header' | 'title' | 'body' | 'icon';
5
+ export declare function generatorCollapsePanelProps(): {
6
+ id: StringConstructor;
7
+ header: PropType<string | VNode>;
8
+ prefixCls: StringConstructor;
9
+ headerClass: StringConstructor;
10
+ showArrow: {
11
+ type: PropType<boolean | undefined>;
12
+ default: boolean;
13
+ };
14
+ className: StringConstructor;
15
+ classNames: {
16
+ type: PropType<Partial<Record<SemanticName, string>>>;
17
+ default: () => {};
18
+ };
19
+ style: PropType<Record<string, string>>;
20
+ styles: {
21
+ type: PropType<Partial<Record<SemanticName, CSSProperties>>>;
22
+ default: () => {};
23
+ };
24
+ isActive: BooleanConstructor;
25
+ openMotion: PropType<Partial<TransitionProps>>;
26
+ destroyInactivePanel: PropType<boolean | undefined>;
27
+ accordion: BooleanConstructor;
28
+ forceRender: PropType<boolean | undefined>;
29
+ onItemClick: PropType<(props: Key) => void>;
30
+ extra: PropType<string | VueNode>;
31
+ panelKey: (StringConstructor | NumberConstructor)[];
32
+ collapsible: PropType<CollapsibleType>;
33
+ expandIcon: PropType<(props: any) => VueNode>;
34
+ role: StringConstructor;
35
+ children: PropType<VueNode | string>;
36
+ };
37
+ export interface CollapsePanelProps {
38
+ id?: string;
39
+ header?: VueNode;
40
+ prefixCls?: string;
41
+ headerClass?: string;
42
+ showArrow?: boolean;
43
+ className?: string;
44
+ classNames?: Partial<Record<SemanticName, string>>;
45
+ style?: object;
46
+ styles?: Partial<Record<SemanticName, CSSProperties>>;
47
+ isActive?: boolean;
48
+ openMotion?: TransitionProps;
49
+ destroyOnHidden?: boolean;
50
+ accordion?: boolean;
51
+ forceRender?: boolean;
52
+ extra?: VueNode;
53
+ onItemClick?: (panelKey: Key) => void;
54
+ expandIcon?: (props: object) => any;
55
+ panelKey?: Key;
56
+ role?: string;
57
+ collapsible?: CollapsibleType;
58
+ children?: VueNode | string;
59
+ }
60
+ export interface ItemType extends Omit<CollapsePanelProps, 'header' | 'prefixCls' | 'panelKey' | 'isActive' | 'accordion' | 'openMotion' | 'expandIcon'> {
61
+ key?: CollapsePanelProps['panelKey'];
62
+ label?: CollapsePanelProps['header'];
63
+ children?: VueNode;
64
+ ref?: Ref<HTMLDivElement>;
65
+ }
66
+ export declare function generatorCollapseProps(): {
67
+ prefixCls: StringConstructor;
68
+ activeKey: PropType<Key | Key[]>;
69
+ defaultActiveKey: PropType<Key | Key[]>;
70
+ openMotion: ObjectConstructor;
71
+ onChange: PropType<(key: Key[]) => void>;
72
+ accordion: BooleanConstructor;
73
+ className: StringConstructor;
74
+ classNames: PropType<Partial<Record<SemanticName, string>>>;
75
+ style: ObjectConstructor;
76
+ styles: PropType<Partial<Record<SemanticName, CSSProperties>>>;
77
+ destroyInactivePanel: BooleanConstructor;
78
+ expandIcon: PropType<(props: any) => VueNode>;
79
+ collapsible: PropType<CollapsibleType>;
80
+ items: PropType<ItemType[]>;
81
+ };
82
+ export interface CollapseProps {
83
+ prefixCls?: string;
84
+ activeKey?: Key | Key[];
85
+ defaultActiveKey?: Key | Key[];
86
+ openMotion?: TransitionProps;
87
+ onChange?: (key: Key[]) => void;
88
+ accordion?: boolean;
89
+ className?: string;
90
+ style?: object;
91
+ destroyOnHidden?: boolean;
92
+ expandIcon?: (props: object) => any;
93
+ collapsible?: CollapsibleType;
94
+ /**
95
+ * Collapse items content
96
+ * @since 3.6.0
97
+ */
98
+ items?: ItemType[];
99
+ classNames?: Partial<Record<SemanticName, string>>;
100
+ styles?: Partial<Record<SemanticName, CSSProperties>>;
101
+ }
102
+ export declare function generatorCollapsePanelContentProps(): {
103
+ isActive: BooleanConstructor;
104
+ prefixCls: StringConstructor;
105
+ className: StringConstructor;
106
+ classNames: PropType<{
107
+ header?: string;
108
+ body?: string;
109
+ }>;
110
+ style: PropType<Record<string, string>>;
111
+ styles: PropType<{
112
+ header?: CSSProperties;
113
+ body?: CSSProperties;
114
+ }>;
115
+ role: StringConstructor;
116
+ forceRender: BooleanConstructor;
117
+ };
118
+ export type { Key, };
@@ -0,0 +1,69 @@
1
+ function e() {
2
+ return {
3
+ id: String,
4
+ header: [String, Object],
5
+ prefixCls: String,
6
+ headerClass: String,
7
+ showArrow: {
8
+ type: Boolean,
9
+ default: !0
10
+ },
11
+ className: String,
12
+ classNames: {
13
+ type: Object,
14
+ default: () => ({})
15
+ },
16
+ style: Object,
17
+ styles: {
18
+ type: Object,
19
+ default: () => ({})
20
+ },
21
+ isActive: Boolean,
22
+ openMotion: Object,
23
+ destroyInactivePanel: Boolean,
24
+ accordion: Boolean,
25
+ forceRender: Boolean,
26
+ onItemClick: Function,
27
+ extra: [String, Object],
28
+ panelKey: [String, Number],
29
+ collapsible: String,
30
+ expandIcon: Function,
31
+ role: String,
32
+ children: [Object, String]
33
+ };
34
+ }
35
+ function t() {
36
+ return {
37
+ prefixCls: String,
38
+ activeKey: [String, Number, Array],
39
+ defaultActiveKey: [String, Number, Array],
40
+ openMotion: Object,
41
+ onChange: Function,
42
+ accordion: Boolean,
43
+ className: String,
44
+ classNames: Object,
45
+ style: Object,
46
+ styles: Object,
47
+ destroyInactivePanel: Boolean,
48
+ expandIcon: Function,
49
+ collapsible: String,
50
+ items: Array
51
+ };
52
+ }
53
+ function n() {
54
+ return {
55
+ isActive: Boolean,
56
+ prefixCls: String,
57
+ className: String,
58
+ classNames: Object,
59
+ style: Object,
60
+ styles: Object,
61
+ role: String,
62
+ forceRender: Boolean
63
+ };
64
+ }
65
+ export {
66
+ n as generatorCollapsePanelContentProps,
67
+ e as generatorCollapsePanelProps,
68
+ t as generatorCollapseProps
69
+ };
@@ -0,0 +1,43 @@
1
+ <script setup lang="ts">
2
+ import type { CollapseProps } from '../src/interface'
3
+ import { h } from 'vue'
4
+ import Collapse from '../src/index'
5
+ import CustomIcon from './custom-icon.vue'
6
+ import Simple from './simple.vue'
7
+ import '../assets/index.less'
8
+
9
+ const basicCollapseItems: CollapseProps['items'] = [
10
+ {
11
+ label: h('input', {
12
+ on: { keydown: (e: MouseEvent) => e.stopPropagation() },
13
+ }),
14
+ children: 'content',
15
+ },
16
+ {
17
+ label: 'title 2',
18
+ children: 'content 2',
19
+ collapsible: 'disabled',
20
+ },
21
+ {
22
+ label: 'title 3',
23
+ children: 'content 3',
24
+ onItemClick: console.log,
25
+ },
26
+ ]
27
+ </script>
28
+
29
+ <template>
30
+ <Story title="Collapse">
31
+ <Variant title="Basic">
32
+ <Collapse :items="basicCollapseItems" />
33
+ </Variant>
34
+
35
+ <Variant title="custom icon">
36
+ <CustomIcon />
37
+ </Variant>
38
+
39
+ <Variant title="simple">
40
+ <Simple />
41
+ </Variant>
42
+ </Story>
43
+ </template>
@@ -0,0 +1,185 @@
1
+ <script setup lang="ts">
2
+ import type { CollapseProps } from '../src/index'
3
+ import { h, ref, shallowRef, watch } from 'vue'
4
+ import Collapse from '../src/index'
5
+
6
+ const initLength = 3
7
+
8
+ const text = `
9
+ A dog is a type of domesticated animal.
10
+ Known for its loyalty and faithfulness,
11
+ it can be found as a welcome guest in many households across the world.
12
+ `
13
+
14
+ function random() {
15
+ return parseInt((Math.random() * 10).toString(), 10) + 1
16
+ }
17
+
18
+ const arrowPath
19
+ = 'M869 487.8L491.2 159.9c-2.9-2.5-6.6-3.9-10.5-3.9h-88'
20
+ + '.5c-7.4 0-10.8 9.2-5.2 14l350.2 304H152c-4.4 0-8 3.6-8 8v60c0 4.4 3.'
21
+ + '6 8 8 8h585.1L386.9 854c-5.6 4.9-2.2 14 5.2 14h91.5c1.9 0 3.8-0.7 5.'
22
+ + '2-2L869 536.2c14.7-12.8 14.7-35.6 0-48.4z'
23
+
24
+ function expandIcon({ isActive }: { isActive: boolean }) {
25
+ return h(
26
+ 'i',
27
+ {
28
+ style: {
29
+ 'margin-right': '.5rem',
30
+ },
31
+ },
32
+ [
33
+ h(
34
+ 'svg',
35
+ {
36
+ viewBox: '0 0 1024 1024',
37
+ width: '1em',
38
+ height: '1em',
39
+ fill: 'currentColor',
40
+ style: {
41
+ verticalAlign: '-.125em',
42
+ transition: 'transform .2s',
43
+ transform: `rotate(${isActive ? 90 : 0}deg)`,
44
+ },
45
+ },
46
+ [
47
+ h('path', {
48
+ d: arrowPath,
49
+ }),
50
+ ],
51
+ ),
52
+ ],
53
+ )
54
+ }
55
+
56
+ const rerender = ref(0)
57
+ const accordion = ref(false)
58
+ const activeKey = ref<Array<string | number | bigint>>(['4'])
59
+
60
+ const time = ref(random())
61
+ watch(
62
+ () => [rerender.value, accordion.value, activeKey.value],
63
+ () => {
64
+ time.value = random()
65
+ },
66
+ )
67
+
68
+ function handleSetActiveKey(e: Array<string | number | bigint>) {
69
+ activeKey.value = e
70
+ }
71
+
72
+ function getCollapsedHeight(el: HTMLDivElement) {
73
+ el.style.height = '0'
74
+ el.style.opacity = '0'
75
+ }
76
+ function getRealHeight(el: HTMLDivElement) {
77
+ console.log(el.scrollHeight)
78
+ el.style.height = `${el.scrollHeight}px`
79
+ el.style.opacity = '1'
80
+ }
81
+ function getCurrentHeight(el: HTMLDivElement) {
82
+ el.style.height = `${el.offsetHeight}px`
83
+ }
84
+
85
+ function skipOpacityTransition(el: HTMLDivElement) {
86
+ el.style.height = ''
87
+ }
88
+ const openMotion = {
89
+ name: 'vc-collapse',
90
+ onBeforeEnter: getCollapsedHeight,
91
+ onEnter: getRealHeight,
92
+ onAfterEnter: skipOpacityTransition,
93
+ onBeforeLeave: getCurrentHeight,
94
+ onLeave: getCollapsedHeight,
95
+ onAfterLeave: skipOpacityTransition,
96
+ }
97
+
98
+ const items = shallowRef<CollapseProps['items']>([])
99
+ function handleUpdateItems() {
100
+ items.value = Array.from({ length: initLength })
101
+ .map((_, i) => {
102
+ return {
103
+ label: `This is panel header ${i + 1}`,
104
+ key: i + 1,
105
+ children: h('p', {}, [text.repeat(time.value)]),
106
+ }
107
+ })
108
+ .concat([
109
+ {
110
+ label: `This is panel header ${initLength + 1}`,
111
+ key: initLength + 1,
112
+ children: h(Collapse, {
113
+ defaultActiveKey: '1',
114
+ expandIcon,
115
+ items: [
116
+ {
117
+ label: 'This is panel nest panel',
118
+ key: '1',
119
+ children: h('p', {}, [text]),
120
+ },
121
+ ],
122
+ }),
123
+ },
124
+ {
125
+ label: `This is panel header ${initLength + 2}`,
126
+ key: initLength + 2,
127
+ children: h(Collapse, {
128
+ defaultActiveKey: '1',
129
+ items: [
130
+ {
131
+ label: 'This is panel nest panel',
132
+ children: h('form', {}, [
133
+ h(
134
+ 'label',
135
+ {
136
+ htmlFor: 'test',
137
+ },
138
+ ['Name:&nbsp;'],
139
+ ),
140
+ h('input', {
141
+ type: 'text',
142
+ id: 'test',
143
+ }),
144
+ ]),
145
+ },
146
+ ],
147
+ }),
148
+ },
149
+ ])
150
+ }
151
+ watch(
152
+ () => time.value,
153
+ () => {
154
+ handleUpdateItems()
155
+ },
156
+ { immediate: true },
157
+ )
158
+ </script>
159
+
160
+ <template>
161
+ <button type="button" @click="rerender += 1">
162
+ reRender
163
+ </button>
164
+ <br>
165
+ <br>
166
+ <button type="button" @click="accordion = !accordion">
167
+ {{ accordion ? "Mode: accordion" : "Mode: collapse" }}
168
+ </button>
169
+ <br>
170
+ <br>
171
+ <button type="button" @click="activeKey = ['2']">
172
+ active header 2
173
+ </button>
174
+ <br>
175
+ <br>
176
+
177
+ <Collapse
178
+ :accordion="accordion"
179
+ :active-key="activeKey"
180
+ :expand-icon="expandIcon"
181
+ :open-motion="openMotion"
182
+ :items="items"
183
+ @change="handleSetActiveKey"
184
+ />
185
+ </template>