Skip to content

Commit f6ce8d7

Browse files
authored
feat(dropdown): 添加自定义宽度的功能 (#17)
1 parent 6bea1a7 commit f6ce8d7

File tree

4 files changed

+67
-46
lines changed

4 files changed

+67
-46
lines changed

packages/devui-vue/devui/dropdown/src/dropdown-types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ export const dropdownProps = {
3636
showAnimation: {
3737
type: Boolean,
3838
default: true
39+
},
40+
width: {
41+
type: [Number, String],
42+
default: '102px'
3943
}
40-
4144
} as const
4245

4346
export type DropdownProps = ExtractPropTypes<typeof dropdownProps>

packages/devui-vue/devui/dropdown/src/dropdown.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
}
99
}
1010

11+
.devui-dropdown-menu-wrap .devui-dropdown-menu {
12+
width: 100%;
13+
}
14+
1115
.devui-dropdown-animation span {
1216
&.icon-chevron-down,
1317
&.icon-select-arrow {

packages/devui-vue/devui/dropdown/src/dropdown.tsx

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,30 @@ export default defineComponent({
4343
return props.showAnimation ? visible.value : true;
4444
});
4545

46-
return () => {
47-
// let vnodes = ctx.slots.default?.() ?? [];
48-
return (
49-
<>
50-
<FlexibleOverlay
51-
origin={props.origin}
52-
v-model:visible={visible.value}
53-
position={position}
54-
hasBackdrop={false}
46+
const wrapStyle = computed(() => typeof props.width === 'string' ? {
47+
width: props.width,
48+
}: {
49+
width: `${props.width}px`,
50+
});
51+
52+
return () => (
53+
<FlexibleOverlay
54+
origin={props.origin}
55+
v-model:visible={visible.value}
56+
position={position}
57+
hasBackdrop={false}
58+
>
59+
<Transition name="devui-dropdown-fade">
60+
<div
61+
ref={dropdownEl}
62+
class="devui-dropdown-menu-wrap"
63+
style={wrapStyle.value}
64+
v-show={animatedVisible.value}
5565
>
56-
<Transition name="devui-dropdown-fade">
57-
<div v-show={animatedVisible.value} ref={dropdownEl} style="min-width:102px">
58-
{ctx.slots.default?.()}
59-
</div>
60-
</Transition>
61-
</FlexibleOverlay>
62-
</>
63-
)
64-
};
66+
{ctx.slots.default?.()}
67+
</div>
68+
</Transition>
69+
</FlexibleOverlay>
70+
);
6571
}
6672
})

packages/devui-vue/docs/components/dropdown/index.md

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,38 @@
1212

1313
```vue
1414
<template>
15-
<div style="display:flex">
16-
触发方式:
17-
<d-radio-group direction="row" v-model="trigger">
18-
<d-radio v-for="item in triggerList" :key="item" :value="item">
19-
{{ item }}
20-
</d-radio>
21-
</d-radio-group>
15+
<div class="space-y-s">
16+
<div class="flex items-center">
17+
触发方式:
18+
<d-radio-group direction="row" v-model="trigger">
19+
<d-radio v-for="item in triggerList" :key="item" :value="item">
20+
{{ item }}
21+
</d-radio>
22+
</d-radio-group>
23+
</div>
24+
<div class="flex items-center">
25+
关闭区域:
26+
<d-radio-group direction="row" v-model="closeScope">
27+
<d-radio v-for="item in closeScopeAreas" :key="item" :value="item">
28+
{{ item }}
29+
</d-radio>
30+
</d-radio-group>
31+
</div>
32+
33+
<div class="flex items-center">
34+
仅当鼠标从菜单移除时才关闭:
35+
<d-switch v-model:checked="closeOnMouseLeaveMenu"></d-switch>
36+
</div>
37+
38+
<div class="flex items-center">
39+
动画开关:
40+
<d-switch v-model:checked="showAnimation"></d-switch>
41+
</div>
42+
<div class="flex items-center">
43+
自定义宽度:
44+
<d-input-number v-model="width" :max="400" :min="100" :step="100" /> px
45+
</div>
2246
</div>
23-
<div style="display:flex">
24-
关闭区域:
25-
<d-radio-group direction="row" v-model="closeScope">
26-
<d-radio v-for="item in closeScopeAreas" :key="item" :value="item">
27-
{{ item }}
28-
</d-radio>
29-
</d-radio-group>
30-
</div>
31-
32-
<div style="display:flex">
33-
仅当鼠标从菜单移除时才关闭:
34-
<d-switch v-model:checked="closeOnMouseLeaveMenu"></d-switch>
35-
</div>
36-
37-
<div style="display:flex">
38-
动画开关:
39-
<d-switch v-model:checked="showAnimation"></d-switch>
40-
</div>
41-
4247
<d-button ref="origin" style="margin-top: 20px; margin-right: 10px">More</d-button>
4348
<d-button
4449
v-show="trigger === 'manually'"
@@ -53,6 +58,7 @@
5358
:closeScope="closeScope"
5459
:closeOnMouseLeaveMenu="closeOnMouseLeaveMenu"
5560
:showAnimation="showAnimation"
61+
:width="width"
5662
>
5763
<ul class="devui-dropdown-menu" role="menu">
5864
<li role="menuitem">
@@ -84,7 +90,8 @@ export default defineComponent({
8490
closeScope: ref('blank'),
8591
closeScopeAreas: ['all', 'blank', 'none'],
8692
closeOnMouseLeaveMenu: ref(false),
87-
showAnimation: ref(true)
93+
showAnimation: ref(true),
94+
width: ref(100)
8895
}
8996
}
9097
})
@@ -122,6 +129,7 @@ d-dropdown 参数
122129
| closeScope | `CloseScopeArea` | `all` | 可选,点击关闭区域,blank 点击非菜单空白才关闭, all 点击菜单内外都关闭,none 菜单内外均不关闭仅下拉按键可以关闭 |
123130
| closeOnMouseLeaveMenu | `boolean` | `false` | 可选,是否进入菜单后离开菜单的时候关闭菜单 |
124131
| showAnimation | `boolean` | `true` | 可选,是否开启动画 |
132+
| width | `number \| string` | `102px` | 可选,对 dropdown 内容的宽度进行自定义 |
125133

126134
TriggerType 类型
127135
```typescript

0 commit comments

Comments
 (0)