Skip to content

Commit 81beda6

Browse files
hoorayCopilot
andcommitted
feat: 添加 FaSelect 组件的定位模式支持
Co-authored-by: Copilot <copilot@github.com>
1 parent 1d86ef8 commit 81beda6

17 files changed

Lines changed: 91 additions & 35 deletions
Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,25 @@
11
<script setup lang="ts">
2-
const modal = shallowRef(false)
3-
const cityInFaModal = shallowRef('shanghai')
2+
const popperValue = ref('2')
3+
const itemAlignedValue = ref('2')
44
55
const options = [
6-
{ label: 'Beijing', value: 'beijing' },
7-
{ label: 'Shanghai', value: 'shanghai' },
8-
{ label: 'Shenzhen', value: 'shenzhen' },
6+
{ label: 'Option 1', value: '1' },
7+
{ label: 'Option 2', value: '2' },
8+
{ label: 'Option 3', value: '3' },
99
]
1010
</script>
1111

1212
<template>
13-
<div>
14-
<div class="flex gap-2">
15-
<FaButton @click="modal = true">
16-
打开 FaModal
17-
</FaButton>
18-
</div>
19-
<FaModal
20-
v-model="modal"
21-
title="FaModal 中的选择器"
22-
description="在模态框内容区内使用 FaSelect"
23-
:footer="false"
24-
class="sm:max-w-lg"
25-
content-class="min-h-auto"
26-
>
27-
<div class="py-4 flex flex-col gap-4">
28-
<FaSelect
29-
v-model="cityInFaModal"
30-
:options="options"
31-
placeholder="请选择城市"
32-
class="w-full"
33-
/>
34-
<div class="text-sm text-muted-foreground">
35-
当前值:{{ cityInFaModal }}
36-
</div>
37-
</div>
38-
</FaModal>
13+
<div class="space-y-2">
14+
<FaSelect
15+
v-model="popperValue"
16+
:options="options"
17+
position="popper"
18+
/>
19+
<FaSelect
20+
v-model="itemAlignedValue"
21+
:options="options"
22+
position="item-aligned"
23+
/>
3924
</div>
4025
</template>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<script setup lang="ts">
2+
const modal = shallowRef(false)
3+
const cityInFaModal = shallowRef('shanghai')
4+
5+
const options = [
6+
{ label: 'Beijing', value: 'beijing' },
7+
{ label: 'Shanghai', value: 'shanghai' },
8+
{ label: 'Shenzhen', value: 'shenzhen' },
9+
]
10+
</script>
11+
12+
<template>
13+
<div>
14+
<div class="flex gap-2">
15+
<FaButton @click="modal = true">
16+
打开 FaModal
17+
</FaButton>
18+
</div>
19+
<FaModal
20+
v-model="modal"
21+
title="FaModal 中的选择器"
22+
description="在模态框内容区内使用 FaSelect"
23+
:footer="false"
24+
class="sm:max-w-lg"
25+
content-class="min-h-auto"
26+
>
27+
<div class="py-4 flex flex-col gap-4">
28+
<FaSelect
29+
v-model="cityInFaModal"
30+
:options="options"
31+
placeholder="请选择城市"
32+
class="w-full"
33+
/>
34+
<div class="text-sm text-muted-foreground">
35+
当前值:{{ cityInFaModal }}
36+
</div>
37+
</div>
38+
</FaModal>
39+
</div>
40+
</template>

‎apps/example/src/views/component_example/select/index.vue‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Demo1 from './_demo1.vue'
33
import Demo2 from './_demo2.vue'
44
import Demo3 from './_demo3.vue'
55
import Demo4 from './_demo4.vue'
6+
import Demo5 from './_demo5.vue'
67
</script>
78

89
<template>
@@ -23,10 +24,15 @@ import Demo4 from './_demo4.vue'
2324
<Demo3 />
2425
</div>
2526
</FaPageMain>
26-
<FaPageMain title="在 FaModal 里展示" main-class="p-0">
27+
<FaPageMain title="定位模式" main-class="p-0">
2728
<div class="p-4">
2829
<Demo4 />
2930
</div>
3031
</FaPageMain>
32+
<FaPageMain title="在 FaModal 里展示" main-class="p-0">
33+
<div class="p-4">
34+
<Demo5 />
35+
</div>
36+
</FaPageMain>
3137
</div>
3238
</template>

