Skip to content

Commit f355cae

Browse files
author
Tenny
committed
fix(Collapse)
Signed-off-by: Tenny <tenny.shu@foxmail.com>
1 parent 418129b commit f355cae

File tree

3 files changed

+47
-43
lines changed

3 files changed

+47
-43
lines changed

docs/components/collapse.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,12 @@
168168

169169
### 项间距
170170

171-
通过设置 `item-space` 设置项间距
171+
通过设置 `gap` 设置项间距
172172

173173
<ClientOnly>
174174
<CodePreview>
175175
<textarea lang="vue-html">
176-
<nt-collapse arrow-placement="right" header-justify="space-between" background item-space="10px">
176+
<nt-collapse arrow-placement="right" header-justify="space-between" background gap="10px">
177177
<nt-collapse-item title="红灯" name="1" >
178178
<div>红灯 - 停</div>
179179
</nt-collapse-item>
@@ -195,7 +195,7 @@
195195
<ClientOnly>
196196
<CodePreview>
197197
<textarea lang="vue-html">
198-
<nt-collapse arrow-placement="right" header-justify="space-between" background border-radius="5px">
198+
<nt-collapse arrow-placement="right" header-justify="space-between" background border-radius="5px" gap="10px">
199199
<nt-collapse-item title="红灯" name="1" >
200200
<div>红灯 - 停</div>
201201
</nt-collapse-item>
@@ -220,7 +220,7 @@
220220
| `arrow-placement` | 箭头位置 | `left` \| `right` | `left` |
221221
| `header-justify` | 面板头部, 水平对齐方式 | `flex-start` \| `space-between` | `flex-start` |
222222
| `background` | 是否带有背景和边框 | `boolean` | `false` |
223-
| `item-space` | 面板之间的间隔 | `string` | - |
223+
| `gap` | 面板之间的间隔 | `string` | - |
224224
| `default-expanded-names` | 默认展开的面板 | `(string \| number)[]` | - |
225225
| `border-radius` | 边框圆角 | `string` | - |
226226

src/components/collapse/Collapse.vue

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,93 +3,95 @@
33
class="nt-collapse"
44
:class="{
55
'nt-collapse--background': props.background,
6-
'nt-collapse--space': props.itemSpace != null
6+
'nt-collapse--space': props.gap != null,
77
}"
88
:style="styles"
99
>
1010
<slot></slot>
1111
</div>
1212
</template>
1313
<script setup lang="ts">
14-
import { computed, provide, ref, watch } from 'vue'
15-
import { collapseContext } from './constant'
14+
import { computed, provide, ref, watch } from 'vue';
15+
import { collapseContext } from './constant';
1616
1717
const props = withDefaults(
1818
defineProps<{
1919
/** 是否手风琴模式 */
20-
accordion?: boolean
20+
accordion?: boolean;
2121
/** 箭头位置 */
22-
arrowPlacement?: 'left' | 'right'
22+
arrowPlacement?: 'left' | 'right';
2323
/** 面板头部, 水平对齐方式 */
24-
headerJustify?: 'space-between' | 'flex-start'
24+
headerJustify?: 'space-between' | 'flex-start';
2525
/** 是否带有边框和背景 */
26-
background?: boolean
26+
background?: boolean;
2727
/** 面板之间的间隔 */
28-
itemSpace?: string
28+
gap?: string;
2929
/** 默认展开的面板 */
30-
defaultExpandedNames?: (string | number)[]
30+
defaultExpandedNames?: (string | number)[];
3131
/** 边框圆角 */
32-
borderRadius?: string
33-
modelValue?: (string | number)[]
32+
borderRadius?: string;
33+
modelValue?: (string | number)[];
3434
}>(),
3535
{
3636
accordion: false,
3737
arrowPlacement: 'left',
3838
headerJustify: undefined,
3939
background: false,
40-
itemSpace: undefined,
40+
gap: undefined,
4141
defaultExpandedNames: () => [],
42-
modelValue: () => []
43-
}
44-
)
42+
modelValue: () => [],
43+
},
44+
);
4545
46-
const emits = defineEmits(['change'])
46+
const emits = defineEmits(['change', 'update:modelValue']);
4747
4848
/** 当前展开的面板 */
4949
50-
const values = defineModel<(string | number)[]>({ default: [] })
51-
const actives = ref<(string | number)[]>(values.value)
50+
const actives = ref<(string | number)[]>(props.modelValue);
5251
5352
const styles = computed(() => {
54-
const styleRes: { [index: string]: string } = {}
55-
if (props.itemSpace != null) {
56-
styleRes['--nt-collapse-item-space'] = props.itemSpace
53+
const styleRes: { [index: string]: string } = {};
54+
if (props.gap != null) {
55+
styleRes['--nt-collapse-gap'] = props.gap;
5756
}
5857
if (props.borderRadius != null) {
59-
styleRes['--nt-collapse-border-radius'] = props.borderRadius
58+
styleRes['--nt-collapse-border-radius'] = props.borderRadius;
6059
}
61-
return styleRes
62-
})
60+
return styleRes;
61+
});
6362
6463
/**
6564
* 更新活动面板
6665
* @param name 面板名称
6766
*/
6867
function updateActive(name: string | number) {
69-
const index = actives.value.indexOf(name)
68+
const index = actives.value.indexOf(name);
7069
if (index === -1) {
7170
if (props.accordion) {
72-
actives.value = [name]
71+
actives.value = [name];
7372
} else {
74-
actives.value.push(name)
73+
actives.value.push(name);
7574
}
7675
} else {
77-
actives.value.splice(index, 1)
76+
actives.value.splice(index, 1);
7877
}
79-
values.value = actives.value
80-
emits('change', actives.value)
78+
emits('update:modelValue', actives.value);
79+
emits('change', actives.value);
8180
}
8281
83-
watch(values, (v) => {
84-
if (v != null) {
85-
actives.value = v
86-
}
87-
})
82+
watch(
83+
() => props.modelValue,
84+
(v) => {
85+
if (v != null) {
86+
actives.value = v;
87+
}
88+
},
89+
);
8890
8991
provide(collapseContext, {
9092
arrowPlacement: props.arrowPlacement,
9193
headerJustify: props.headerJustify,
9294
actives,
93-
toggle: updateActive
94-
})
95+
toggle: updateActive,
96+
});
9597
</script>

style/collapse/index.css

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
--nt-collapse-border-color: #dedede;
44
/* 圆角 */
55
--nt-collapse-border-radius: 0px;
6+
--nt-collapse-gap: 0px;
7+
display: grid;
8+
gap: var(--nt-collapse-gap, 0px);
9+
grid-template-columns: 1fr;
610
}
711

812
.nt-collapse-item {
913
border-bottom: 1px solid var(--nt-collapse-border-color);
1014
color: #333;
1115
font-size: 14px;
12-
margin-top: var(--nt-collapse-item-space, 0px);
1316
}
1417
.nt-collapse-item:first-child {
1518
border-top-left-radius: var(--nt-collapse-border-radius, 0px);
1619
border-top-right-radius: var(--nt-collapse-border-radius, 0px);
17-
margin-top: 0;
1820
}
1921
.nt-collapse-item:last-child {
2022
border-bottom-left-radius: var(--nt-collapse-border-radius, 0px);

0 commit comments

Comments
 (0)