Cascader
级联选择器
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 { Cascader, Form, FormItem, Submit } from '@silver-formily/element-plus'
type IAddress = { name: string, code: string, cities: number, districts: number } | string
function transformAddress(data = {}) {
return Object.entries(data).reduce(
(
buf,
[key, value]: [string, IAddress],
) => {
if (typeof value === 'string')
return buf.concat({ label: value, value: key })
const { name, code, cities, districts } = value as any
const _cities = transformAddress(cities)
const _districts = transformAddress(districts)
return buf.concat({
label: name,
value: code,
children: _cities.length > 0
? _cities
: (_districts.length > 0
? _districts
: undefined),
})
},
[],
)
}
function useAddress(pattern) {
onFieldReact(pattern, (field: DataField) => {
if (globalThis.window === undefined)
return
field.loading = true
fetch('/location.json')
.then(res => res.json())
.then(
action.bound((data) => {
field.dataSource = transformAddress(data)
field.loading = false
}),
)
})
}
const form = createForm({
effects: () => {
useAddress('address')
},
})
const { SchemaField, SchemaStringField } = createSchemaField({
components: {
FormItem,
Cascader,
},
})
async function onSubmit(value) {
console.log(value)
}
</script>
<template>
<Form :form="form">
<SchemaField>
<SchemaStringField
name="address"
title="地址选择"
required
x-decorator="FormItem"
x-component="Cascader"
:x-component-props="{
style: {
width: '240px',
},
}"
/>
</SchemaField>
<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 { Cascader, Form, FormItem, Submit } from '@silver-formily/element-plus'
type IAddress = { name: string, code: string, cities: number, districts: number } | string
function transformAddress(data = {}) {
return Object.entries(data).reduce(
(
buf,
[key, value]: [string, IAddress],
) => {
if (typeof value === 'string')
return buf.concat({ label: value, value: key })
const { name, code, cities, districts } = value as any
const _cities = transformAddress(cities)
const _districts = transformAddress(districts)
return buf.concat({
label: name,
value: code,
children: _cities.length > 0
? _cities
: (_districts.length > 0
? _districts
: undefined),
})
},
[],
)
}
function useAsyncDataSource(url, transform) {
return (field) => {
if (globalThis.window === undefined)
return
field.loading = true
fetch(url)
.then(res => res.json())
.then(
action.bound((data) => {
field.dataSource = transform(data)
field.loading = false
}),
)
}
}
const schema = {
type: 'object',
properties: {
cascader: {
'type': 'string',
'title': '地址选择',
'x-decorator': 'FormItem',
'x-component': 'Cascader',
'x-component-props': {
style: {
width: '240px',
},
},
'x-reactions': [
'{{useAsyncDataSource("/location.json",transformAddress)}}',
],
},
},
}
const form = createForm()
const { SchemaField } = createSchemaField({
components: {
FormItem,
Cascader,
},
})
async function onSubmit(value) {
console.log(value)
}
</script>
<template>
<Form :form="form">
<SchemaField :schema="schema" :scope="{ useAsyncDataSource, transformAddress }" />
<Submit @submit="onSubmit">
提交
</Submit>
</Form>
</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 { Cascader, Form, FormItem, Submit } from '@silver-formily/element-plus'
type IAddress = { name: string, code: string, cities: number, districts: number } | string
function transformAddress(data = {}) {
return Object.entries(data).reduce(
(
buf,
[key, value]: [string, IAddress],
) => {
if (typeof value === 'string')
return buf.concat({ label: value, value: key })
const { name, code, cities, districts } = value as any
const _cities = transformAddress(cities)
const _districts = transformAddress(districts)
return buf.concat({
label: name,
value: code,
children: _cities.length > 0
? _cities
: (_districts.length > 0
? _districts
: undefined),
})
},
[],
)
}
function useAddress(pattern) {
onFieldReact(pattern, (field: DataField) => {
if (globalThis.window === undefined)
return
field.loading = true
fetch('/location.json')
.then(res => res.json())
.then(
action.bound((data) => {
field.dataSource = transformAddress(data)
field.loading = false
}),
)
})
}
const form = createForm({
effects: () => {
useAddress('address')
},
})
async function onSubmit(value) {
console.log(value)
}
</script>
<template>
<Form :form="form">
<Field
name="address"
title="地址选择"
required
:decorator="[FormItem]"
:component="[
Cascader,
{
style: {
width: '240px',
},
},
]"
/>
<Submit @submit="onSubmit">
提交
</Submit>
</Form>
</template>API
参考 https://cn.element-plus.org/zh-CN/component/cascader.html