‎packages/components/src/select/README.md‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# FaSelect 选择器组件
22

3-
下拉选择器组件,支持单选、多选、分组和自定义选项
3+
下拉选择器组件,支持单选、多选、分组和下拉内容定位方式
44

55
## 使用场景
66

@@ -16,6 +16,7 @@
1616
|------|------|--------|------|
1717
| `multiple` | `boolean` | `false` | 是否多选 |
1818
| `disabled` | `boolean` | `false` | 是否禁用 |
19+
| `position` | `'popper' \| 'item-aligned'` | `'popper'` | 下拉内容的定位模式 |
1920
| `options` | `(Option \| GroupOption)[]` | **必需** | 选项数据 |
2021
| `placeholder` | `string` | - | 占位提示文本 |
2122
| `class` | `HTMLAttributes['class']` | - | 自定义 CSS 类 |
@@ -155,6 +156,28 @@ function handleChange(value) {
155156
</template>
156157
```
157158

159+
### 定位模式
160+
161+
```vue
162+
<script setup lang="ts">
163+
const popperValue = ref('2')
164+
const itemAlignedValue = ref('2')
165+
166+
const options = [
167+
{ label: '选项 1', value: '1' },
168+
{ label: '选项 2', value: '2' },
169+
{ label: '选项 3', value: '3' },
170+
]
171+
</script>
172+
173+
<template>
174+
<div class="flex flex-col gap-4">
175+
<FaSelect v-model="popperValue" :options="options" position="popper" />
176+
<FaSelect v-model="itemAlignedValue" :options="options" position="item-aligned" />
177+
</div>
178+
</template>
179+
```
180+
158181
### 禁用选择器
159182

160183
```vue
@@ -196,3 +219,4 @@ const cities = [
196219
3. **值类型**`value` 可以是字符串、数字等类型
197220
4. **分组显示**:当 `options` 中包含 `options` 属性时,会自动识别为分组选项
198221
5. **z-index**:下拉菜单的 z-index 默认为 2000,确保在其他内容上方显示
222+
6. **定位模式**`position="popper"` 时下拉内容跟随触发器定位,`position="item-aligned"` 时会更接近原生 Select 的对齐方式

‎packages/components/src/select/index.vue‎

100755100644
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import type { AcceptableValue } from 'reka-ui'
2+
import type { AcceptableValue, SelectContentProps } from 'reka-ui'
33
import type { HTMLAttributes } from 'vue'
44
import { useTextDirection } from '@vueuse/core'
55
import { cn } from '../../utils'
@@ -30,6 +30,7 @@ defineOptions({
3030
const props = defineProps<{
3131
multiple?: boolean
3232
disabled?: boolean
33+
position?: SelectContentProps['position']
3334
options: (Option | GroupOption)[]
3435
placeholder?: string
3536
class?: HTMLAttributes['class']
@@ -97,7 +98,7 @@ const selectedOption = computed({
9798
<SelectTrigger :class="cn('w-[200px]', props.class)">
9899
<SelectValue :placeholder="props.placeholder" :selected-option="selectedOption?.label" />
99100
</SelectTrigger>
100-
<SelectContent class="z-2000">
101+
<SelectContent :position class="z-2000">
101102
<template v-for="option in props.options" :key="option.label">
102103
<SelectGroup v-if="option.hasOwnProperty('options')">
103104
<SelectLabel>{{ option.label }}</SelectLabel>

‎packages/components/src/select/select/Select.vue‎

100755100644
File mode changed.

‎packages/components/src/select/select/SelectContent.vue‎

100755100644
File mode changed.

‎packages/components/src/select/select/SelectGroup.vue‎

100755100644
File mode changed.

‎packages/components/src/select/select/SelectItem.vue‎

100755100644
File mode changed.

‎packages/components/src/select/select/SelectItemText.vue‎

100755100644
File mode changed.

0 commit comments

Comments
 (0)