From 9ed18b2f9689c5c8609ace6e76f895d70db4b7e8 Mon Sep 17 00:00:00 2001 From: 07akioni <07akioni2@gmail.com> Date: Sun, 10 Oct 2021 00:01:49 +0800 Subject: [PATCH] refactor(cascader): clean codes --- CHANGELOG.en-US.md | 7 ++----- CHANGELOG.zh-CN.md | 7 ++----- src/cascader/demos/enUS/index.demo-entry.md | 2 +- src/cascader/demos/zhCN/index.demo-entry.md | 2 +- src/cascader/src/Cascader.tsx | 21 ++++++++------------- src/cascader/src/interface.ts | 10 ++++++---- src/cascader/src/utils.ts | 11 +++++++++++ 7 files changed, 31 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.en-US.md b/CHANGELOG.en-US.md index f3c7a456e..4734ff64f 100644 --- a/CHANGELOG.en-US.md +++ b/CHANGELOG.en-US.md @@ -2,17 +2,14 @@ ## Pending -### Feats - -- `n-layout` and `n-layout-sider` add `on-scroll` prop,closes [#1232](https://github.com/TuSimple/naive-ui/issues/1232). - ### Fixes - Fix `n-menu`'s incorrect warning on `default-expanded-keys`. ### Feats -- `n-cascader` provide all top Key values in `update:value` callback function,closes [#1235](https://github.com/TuSimple/naive-ui/issues/1235). +- `n-cascader` provide all options paths in `update:value` callback function, closes [#1235](https://github.com/TuSimple/naive-ui/issues/1235). +- `n-layout` and `n-layout-sider` add `on-scroll` prop,closes [#1232](https://github.com/TuSimple/naive-ui/issues/1232). ## 2.19.5 (2021-10-07) diff --git a/CHANGELOG.zh-CN.md b/CHANGELOG.zh-CN.md index 926d3e393..d08f8acee 100644 --- a/CHANGELOG.zh-CN.md +++ b/CHANGELOG.zh-CN.md @@ -2,17 +2,14 @@ ## Pending -### Feats - -- `n-layout` 和 `n-layout-sider` 增加 `on-scroll` 属性,关闭 [#1232](https://github.com/TuSimple/naive-ui/issues/1232) - ### Fixes - 修复 `n-menu` 对于 `default-expanded-keys` 的错误警报 ### Feats -- `n-cascader` 值改变时回调函数提供上层节点的全部 key 值,关闭 [#1235](https://github.com/TuSimple/naive-ui/issues/1235) +- `n-cascader` 值改变时回调函数提供上层节点的全部 options 值,关闭 [#1235](https://github.com/TuSimple/naive-ui/issues/1235) +- `n-layout` 和 `n-layout-sider` 增加 `on-scroll` 属性,关闭 [#1232](https://github.com/TuSimple/naive-ui/issues/1232) ## 2.19.5 (2021-10-07) diff --git a/src/cascader/demos/enUS/index.demo-entry.md b/src/cascader/demos/enUS/index.demo-entry.md index 3b514a782..5507ab9e4 100644 --- a/src/cascader/demos/enUS/index.demo-entry.md +++ b/src/cascader/demos/enUS/index.demo-entry.md @@ -47,7 +47,7 @@ custom-field | on-blur | `() => void` | `undefined` | Callback on blurred. | | on-focus | `() => void` | `undefined` | Callback on focused. | | on-load | `(option: CascaderOption) => Promise` | `undefined` | Callback when a node is loaded. Set `option.children` in the returned promise. Loading will stop after the promise is resolved or rejected. | -| on-update:value | `(value: string \| number \| Array \| null, option: CascaderOption \| Array \| null, pathValues: Array \| Array> \| null) => void` | `undefined` | Callback executed when the value changes. | +| on-update:value | `(value: string \| number \| Array \| null, option: CascaderOption \| Array \| null, pathValues: Array \| Array \| null) => void` | `undefined` | Callback executed when the value changes. | #### CascaderOption Properties diff --git a/src/cascader/demos/zhCN/index.demo-entry.md b/src/cascader/demos/zhCN/index.demo-entry.md index d3fee2417..9d5a7dbed 100644 --- a/src/cascader/demos/zhCN/index.demo-entry.md +++ b/src/cascader/demos/zhCN/index.demo-entry.md @@ -47,7 +47,7 @@ custom-field | on-blur | `() => void` | `undefined` | 用户 blur 时执行的回调 | | on-focus | `() => void` | `undefined` | 用户 focus 时执行的回调 | | on-load | `(option: CascaderOption) => Promise` | `undefined` | 在点击未加载完成节点时的回调,在返回的 promise 中设定 `option.children`,在返回的 promise resolve 或 reject 之后完成加载 | -| on-update:value | `(value: string \| number \| Array \| null, option: CascaderOption \| Array \| null, pathValues: Array \| Array> \| null) => void` | `undefined` | 值改变时执行的回调 | +| on-update:value | `(value: string \| number \| Array \| null, option: CascaderOption \| Array \| null, pathValues: Array \| Array \| null) => void` | `undefined` | 值改变时执行的回调 | #### CascaderOption Properties diff --git a/src/cascader/src/Cascader.tsx b/src/cascader/src/Cascader.tsx index 25901ce17..e9f75a70a 100644 --- a/src/cascader/src/Cascader.tsx +++ b/src/cascader/src/Cascader.tsx @@ -29,7 +29,7 @@ import { call, useAdjustedTo, warnOnce } from '../../_utils' import type { ExtractPublicPropTypes, MaybeArray } from '../../_utils' import { cascaderLight } from '../styles' import type { CascaderTheme } from '../styles' -import { getPathLabel } from './utils' +import { getPathLabel, getRawNodePath } from './utils' import CascaderMenu from './CascaderMenu' import CascaderSelectMenu from './CascaderSelectMenu' import { @@ -256,10 +256,7 @@ export default defineComponent({ function doUpdateValue ( value: Value | null, option: CascaderOption | null | Array, - optionPath: - | null - | Array - | Array> + optionPath: null | CascaderOption[] | Array ): void { const { onUpdateValue, @@ -302,9 +299,8 @@ export default defineComponent({ checkedKeys.map( (checkedKey) => getNode(checkedKey)?.rawNode || null ), - checkedKeys.map( - (checkedKey) => - getPath(checkedKey)?.treeNodePath?.map((v) => v.rawNode) || null + checkedKeys.map((checkedKey) => + getRawNodePath(getPath(checkedKey)?.treeNodePath) ) ) if (filterable) focusSelectionInput() @@ -329,7 +325,7 @@ export default defineComponent({ doUpdateValue( key, tmNode.rawNode, - getPath(key).treeNodePath.map((v) => v.rawNode) + getRawNodePath(getPath(key).treeNodePath) ) } else { return false @@ -339,7 +335,7 @@ export default defineComponent({ doUpdateValue( key, tmNode?.rawNode || null, - getPath(key)?.treeNodePath?.map((v) => v.rawNode) || null + getRawNodePath(getPath(key)?.treeNodePath) ) } } @@ -358,9 +354,8 @@ export default defineComponent({ doUpdateValue( checkedKeys, checkedKeys.map((checkedKey) => getNode(checkedKey)?.rawNode || null), - checkedKeys.map( - (checkedKey) => - getPath(checkedKey)?.treeNodePath?.map((v) => v.rawNode) || null + checkedKeys.map((checkedKey) => + getRawNodePath(getPath(checkedKey)?.treeNodePath) ) ) } diff --git a/src/cascader/src/interface.ts b/src/cascader/src/interface.ts index 9698cac23..6b6a74b37 100644 --- a/src/cascader/src/interface.ts +++ b/src/cascader/src/interface.ts @@ -40,15 +40,17 @@ export type OnUpdateValue = ( (string[] | null) & (number[] | null) & (Array | null), - option: CascaderOption & - null & + option: null & + CascaderOption & CascaderOption[] & - Array + Array, + path: null & CascaderOption[] & Array ) => void export type OnUpdateValueImpl = ( value: Value | null, - option: CascaderOption | null | Array + option: CascaderOption | null | Array, + path: Array | CascaderOption[] | null ) => void export type MenuModel = TmNode[][] diff --git a/src/cascader/src/utils.ts b/src/cascader/src/utils.ts index 5ca4800c7..b029a4649 100644 --- a/src/cascader/src/utils.ts +++ b/src/cascader/src/utils.ts @@ -1,6 +1,15 @@ import type { TmNode, CascaderOption } from './interface' import type { SelectBaseOption } from '../../select/src/interface' +function getRawNodePath (tmNodes: TmNode[]): CascaderOption[] +function getRawNodePath (tmNodes: TmNode[] | undefined): CascaderOption[] | null +function getRawNodePath ( + tmNodes: TmNode[] | undefined +): CascaderOption[] | null { + if (!tmNodes) return null + return tmNodes.map((tmNode) => tmNode.rawNode) +} + function traverseWithCallback ( options: T[], beforeCallback: (node: T) => void, @@ -17,6 +26,8 @@ function traverseWithCallback ( } } +export { getRawNodePath } + function createSelectOptions ( tmNodes: TmNode[], checkStrategyIsChild: boolean