Skip to content

Commit

Permalink
fix(antd/next): fix Cascader in form readPretty mode display error #3250
Browse files Browse the repository at this point in the history
 (#3253)

* fix(antd): fix Cascader in form readPretty mode display error

* fix(next): fix Cascader in form readPretty mode display error
  • Loading branch information
fengwuxp authored Jul 6, 2022
1 parent 679fbb7 commit d571910
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 13 deletions.
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)
const labels = values
.map((val: Array<DefaultOptionType>) => {
return val.map((item) => item.label).join('/')
})
.join(' ')
return labels || placeholder
}
return (
Expand Down
44 changes: 39 additions & 5 deletions packages/next/src/preview-text/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ const TreeSelect: React.FC<React.PropsWithChildren<TreeSelectProps>> = observer(
}
)

type SelectOptionType = {
value: string
label: string
children?: SelectOptionType[]
}

const Cascader: React.FC<React.PropsWithChildren<CascaderProps>> = observer(
(props) => {
const field = useField<Field>()
Expand All @@ -218,15 +224,43 @@ const Cascader: React.FC<React.PropsWithChildren<CascaderProps>> = observer(
: props?.dataSource?.length
? props.dataSource
: []
const findSelectedItem = (
items: SelectOptionType[],
val: string | number
) => {
return items.find((item) => item.value == val)
}
const findSelectedItems = (
sources: SelectOptionType[],
selectedValues: Array<string[] | number[]>
): Array<SelectOptionType[]> => {
return selectedValues.map((value) => {
const result: Array<SelectOptionType> = []
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 = () => {
return props.multiple ? toArr(props.value) : [props.value]
const val = toArr(props.value)
// 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)
const labels = values
.map((val: Array<{ value: string; label: string }>) => {
return val.map((item) => item.label).join('/')
})
.join(' ')
return labels || placeholder
}
return <div className={cls(prefixCls, props.className)}>{getLabels()}</div>
Expand Down

0 comments on commit d571910

Please sign in to comment.