Select
下拉框组件
Markup Schema 同步数据源案例
vue
<script lang="ts" setup>
import { createForm } from '@formily/core'
import { createSchemaField, FormProvider } from '@formily/vue'
import { FormItem, Select, Submit } from '@silver-formily/element-plus'
const form = createForm()
const { SchemaField, SchemaStringField } = createSchemaField({
components: {
FormItem,
Select,
},
})
async function log(value) {
console.log(value)
}
</script>
<template>
<FormProvider :form="form">
<SchemaField>
<SchemaStringField
name="select"
title="选择框"
x-decorator="FormItem"
x-component="Select"
:x-component-props="{
style: {
width: '240px',
},
}"
:enum="[
{
label: '选项1',
value: 1,
},
{
label: '选项2',
value: 2,
},
]"
/>
<SchemaStringField
name="select2"
title="选择框2"
x-decorator="FormItem"
x-component="Select"
:x-component-props="{
style: {
width: '240px',
},
}"
:enum="[
{
label: '选项1',
value: 1,
},
{
label: '选项2',
value: 2,
},
]"
/>
</SchemaField>
<Submit @submit="log">
提交
</Submit>
</FormProvider>
</template>Markup Schema 异步搜索案例
vue
<script lang="ts" setup>
import type { DataField } from '@formily/core'
import { createForm, onFieldInit, onFieldReact } from '@formily/core'
import { action, observable } from '@formily/reactive'
import { createSchemaField, FormProvider } from '@formily/vue'
import { FormItem, Select, Submit } from '@silver-formily/element-plus'
let timeout
function fetchData(value, callback) {
if (timeout) {
clearTimeout(timeout)
timeout = null
}
function fake() {
callback([
{
label: 'AAA',
value: 'aaa',
},
{
label: 'BBB',
value: 'ccc',
},
])
}
timeout = setTimeout(fake, 300)
}
function useAsyncDataSource(pattern, service) {
const keyword = observable.ref('')
onFieldInit(pattern, (field) => {
field.setComponentProps({
remoteMethod: (value) => {
keyword.value = value
},
})
})
onFieldReact(pattern, (field: DataField) => {
field.loading = true
service({ field, keyword: keyword.value }).then(
action.bound((data) => {
field.dataSource = data
field.loading = false
}),
)
})
}
const form = createForm({
effects: () => {
useAsyncDataSource('select', async ({ keyword }) => {
if (!keyword) {
return []
}
return new Promise((resolve) => {
fetchData(keyword, resolve)
})
})
},
})
const { SchemaField, SchemaStringField } = createSchemaField({
components: {
FormItem,
Select,
},
})
async function log(value) {
console.log(value)
}
</script>
<template>
<FormProvider :form="form">
<SchemaField>
<SchemaStringField
name="select"
title="异步搜索选择框"
x-decorator="FormItem"
x-component="Select"
:x-component-props="{
filterable: true,
remote: true,
style: {
width: '240px',
},
}"
/>
</SchemaField>
<Submit @submit="log">
提交
</Submit>
</FormProvider>
</template>Markup Schema 异步联动数据源案例
vue
<script lang="ts" setup>
import type { DataField } from '@formily/core'
import { createForm, onFieldReact } from '@formily/core'
import { action } from '@formily/reactive'
import { createSchemaField } from '@formily/vue'
import { Form, FormItem, Select, Submit } from '@silver-formily/element-plus'
function useAsyncDataSource(pattern, service) {
onFieldReact(pattern, (field: DataField) => {
field.loading = true
service(field).then(
action.bound((data) => {
field.dataSource = data
field.loading = false
}),
)
})
}
const form = createForm({
effects: () => {
useAsyncDataSource('select', async (field) => {
const linkage = field.query('linkage').get('value')
if (!linkage)
return []
return new Promise((resolve) => {
setTimeout(() => {
if (linkage === 1) {
resolve([
{
label: 'AAA',
value: 'aaa',
},
{
label: 'BBB',
value: 'ccc',
},
])
}
else if (linkage === 2) {
resolve([
{
label: 'CCC',
value: 'ccc',
},
{
label: 'DDD',
value: 'ddd',
},
])
}
}, 1500)
})
})
},
})
const { SchemaField, SchemaNumberField, SchemaStringField } = createSchemaField(
{
components: {
FormItem,
Select,
},
},
)
async function onSubmit(value) {
console.log(value)
}
</script>
<template>
<Form :form="form">
<SchemaField>
<SchemaNumberField
name="linkage"
title="联动选择框"
x-decorator="FormItem"
x-component="Select"
:enum="[
{ label: '发请求1', value: 1 },
{ label: '发请求2', value: 2 },
]"
:x-component-props="{
style: {
width: '240px',
},
}"
/>
<SchemaStringField
name="select"
title="异步选择框"
x-decorator="FormItem"
x-component="Select"
:x-component-props="{
style: {
width: '240px',
},
}"
/>
</SchemaField>
<Submit @submit="onSubmit">
提交
</Submit>
</Form>
</template>Markup Schema OptionGroup案例
vue
<script lang="ts" setup>
import { createForm } from '@formily/core'
import { createSchemaField, FormProvider } from '@formily/vue'
import { FormItem, Select, Submit } from '@silver-formily/element-plus'
const form = createForm()
const { SchemaField, SchemaStringField } = createSchemaField({
components: {
FormItem,
Select,
},
})
async function log(value) {
console.log(value)
}
</script>
<template>
<FormProvider :form="form">
<SchemaField>
<SchemaStringField
name="select"
title="选择框"
x-decorator="FormItem"
x-component="Select"
:x-component-props="{
style: {
width: '240px',
},
}"
:enum="[
{
label: 'Popular cities',
options: [
{
value: 'Shanghai',
label: 'Shanghai',
},
{
value: 'Beijing',
label: 'Beijing',
},
],
},
{
label: 'City name',
disabled: true,
options: [
{
value: 'Chengdu',
label: 'Chengdu',
},
{
value: 'Shenzhen',
label: 'Shenzhen',
},
{
value: 'Guangzhou',
label: 'Guangzhou',
},
{
value: 'Dalian',
label: 'Dalian',
},
],
},
]"
/>
</SchemaField>
<Submit @submit="log">
提交
</Submit>
</FormProvider>
</template>JSON Schema 同步数据源案例
vue
<script lang="ts" setup>
import { createForm } from '@formily/core'
import { createSchemaField } from '@formily/vue'
import { Form, FormItem, Select, Submit } from '@silver-formily/element-plus'
const schema = {
type: 'object',
properties: {
select: {
'type': 'string',
'title': '选择框',
'enum': [
{
label: '选项1',
value: 1,
},
{
label: '选项2',
value: 2,
},
],
'x-decorator': 'FormItem',
'x-component': 'Select',
'x-component-props': {
style: 'width: 240px;',
},
},
},
}
const form = createForm()
const { SchemaField } = createSchemaField({
components: {
FormItem,
Select,
},
})
async function onSubmit(value) {
console.log(value)
}
</script>
<template>
<Form :form="form">
<SchemaField :schema="schema" />
<Submit @submit="onSubmit">
提交
</Submit>
</Form>
</template>JSON Schema 异步联动数据源案例
vue
<script lang="ts" setup>
import { createForm } from '@formily/core'
import { action } from '@formily/reactive'
import { createSchemaField } from '@formily/vue'
import { Form, FormItem, Select, Submit } from '@silver-formily/element-plus'
const schema = {
type: 'object',
properties: {
linkage: {
'type': 'string',
'title': '联动选择框',
'enum': [
{
label: '发请求1',
value: 1,
},
{
label: '发请求2',
value: 2,
},
],
'x-decorator': 'FormItem',
'x-component': 'Select',
'x-component-props': {
style: 'width: 240px;',
},
},
select: {
'type': 'string',
'title': '异步选择框',
'x-decorator': 'FormItem',
'x-component': 'Select',
'x-component-props': {
style: 'width: 240px;',
},
'x-reactions': ['{{useAsyncDataSource(loadData)}}'],
},
},
}
function useAsyncDataSource(service) {
return (field) => {
field.loading = true
service(field).then(
action.bound((data) => {
field.dataSource = data
field.loading = false
}),
)
}
}
async function loadData(field) {
const linkage = field.query('linkage').get('value')
if (!linkage)
return []
return new Promise((resolve) => {
setTimeout(() => {
if (linkage === 1) {
resolve([
{
label: 'AAA',
value: 'aaa',
},
{
label: 'BBB',
value: 'ccc',
},
])
}
else if (linkage === 2) {
resolve([
{
label: 'CCC',
value: 'ccc',
},
{
label: 'DDD',
value: 'ddd',
},
])
}
}, 1500)
})
}
const form = createForm()
const { SchemaField } = createSchemaField({
components: {
FormItem,
Select,
},
})
async function onSubmit(value) {
console.log(value)
}
</script>
<template>
<Form :form="form">
<SchemaField :schema="schema" :scope="{ useAsyncDataSource, loadData }" />
<Submit @submit="onSubmit">
提交
</Submit>
</Form>
</template>Template 同步数据源案例
vue
<script lang="ts" setup>
import { createForm } from '@formily/core'
import { Field, FormProvider } from '@formily/vue'
import { FormItem, Select, Submit } from '@silver-formily/element-plus'
const form = createForm()
async function log(value) {
console.log(value)
}
</script>
<template>
<FormProvider :form="form">
<Field
name="select"
title="选择框"
:decorator="[FormItem]"
:component="[
Select,
{
style: {
width: '240px',
},
},
]"
:data-source="[
{
label: '选项1',
value: 1,
},
{
label: '选项2',
value: 2,
},
]"
/>
<Submit @submit="log">
提交
</Submit>
</FormProvider>
</template>Template 异步联动数据源案例
vue
<script lang="ts" setup>
import type { DataField } from '@formily/core'
import { createForm, onFieldReact } from '@formily/core'
import { action } from '@formily/reactive'
import { Field } from '@formily/vue'
import { Form, FormItem, Select, Submit } from '@silver-formily/element-plus'
function useAsyncDataSource(pattern, service) {
onFieldReact(pattern, (field: DataField) => {
field.loading = true
service(field).then(
action.bound((data) => {
field.dataSource = data
field.loading = false
}),
)
})
}
const form = createForm({
effects: () => {
useAsyncDataSource('select', async (field) => {
const linkage = field.query('linkage').get('value')
if (!linkage)
return []
return new Promise((resolve) => {
setTimeout(() => {
if (linkage === 1) {
resolve([
{
label: 'AAA',
value: 'aaa',
},
{
label: 'BBB',
value: 'ccc',
},
])
}
else if (linkage === 2) {
resolve([
{
label: 'CCC',
value: 'ccc',
},
{
label: 'DDD',
value: 'ddd',
},
])
}
}, 1500)
})
})
},
})
async function onSubmit(value) {
console.log(value)
}
</script>
<template>
<Form :form="form">
<Field
name="linkage"
title="联动选择框"
:decorator="[FormItem]"
:component="[
Select,
{
style: {
width: '240px',
},
},
]"
:data-source="[
{ label: '发请求1', value: 1 },
{ label: '发请求2', value: 2 },
]"
/>
<Field
name="select"
title="异步选择框"
:decorator="[FormItem]"
:component="[
Select,
{
style: {
width: '240px',
},
},
]"
/>
<Submit @submit="onSubmit">
提交
</Submit>
</Form>
</template>Template 作用域插槽
vue
<script lang="ts" setup>
import { createForm } from '@formily/core'
import { createSchemaField, FormProvider } from '@formily/vue'
import { FormItem, Select, Submit } from '@silver-formily/element-plus'
const form = createForm()
const { SchemaField, SchemaStringField } = createSchemaField({
components: {
FormItem,
Select,
},
})
async function log(value) {
console.log(value)
}
</script>
<template>
<FormProvider :form="form">
<SchemaField>
<SchemaStringField
name="select"
title="选择框"
x-decorator="FormItem"
x-component="Select"
:x-component-props="{
style: {
width: '240px',
},
}"
:enum="[
{
label: '选项1',
value: 1,
},
{
label: '选项2',
value: 2,
},
]"
:x-content="{
option: (props, { attrs }) => `${attrs.option?.label}-${attrs.option?.value}`,
}"
/>
<SchemaStringField
name="select2"
title="选择框2"
x-decorator="FormItem"
x-component="Select"
:x-component-props="{
style: {
width: '240px',
},
}"
:enum="[
{
label: '选项1',
value: 1,
},
{
label: '选项2',
value: 2,
},
]"
/>
</SchemaField>
<Submit @submit="log">
提交
</Submit>
</FormProvider>
</template>Template Header插槽
vue
<script lang="ts" setup>
import { createForm } from '@formily/core'
import { Field, FormProvider } from '@formily/vue'
import { FormItem, Select, Submit } from '@silver-formily/element-plus'
import { ElCheckbox } from 'element-plus'
const form = createForm()
async function log(value) {
console.log(value)
}
</script>
<template>
<FormProvider :form="form">
<Field
name="select"
title="选择框"
:decorator="[FormItem]"
:component="[
Select,
{
style: {
width: '240px',
},
multiple: true,
},
]"
:data-source="[
{
value: 'Beijing',
label: 'Beijing',
},
{
value: 'Shanghai',
label: 'Shanghai',
},
{
value: 'Nanjing',
label: 'Nanjing',
},
{
value: 'Chengdu',
label: 'Chengdu',
},
{
value: 'Shenzhen',
label: 'Shenzhen',
},
{
value: 'Guangzhou',
label: 'Guangzhou',
},
]"
>
<template #header="{ field }">
<ElCheckbox
:indeterminate="field.value?.length > 0 && field.value?.length < field?.dataSource?.length"
@change="(value) => value ? field?.setValue(field?.dataSource?.map((item) => item.value)) : field?.setValue([])"
>
All
</ElCheckbox>
</template>
<template #footer="{ field }">
Selected City: {{ field.value?.length ?? 0 }}
</template>
</Field>
<Submit @submit="log">
提交
</Submit>
</FormProvider>
</template>API
参考 https://cn.element-plus.org/zh-CN/component/select.html
扩展属性
| 属性名 | 类型 | 描述 | 默认值 |
|---|---|---|---|
| options | SelectOptionProps[] | 选项配置数组,一般情况下请通过dataSource来配置 | [] |
提示
- 如果在
options中有options数组则会渲染为OptionGroup,第一层的options属性会当作ElOptionGroup的属性。具体请参考Demo。 - 如果没有提供
valueKey配置项的话label属性会作为遍历的key值,请保证其唯一性。
插槽
提示
- 组件继承了
ElSelect的所有插槽。具体使用时请注意所使用的element-plus版本。 - 组件额外的为header、footer、tag插槽提供了field作用域。其余插槽没有改动。
- 组件目前无法使用
OptionGroup的default插槽。
| 插槽名 | 描述 | 类型 |
|---|---|---|
| header | 下拉框头部插槽 | object |
| footer | 下拉框尾部插槽 | object |
| prefix | Select头部插槽 | -- |
| empty | 无选项时插槽 | -- |
| tag | 自定义标签内容 | object |
| loading | 自定义loading | -- |
| label | 自定义标签内容 | object |