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

fix(antd): fix Cascader in form readPretty mode display error #3250 #3253

Merged
merged 2 commits into from
Jul 6, 2022
Merged
Changes from 1 commit
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
41 changes: 33 additions & 8 deletions packages/antd/src/preview-text/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { InputProps } from 'antd/lib/input'
import { InputNumberProps } from 'antd/lib/input-number'
import { SelectProps } from 'antd/lib/select'
import { TreeSelectProps } from 'antd/lib/tree-select'
import { CascaderProps } from 'antd/lib/cascader'
import { CascaderProps, DefaultOptionType } from 'antd/lib/cascader'
import {
DatePickerProps,
RangePickerProps as DateRangePickerProps,
Expand Down Expand Up @@ -239,18 +239,43 @@ const Cascader: React.FC<React.PropsWithChildren<CascaderProps<any>>> =
: props?.options?.length
? props.options
: []
const findSelectedItem = (
items: DefaultOptionType[],
val: string | number
) => {
return items.find((item) => item.value == val)
}
const findSelectedItems = (
sources: DefaultOptionType[],
selectedValues: Array<string[] | number[]>
): Array<any[]> => {
return selectedValues.map((value) => {
const result: Array<DefaultOptionType> = []
let items = sources
value.forEach((val) => {
const selectedItem = findSelectedItem(items, val)
result.push({
label: selectedItem?.label ?? '',
value: selectedItem?.value,
})
items = selectedItem?.children ?? []
})
return result
})
}
const getSelected = () => {
const val = toArr(props.value)
return props.multiple
? val.map((item) => item[item.length - 1])
: val.slice(val.length - 1)
// unified conversion to multi selection mode
return props.multiple ? val : [val]
}
const getLabels = () => {
const selected = getSelected()
const labels = getValueByValue(dataSource, selected)
?.filter((item) => isValid(item))
?.map((item) => item?.whole?.join('/'))
.join(', ')
const values = findSelectedItems(dataSource, selected)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为了避免修改 getValueByValue 影响其他组件,重新写了查找选中值的逻辑。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fusion组件要不要也改一下?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,晚上处理一下

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fusion Cascader readPretty 已修复

测试发现一个新问题:
fusion 的级联选择组件普通模式回填表单值也有问题,参见: demo
回填查找逻辑好像是使用数组的最后一项,要是级联选择最后一个选项有重复(相对上一个选项)的话会就会有问题(参见例子的租户环境级联选中)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那这个是fusion的问题吧,先忽略

const labels = values
.map((val: Array<DefaultOptionType>) => {
return val.map((item) => item.label).join('/')
})
.join(' ')
return labels || placeholder
}
return (
Expand Down