From 7d4c9af382a9a5ff32a53a827fab958e575aaaa5 Mon Sep 17 00:00:00 2001 From: neverland Date: Sun, 17 Jan 2021 15:09:52 +0800 Subject: [PATCH] feat(Cascader): add field-names prop (#7933) --- src/cascader/README.md | 45 ++++++++++ src/cascader/README.zh-CN.md | 63 ++++++++++++-- src/cascader/demo/index.vue | 87 +++++++++++++++++-- src/cascader/index.js | 38 +++++--- .../test/__snapshots__/demo.spec.js.snap | 35 +++++--- src/cascader/test/index.spec.js | 47 ++++++++++ 6 files changed, 276 insertions(+), 39 deletions(-) diff --git a/src/cascader/README.md b/src/cascader/README.md index 55ed09ef3..4ebee93dc 100644 --- a/src/cascader/README.md +++ b/src/cascader/README.md @@ -147,6 +147,50 @@ export default { }; ``` +### Custom Field Names + +```html + +``` + +```js +import { ref } from 'vue'; + +export default { + setup() { + const code = ref(''); + const fieldNames = { + text: 'name', + value: 'code', + children: 'items', + }; + const options = [ + { + name: 'Zhejiang', + code: '330000', + items: [{ name: 'Hangzhou', code: '330100' }], + }, + { + name: 'Jiangsu', + code: '320000', + items: [{ name: 'Nanjing', code: '320100' }], + }, + ]; + + return { + code, + options, + fieldNames, + }; + }, +}; +``` + ## API ### Props @@ -159,6 +203,7 @@ export default { | placeholder | Placeholder of unselected tab | _string_ | `Select` | | active-color | Active color | _string_ | `#ee0a24` | | closeable | Whether to show close icon | _boolean_ | `true` | +| field-names `v3.0.4` | Custom the fields of options | _object_ | `{ text: 'text', value: 'value', children: 'children' }` | ### Events diff --git a/src/cascader/README.zh-CN.md b/src/cascader/README.zh-CN.md index 4433e49dd..75f790c04 100644 --- a/src/cascader/README.zh-CN.md +++ b/src/cascader/README.zh-CN.md @@ -159,18 +159,65 @@ export default { }; ``` +### 自定义字段名 + +通过 `field-names` 属性可以自定义 `options` 里的字段名称。 + +```html + +``` + +```js +import { ref } from 'vue'; + +export default { + setup() { + const code = ref(''); + const fieldNames = { + text: 'name', + value: 'code', + children: 'items', + }; + const options = [ + { + name: '浙江省', + code: '330000', + items: [{ name: '杭州市', code: '330100' }], + }, + { + name: '江苏省', + code: '320000', + items: [{ name: '南京市', code: '320100' }], + }, + ]; + + return { + code, + options, + fieldNames, + }; + }, +}; +``` + ## API ### Props -| 参数 | 说明 | 类型 | 默认值 | -| ------------ | ------------------ | ------------------ | --------- | -| title | 顶部标题 | _string_ | - | -| value | 选中项的值 | _string \| number_ | - | -| options | 可选项数据源 | _Option[]_ | `[]` | -| placeholder | 未选中时的提示文案 | _string_ | `请选择` | -| active-color | 选中状态的高亮颜色 | _string_ | `#ee0a24` | -| closeable | 是否显示关闭图标 | _boolean_ | `true` | +| 参数 | 说明 | 类型 | 默认值 | +| --- | --- | --- | --- | +| title | 顶部标题 | _string_ | - | +| value | 选中项的值 | _string \| number_ | - | +| options | 可选项数据源 | _Option[]_ | `[]` | +| placeholder | 未选中时的提示文案 | _string_ | `请选择` | +| active-color | 选中状态的高亮颜色 | _string_ | `#ee0a24` | +| closeable | 是否显示关闭图标 | _boolean_ | `true` | +| field-names `v3.0.4` | 自定义 `options` 结构中的字段 | _object_ | `{ text: 'text', value: 'value', children: 'children' }` | ### Events diff --git a/src/cascader/demo/index.vue b/src/cascader/demo/index.vue index e6d7bebf7..09b6222fa 100644 --- a/src/cascader/demo/index.vue +++ b/src/cascader/demo/index.vue @@ -8,7 +8,7 @@ :placeholder="t('selectArea')" @click="base.show = true" /> - + - + - + + + + + + + +