@v-c/collapse 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.
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,110 +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 pickAttrs from '@v-c/util/dist/pickAttrs'
6
- import { defineComponent, ref, toRef } from 'vue'
7
- import { useItems } from './hooks/useItems'
8
-
9
- function getActiveKeysArray(activeKey: Key | Array<Key>) {
10
- let currentActiveKey = activeKey
11
- if (!Array.isArray(currentActiveKey)) {
12
- const activeKeyType = typeof currentActiveKey
13
- currentActiveKey
14
- = activeKeyType === 'number' || activeKeyType === 'string'
15
- ? [currentActiveKey]
16
- : []
17
- }
18
- return currentActiveKey.map(key => String(key))
19
- }
20
-
21
- const defaults = {
22
- prefixCls: 'vc-collapse',
23
- } as any
24
-
25
- const Collapse = defineComponent<CollapseProps>({
26
- name: 'VcCollapse',
27
- inheritAttrs: false,
28
- setup(props = defaults, { attrs, expose, slots }) {
29
- const refWrapper = ref<HTMLDivElement>()
30
-
31
- const [activeKey, setActiveKey] = useMergedState<
32
- Key | Key[],
33
- Ref<Array<Key>>
34
- >([], {
35
- value: toRef(props, 'activeKey') as Ref<Key | Key[]>,
36
- onChange: v => props.onChange?.(v as Key[]),
37
- defaultValue: props.defaultActiveKey,
38
- postState: getActiveKeysArray,
39
- })
40
-
41
- const getActiveKey = (key: Key) => {
42
- if (props.accordion) {
43
- return activeKey.value[0] === key ? [] : [key]
44
- }
45
-
46
- const index = activeKey.value.indexOf(key)
47
- const isActive = index > -1
48
- if (isActive) {
49
- return activeKey.value.filter(item => item !== key)
50
- }
51
-
52
- return [...activeKey.value, key]
53
- }
54
- const onItemClick = (key: Key) => {
55
- activeKey.value = getActiveKey(key)
56
- setActiveKey(activeKey.value)
57
- }
58
-
59
- expose({
60
- ref: refWrapper,
61
- })
62
-
63
- return () => {
64
- const {
65
- prefixCls = 'vc-collapse',
66
- className,
67
- style,
68
- openMotion,
69
- expandIcon,
70
- collapsible,
71
- accordion,
72
- classNames,
73
- styles,
74
- items,
75
- destroyOnHidden,
76
- } = props
77
-
78
- const collapseClassName = classnames(prefixCls, className)
79
-
80
- const mergedProps = { ...props, ...attrs }
81
-
82
- const mergedChildren = useItems(items, slots.default, {
83
- prefixCls,
84
- accordion,
85
- openMotion,
86
- expandIcon,
87
- collapsible,
88
- onItemClick,
89
- activeKey: activeKey.value,
90
- destroyOnHidden,
91
- classNames,
92
- styles,
93
- })
94
-
95
- return (
96
- <div
97
- ref={refWrapper}
98
- class={collapseClassName}
99
- style={style as any}
100
- role={accordion ? 'tablist' : undefined}
101
- {...pickAttrs(mergedProps, { aria: true, data: true })}
102
- >
103
- {mergedChildren}
104
- </div>
105
- )
106
- }
107
- },
108
- })
109
-
110
- export default Collapse
package/src/Panel.tsx DELETED
@@ -1,178 +0,0 @@
1
- import type { HTMLAttributes } from 'vue'
2
- import type { CollapsePanelProps } from './interface'
3
- import { classNames as classnames } from '@v-c/util'
4
- import KeyCode from '@v-c/util/dist/KeyCode'
5
- import omit from '@v-c/util/dist/omit'
6
- import { computed, defineComponent, ref, Transition } from 'vue'
7
- import PanelContent from './PanelContent'
8
-
9
- const defaults = {
10
- showArrow: true,
11
- classNames: {},
12
- styles: {},
13
- } as any
14
-
15
- const CollapsePanel = defineComponent<CollapsePanelProps>({
16
- name: 'CollapsePanel',
17
- inheritAttrs: false,
18
- setup(props = defaults, { attrs, expose }) {
19
- const disabled = computed(() => props.collapsible === 'disabled')
20
- const refWrapper = ref()
21
- const ifExtraExist = computed(
22
- () =>
23
- props.extra !== null
24
- && props.extra !== undefined
25
- && typeof props.extra !== 'boolean',
26
- )
27
-
28
- const collapsibleProps = computed(() => {
29
- return {
30
- 'onClick': () => {
31
- props.onItemClick?.(props.panelKey!)
32
- },
33
- 'onKeydown': (e: KeyboardEvent) => {
34
- if (
35
- e.key === 'Enter'
36
- || e.keyCode === KeyCode.ENTER
37
- || e.which === KeyCode.ENTER
38
- ) {
39
- props.onItemClick?.(props.panelKey!)
40
- }
41
- },
42
- 'role': props.accordion ? 'tab' : 'button',
43
- 'aria-expanded': props.isActive,
44
- 'aria-disabled': disabled.value,
45
- 'tabIndex': disabled.value ? -1 : 0,
46
- }
47
- })
48
-
49
- expose({
50
- ref: refWrapper,
51
- })
52
-
53
- return () => {
54
- const {
55
- extra,
56
- prefixCls,
57
- isActive,
58
- class: className,
59
- expandIcon,
60
- forceRender,
61
- headerClass,
62
- collapsible,
63
- accordion,
64
- openMotion = {},
65
- onItemClick,
66
- classNames: customizeClassNames = {},
67
- showArrow = true,
68
- destroyOnHidden,
69
- styles = {},
70
- header,
71
- panelKey,
72
- children,
73
- ...restProps
74
- } = props
75
-
76
- const collapsePanelClassNames = classnames(
77
- `${prefixCls}-item`,
78
- {
79
- [`${prefixCls}-item-active`]: isActive,
80
- [`${prefixCls}-item-disabled`]: disabled.value,
81
- },
82
- className,
83
- )
84
- const headerClassName = classnames(
85
- headerClass,
86
- `${prefixCls}-header`,
87
- {
88
- [`${prefixCls}-collapsible-${collapsible}`]: !!collapsible,
89
- },
90
- customizeClassNames.header,
91
- )
92
-
93
- const headerProps: HTMLAttributes = {
94
- class: headerClassName,
95
- style: styles.header,
96
- ...(['header', 'icon'].includes(collapsible!)
97
- ? {}
98
- : collapsibleProps.value),
99
- }
100
-
101
- // ======================== Icon ========================
102
- const iconNodeInner
103
- = typeof expandIcon === 'function'
104
- ? (
105
- expandIcon(props)
106
- )
107
- : (
108
- <i class="arrow" />
109
- )
110
- const iconNode = iconNodeInner && (
111
- <div
112
- class={classnames(`${prefixCls}-expand-icon`, customizeClassNames?.icon)}
113
- style={styles?.icon}
114
- {...(['header', 'icon'].includes(collapsible!)
115
- ? collapsibleProps.value
116
- : {})}
117
- >
118
- {iconNodeInner}
119
- </div>
120
- )
121
-
122
- const panelContent = (
123
- <PanelContent
124
- v-show={isActive}
125
- prefixCls={prefixCls}
126
- classNames={customizeClassNames}
127
- styles={styles}
128
- isActive={isActive}
129
- forceRender={forceRender}
130
- role={accordion ? 'tabpanel' : undefined}
131
- v-slots={{ default: () => children }}
132
- />
133
- )
134
-
135
- const transitionProps = {
136
- 'appear': false,
137
- 'leave-to-class': `${prefixCls}-panel-hidden`,
138
- ...openMotion,
139
- }
140
-
141
- const mergedRestProps = {
142
- ...restProps,
143
- ...omit(attrs, ['class']),
144
- }
145
-
146
- return (
147
- <div
148
- {...mergedRestProps as any}
149
- ref={refWrapper}
150
- class={collapsePanelClassNames}
151
- >
152
- <div {...headerProps}>
153
- {showArrow && iconNode}
154
- <span
155
- class={classnames(
156
- `${prefixCls}-title`,
157
- customizeClassNames?.title,
158
- )}
159
- style={styles?.title}
160
- {...(collapsible === 'header' ? collapsibleProps.value : {})}
161
- >
162
- {header}
163
- </span>
164
- {ifExtraExist.value && (
165
- <div class={`${prefixCls}-extra`}>{extra}</div>
166
- )}
167
- </div>
168
-
169
- <Transition {...transitionProps}>
170
- { !destroyOnHidden || isActive ? panelContent : null}
171
- </Transition>
172
- </div>
173
- )
174
- }
175
- },
176
- })
177
-
178
- export default CollapsePanel
@@ -1,63 +0,0 @@
1
- import type { CollapsePanelProps } from './interface'
2
- import { classNames as classnames } from '@v-c/util'
3
- import { defineComponent, ref, watch } from 'vue'
4
-
5
- const PanelContent = defineComponent<CollapsePanelProps>({
6
- name: 'PanelContent',
7
- inheritAttrs: false,
8
- setup(props, { slots }) {
9
- const rendered = ref(props.isActive || props.forceRender)
10
-
11
- watch(
12
- () => [props.isActive, props.forceRender],
13
- () => {
14
- if (props.isActive || props.forceRender) {
15
- rendered.value = true
16
- }
17
- },
18
- )
19
-
20
- return () => {
21
- if (!rendered.value) {
22
- return null
23
- }
24
-
25
- const {
26
- prefixCls,
27
- isActive,
28
- style,
29
- role,
30
- class: className,
31
- classNames: customizeClassNames,
32
- styles,
33
- } = props
34
-
35
- return (
36
- <div
37
- class={classnames(
38
- `${prefixCls}-panel`,
39
- {
40
- [`${prefixCls}-panel-active`]: isActive,
41
- [`${prefixCls}-panel-inactive`]: !isActive,
42
- },
43
- className,
44
- )}
45
- style={style as any}
46
- role={role}
47
- >
48
- <div
49
- class={classnames(
50
- `${prefixCls}-body`,
51
- customizeClassNames?.body,
52
- )}
53
- style={styles?.body}
54
- >
55
- {slots.default?.()}
56
- </div>
57
- </div>
58
- )
59
- }
60
- },
61
- })
62
-
63
- export default PanelContent