Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(n-cascader): provide all top Key values in callback function #1298

Merged
merged 3 commits into from
Oct 9, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

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

## 2.19.5 (2021-10-07)

### Fixes
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- 修复 `n-menu` 对于 `default-expanded-keys` 的错误警报

### Feats

- `n-cascader` 值改变时回调函数提供上层节点的全部 key 值,关闭 [#1235](https://github.com/TuSimple/naive-ui/issues/1235)

## 2.19.5 (2021-10-07)

### Fixes
Expand Down
2 changes: 1 addition & 1 deletion src/cascader/demos/enUS/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) => 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<string \| number \| null> \| Array<Array<string \| number \| null>> \| null) => void` | `undefined` | Callback executed when the value changes. |

#### CascaderOption Properties

Expand Down
2 changes: 1 addition & 1 deletion src/cascader/demos/zhCN/index.demo-entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) => void` | `undefined` | 值改变时执行的回调 |
| 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` | 值改变时执行的回调 |

#### CascaderOption Properties

Expand Down
47 changes: 35 additions & 12 deletions src/cascader/src/Cascader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,27 @@ export default defineComponent({
}
function doUpdateValue (
value: Value | null,
option: CascaderOption | null | Array<CascaderOption | null>
option: CascaderOption | null | Array<CascaderOption | null>,
optionPath:
| null
| Array<CascaderOption | null>
| Array<Array<CascaderOption | null>>
): void {
const {
onUpdateValue,
'onUpdate:value': _onUpdateValue,
onChange
} = props
const { nTriggerFormInput, nTriggerFormChange } = formItem
if (onUpdateValue) call(onUpdateValue as OnUpdateValueImpl, value, option)
if (onUpdateValue) {
call(onUpdateValue as OnUpdateValueImpl, value, option, optionPath)
}
if (_onUpdateValue) {
call(_onUpdateValue as OnUpdateValueImpl, value, option)
call(_onUpdateValue as OnUpdateValueImpl, value, option, optionPath)
}
if (onChange) {
call(onChange as OnUpdateValueImpl, value, option, optionPath)
}
if (onChange) call(onChange as OnUpdateValueImpl, value, option)
uncontrolledValueRef.value = value
nTriggerFormInput()
nTriggerFormChange()
Expand All @@ -281,7 +289,7 @@ export default defineComponent({
function doCheck (key: Key): boolean {
const { cascade, multiple, filterable } = props
const {
value: { check, getNode }
value: { check, getNode, getPath }
} = treeMateRef
if (multiple) {
try {
Expand All @@ -293,6 +301,9 @@ export default defineComponent({
checkedKeys,
checkedKeys.map(
(checkedKey) => getNode(checkedKey)?.rawNode || null
),
checkedKeys.map((checkedKey) =>
getPath(checkedKey).treeNodePath.map((v) => v.rawNode)
)
)
if (filterable) focusSelectionInput()
Expand All @@ -313,14 +324,23 @@ export default defineComponent({
} else {
if (mergedCheckStrategyRef.value === 'child') {
const tmNode = getNode(key)
console.log('%%%%%%', getPath(key))
LYErin marked this conversation as resolved.
Show resolved Hide resolved
if (tmNode?.isLeaf) {
doUpdateValue(key, tmNode.rawNode)
doUpdateValue(
key,
tmNode.rawNode,
getPath(key).treeNodePath.map((v) => v.rawNode)
)
} else {
return false
}
} else {
const tmNode = getNode(key)
doUpdateValue(key, tmNode?.rawNode || null)
doUpdateValue(
key,
tmNode?.rawNode || null,
getPath(key).treeNodePath.map((v) => v.rawNode)
LYErin marked this conversation as resolved.
Show resolved Hide resolved
)
}
}
return true
Expand All @@ -329,15 +349,18 @@ export default defineComponent({
const { cascade, multiple } = props
if (multiple) {
const {
value: { uncheck, getNode }
value: { uncheck, getNode, getPath }
} = treeMateRef
const { checkedKeys } = uncheck(key, mergedKeysRef.value.checkedKeys, {
cascade,
checkStrategy: mergedCheckStrategyRef.value
})
doUpdateValue(
checkedKeys,
checkedKeys.map((checkedKey) => getNode(checkedKey)?.rawNode || null)
checkedKeys.map((checkedKey) => getNode(checkedKey)?.rawNode || null),
checkedKeys.map((checkedKey) =>
getPath(checkedKey).treeNodePath.map((v) => v.rawNode)
)
)
}
}
Expand Down Expand Up @@ -617,9 +640,9 @@ export default defineComponent({
function handleClear (e: MouseEvent): void {
e.stopPropagation()
if (props.multiple) {
doUpdateValue([], [])
doUpdateValue([], [], [])
} else {
doUpdateValue(null, null)
doUpdateValue(null, null, null)
}
}
function handleTriggerFocus (e: FocusEvent): void {
Expand Down Expand Up @@ -678,7 +701,7 @@ export default defineComponent({
if (multiple && Array.isArray(mergedValue)) {
doUncheck((option as any)[valueField])
} else {
doUpdateValue(null, null)
doUpdateValue(null, null, null)
}
}
function handleKeyDown (e: KeyboardEvent): void {
Expand Down