@v-c/collapse 0.0.4 → 1.0.0

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/assets/index.less DELETED
@@ -1,117 +0,0 @@
1
- @prefixCls: vc-collapse;
2
- @text-color: #666;
3
- @borderStyle: 1px solid #d9d9d9;
4
-
5
- @import "./motion.less";
6
-
7
- #arrow {
8
- .common() {
9
- width: 0;
10
- height: 0;
11
- font-size: 0;
12
- line-height: 0;
13
- }
14
- .right(@w, @h, @color) {
15
- border-top: @w solid transparent;
16
- border-bottom: @w solid transparent;
17
- border-left: @h solid @color;
18
- }
19
-
20
- .bottom(@w, @h, @color) {
21
- border-left: @w solid transparent;
22
- border-right: @w solid transparent;
23
- border-top: @h solid @color;
24
- }
25
- }
26
-
27
- .@{prefixCls} {
28
- background-color: #f7f7f7;
29
- border-radius: 3px;
30
- border: @borderStyle;
31
-
32
- // &-anim-active {
33
- // transition: height 0.2s ease-out;
34
- // }
35
-
36
- & > &-item {
37
- border-top: @borderStyle;
38
- &:first-child {
39
- border-top: none;
40
- }
41
-
42
- > .@{prefixCls}-header {
43
- display: flex;
44
- align-items: center;
45
- line-height: 22px;
46
- padding: 10px 16px;
47
- color: #666;
48
- cursor: pointer;
49
- .arrow {
50
- display: inline-block;
51
- content: "\20";
52
- #arrow > .common();
53
- #arrow > .right(3px, 4px, #666);
54
- vertical-align: middle;
55
- margin-right: 8px;
56
- }
57
-
58
- .@{prefixCls}-extra {
59
- margin: 0 16px 0 auto;
60
- }
61
- }
62
- .@{prefixCls}-collapsible-header {
63
- cursor: default;
64
- .@{prefixCls}-title {
65
- cursor: pointer;
66
- }
67
- .@{prefixCls}-expand-icon {
68
- cursor: pointer;
69
- }
70
- }
71
- .@{prefixCls}-collapsible-icon {
72
- cursor: default;
73
- .@{prefixCls}-expand-icon {
74
- cursor: pointer;
75
- }
76
- }
77
- }
78
-
79
- & > &-item-disabled > .@{prefixCls}-header {
80
- cursor: not-allowed;
81
- color: #999;
82
- background-color: #f3f3f3;
83
- }
84
-
85
- &-panel {
86
- overflow: hidden;
87
- color: @text-color;
88
- padding: 0 16px;
89
- background-color: #fff;
90
-
91
- & > &-box {
92
- margin-top: 16px;
93
- margin-bottom: 16px;
94
- }
95
-
96
- // &-inactive {
97
- // display: none;
98
- // }
99
- }
100
-
101
- &-item:last-child {
102
- > .@{prefixCls}-panel {
103
- border-radius: 0 0 3px 3px;
104
- }
105
- }
106
-
107
- & > &-item-active {
108
- > .@{prefixCls}-header {
109
- .arrow {
110
- position: relative;
111
- top: 2px;
112
- #arrow > .bottom(3px, 4px, #666);
113
- margin-right: 6px;
114
- }
115
- }
116
- }
117
- }
@@ -1,14 +0,0 @@
1
- @prefixCls: vc-collapse;
2
-
3
- .@{prefixCls} {
4
- &-enter-active,
5
- &-leave-active {
6
- transition:
7
- height 0.3s,
8
- opacity 0.3s;
9
- }
10
-
11
- &-panal-hidden {
12
- display: none;
13
- }
14
- }
@@ -1,45 +0,0 @@
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
- onKeydown: (e: KeyboardEvent) => {
13
- e.stopPropagation()
14
- },
15
- }),
16
- children: 'content',
17
- },
18
- {
19
- label: 'title 2',
20
- children: 'content 2',
21
- collapsible: 'disabled',
22
- },
23
- {
24
- label: 'title 3',
25
- children: 'content 3',
26
- onItemClick: console.log,
27
- },
28
- ]
29
- </script>
30
-
31
- <template>
32
- <Story title="Collapse">
33
- <Variant title="Basic">
34
- <Collapse :items="basicCollapseItems" />
35
- </Variant>
36
-
37
- <Variant title="custom icon">
38
- <CustomIcon />
39
- </Variant>
40
-
41
- <Variant title="simple">
42
- <Simple />
43
- </Variant>
44
- </Story>
45
- </template>
@@ -1,185 +0,0 @@
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>
package/docs/simple.vue DELETED
@@ -1,218 +0,0 @@
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
- const collapsible = ref<CollapseProps['collapsible']>()
60
-
61
- const time = ref(random())
62
- watch(
63
- () => [rerender.value, accordion.value, activeKey.value],
64
- () => {
65
- time.value = random()
66
- },
67
- )
68
-
69
- function handleSetActiveKey(e: Array<string | number | bigint>) {
70
- activeKey.value = e
71
- }
72
-
73
- function getCollapsedHeight(el: HTMLDivElement) {
74
- el.style.height = '0'
75
- el.style.opacity = '0'
76
- }
77
- function getRealHeight(el: HTMLDivElement) {
78
- console.log(el.scrollHeight)
79
- el.style.height = `${el.scrollHeight}px`
80
- el.style.opacity = '1'
81
- }
82
- function getCurrentHeight(el: HTMLDivElement) {
83
- el.style.height = `${el.offsetHeight}px`
84
- }
85
-
86
- function skipOpacityTransition(el: HTMLDivElement) {
87
- el.style.height = ''
88
- }
89
- const openMotion = {
90
- name: 'vc-collapse',
91
- onBeforeEnter: getCollapsedHeight,
92
- onEnter: getRealHeight,
93
- onAfterEnter: skipOpacityTransition,
94
- onBeforeLeave: getCurrentHeight,
95
- onLeave: getCollapsedHeight,
96
- onAfterLeave: skipOpacityTransition,
97
- }
98
-
99
- const items = shallowRef<CollapseProps['items']>([])
100
- function handleUpdateItems() {
101
- items.value = Array.from({ length: initLength })
102
- .map((_, i) => {
103
- return {
104
- label: `This is panel header ${i + 1}`,
105
- key: i + 1,
106
- children: h('p', {}, [text.repeat(time.value)]),
107
- }
108
- })
109
-
110
- items.value = items.value.concat([
111
- {
112
- label: `This is panel header ${initLength + 1}`,
113
- key: initLength + 1,
114
- children: h(Collapse, {
115
- defaultActiveKey: '1',
116
- expandIcon,
117
- items: [
118
- {
119
- label: 'This is panel nest panel',
120
- key: '1',
121
- children: h('p', {}, [text]),
122
- },
123
- ],
124
- }),
125
- },
126
- {
127
- label: `This is panel header ${initLength + 2}`,
128
- key: initLength + 2,
129
- children: h(Collapse, {
130
- defaultActiveKey: '1',
131
- items: [
132
- {
133
- label: 'This is panel nest panel',
134
- children: h('form', {}, [
135
- h(
136
- 'label',
137
- {
138
- htmlFor: 'test',
139
- },
140
- ['Name:&nbsp;'],
141
- ),
142
- h('input', {
143
- type: 'text',
144
- id: 'test',
145
- }),
146
- ]),
147
- },
148
- ],
149
- }),
150
- },
151
- {
152
- label: `This is panel header ${initLength + 3}`,
153
- key: initLength + 3,
154
- extra: h('span', {}, ['Extra Node']),
155
- children: h('p', {}, ['Panel with extra']),
156
- },
157
- ])
158
- }
159
- watch(
160
- () => time.value,
161
- () => {
162
- handleUpdateItems()
163
- },
164
- { immediate: true },
165
- )
166
-
167
- function handleCollapsibleChange(e: Event) {
168
- const values = [undefined, 'header', 'icon', 'disabled']
169
- collapsible.value = Reflect.get(values, (e.target as HTMLSelectElement).value) as CollapseProps['collapsible']
170
- console.log(collapsible.value)
171
- }
172
- </script>
173
-
174
- <template>
175
- <button type="button" @click="rerender += 1">
176
- reRender
177
- </button>
178
- <br>
179
- <br>
180
- <button type="button" @click="accordion = !accordion">
181
- {{ accordion ? "Mode: accordion" : "Mode: collapse" }}
182
- </button>
183
- <br>
184
- <br>
185
- <div>
186
- collapsible:
187
- <select @change="handleCollapsibleChange">
188
- <option :value="0">
189
- default
190
- </option>
191
- <option :value="1">
192
- header
193
- </option>
194
- <option :value="2">
195
- icon
196
- </option>
197
- <option :value="3">
198
- disabled
199
- </option>
200
- </select>
201
- </div>
202
- <br>
203
- <button type="button" @click="activeKey = ['2']">
204
- active header 2
205
- </button>
206
- <br>
207
- <br>
208
-
209
- <Collapse
210
- :accordion="accordion"
211
- :active-key="activeKey"
212
- :expand-icon="expandIcon"
213
- :open-motion="openMotion"
214
- :collapsible="collapsible"
215
- :items="items"
216
- @change="handleSetActiveKey"
217
- />
218
- </template>
package/src/Collapse.tsx DELETED
@@ -1,109 +0,0 @@
1
- import type { Ref } from 'vue'
2
- import type { CollapseProps, Key } from './interface'
3
- import { classNames as classnames } from '@v-c/util'
4
- import useMergedState from '@v-c/util/dist/hooks/useMergedState'
5
- import omit from '@v-c/util/dist/omit'
6
- import pickAttrs from '@v-c/util/dist/pickAttrs'
7
- import { defineComponent, ref, toRef } from 'vue'
8
- import { useItems } from './hooks/useItems'
9
-
10
- function getActiveKeysArray(activeKey: Key | Array<Key>) {
11
- let currentActiveKey = activeKey
12
- if (!Array.isArray(currentActiveKey)) {
13
- const activeKeyType = typeof currentActiveKey
14
- currentActiveKey
15
- = activeKeyType === 'number' || activeKeyType === 'string'
16
- ? [currentActiveKey]
17
- : []
18
- }
19
- return currentActiveKey.map(key => String(key))
20
- }
21
-
22
- const defaults = {
23
- prefixCls: 'vc-collapse',
24
- } as any
25
-
26
- const Collapse = defineComponent<CollapseProps>({
27
- name: 'VcCollapse',
28
- inheritAttrs: false,
29
- setup(props = defaults, { attrs, expose, slots }) {
30
- const refWrapper = ref<HTMLDivElement>()
31
-
32
- const [activeKey, setActiveKey] = useMergedState<
33
- Key | Key[],
34
- Ref<Array<Key>>
35
- >([], {
36
- value: toRef(props, 'activeKey') as Ref<Key | Key[]>,
37
- onChange: v => props.onChange?.(v as Key[]),
38
- defaultValue: props.defaultActiveKey,
39
- postState: getActiveKeysArray,
40
- })
41
-
42
- const getActiveKey = (key: Key) => {
43
- if (props.accordion) {
44
- return activeKey.value[0] === key ? [] : [key]
45
- }
46
-
47
- const index = activeKey.value.indexOf(key)
48
- const isActive = index > -1
49
- if (isActive) {
50
- return activeKey.value.filter(item => item !== key)
51
- }
52
-
53
- return [...activeKey.value, key]
54
- }
55
- const onItemClick = (key: Key) => {
56
- activeKey.value = getActiveKey(key)
57
- setActiveKey(activeKey.value)
58
- }
59
-
60
- expose({
61
- ref: refWrapper,
62
- })
63
-
64
- return () => {
65
- const {
66
- prefixCls = 'vc-collapse',
67
- openMotion,
68
- expandIcon,
69
- collapsible,
70
- accordion,
71
- classNames,
72
- styles,
73
- items,
74
- destroyOnHidden,
75
- } = props
76
-
77
- const collapseClassName = classnames(prefixCls, (attrs as any).class)
78
-
79
- const mergedProps = { ...props, ...omit(attrs, ['class', 'style']) }
80
-
81
- const mergedChildren = useItems(items, slots.default, {
82
- prefixCls,
83
- accordion,
84
- openMotion,
85
- expandIcon,
86
- collapsible,
87
- onItemClick,
88
- activeKey: activeKey.value,
89
- destroyOnHidden,
90
- classNames,
91
- styles,
92
- })
93
-
94
- return (
95
- <div
96
- ref={refWrapper}
97
- class={collapseClassName}
98
- style={(attrs as any).style}
99
- role={accordion ? 'tablist' : undefined}
100
- {...pickAttrs(mergedProps, { aria: true, data: true })}
101
- >
102
- {mergedChildren}
103
- </div>
104
- )
105
- }
106
- },
107
- })
108
-
109
- export default Collapse