refactor(cascader): clean codes

This commit is contained in:
07akioni 2021-10-10 00:01:49 +08:00
parent 94f69ed9ca
commit 9ed18b2f96
7 changed files with 31 additions and 29 deletions

View File

@ -2,17 +2,14 @@
## Pending
### Feats
- `n-layout` and `n-layout-sider` add `on-scroll` propcloses [#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 functioncloses [#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` propcloses [#1232](https://github.com/TuSimple/naive-ui/issues/1232).
## 2.19.5 (2021-10-07)

View File

@ -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)

View File

@ -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<void>` | `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<string \| number> \| null, option: CascaderOption \| Array<CascaderOption \| null> \| null, pathValues: Array<string \| number \| null> \| Array<Array<string \| number \| null>> \| null) => void` | `undefined` | Callback executed when the value changes. |
| on-update:value | `(value: string \| number \| Array<string \| number> \| null, option: CascaderOption \| Array<CascaderOption \| null> \| null, pathValues: Array<CascaderOption \| null> \| Array<CascaderOption[] \| null> \| null) => void` | `undefined` | Callback executed when the value changes. |
#### CascaderOption Properties

View File

@ -47,7 +47,7 @@ custom-field
| on-blur | `() => void` | `undefined` | 用户 blur 时执行的回调 |
| on-focus | `() => void` | `undefined` | 用户 focus 时执行的回调 |
| on-load | `(option: CascaderOption) => Promise<void>` | `undefined` | 在点击未加载完成节点时的回调,在返回的 promise 中设定 `option.children`,在返回的 promise resolve 或 reject 之后完成加载 |
| on-update:value | `(value: string \| number \| Array<string \| number> \| null, option: CascaderOption \| Array<CascaderOption \| null> \| null, pathValues: Array<string \| number \| null> \| Array<Array<string \| number \| null>> \| null) => void` | `undefined` | 值改变时执行的回调 |
| on-update:value | `(value: string \| number \| Array<string \| number> \| null, option: CascaderOption \| Array<CascaderOption \| null> \| null, pathValues: Array<CascaderOption \| null> \| Array<CascaderOption[] \| null> \| null) => void` | `undefined` | 值改变时执行的回调 |
#### CascaderOption Properties

View File

@ -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<CascaderOption | null>,
optionPath:
| null
| Array<CascaderOption | null>
| Array<Array<CascaderOption | null>>
optionPath: null | CascaderOption[] | Array<CascaderOption[] | null>
): 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)
)
)
}

View File

@ -40,15 +40,17 @@ export type OnUpdateValue = (
(string[] | null) &
(number[] | null) &
(Array<string | number> | null),
option: CascaderOption &
null &
option: null &
CascaderOption &
CascaderOption[] &
Array<CascaderOption | null>
Array<CascaderOption | null>,
path: null & CascaderOption[] & Array<CascaderOption[] | null>
) => void
export type OnUpdateValueImpl = (
value: Value | null,
option: CascaderOption | null | Array<CascaderOption | null>
option: CascaderOption | null | Array<CascaderOption | null>,
path: Array<CascaderOption[] | null> | CascaderOption[] | null
) => void
export type MenuModel = TmNode[][]

View File

@ -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<T extends { children?: T[] }> (
options: T[],
beforeCallback: (node: T) => void,
@ -17,6 +26,8 @@ function traverseWithCallback<T extends { children?: T[] }> (
}
}
export { getRawNodePath }
function createSelectOptions (
tmNodes: TmNode[],
checkStrategyIsChild: boolean