Skip to content

Commit

Permalink
fix: onChange in cascader-view unable to get value (#6444)
Browse files Browse the repository at this point in the history
* fix: onChange in cascader-view unable to get value

* fix: cascader update

* feat: update test
  • Loading branch information
1587315093 authored Nov 23, 2023
1 parent abef05c commit 61c9d27
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
5 changes: 4 additions & 1 deletion src/components/cascader-view/cascader-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ export const CascaderView: FC<CascaderViewProps> = p => {
const props = mergeProps(defaultProps, p)

const { locale } = useConfig()
const generateValueExtend = useCascaderValueExtend(props.options)
const [labelName, valueName, childrenName, disabledName] = useFieldNames(
props.fieldNames
)
const generateValueExtend = useCascaderValueExtend(props.options, {
valueName,
childrenName,
})

const [value, setValue] = usePropsValue({
...props,
Expand Down
8 changes: 7 additions & 1 deletion src/components/cascader-view/tests/cascader-view.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ describe('CascaderView', () => {

expect(onChange).toBeCalledTimes(3)
expect(onTabsChange).toBeCalledTimes(2)
expect(onChange.mock.calls[2][0]).toEqual(['浙江', '杭州', '西湖区'])

const [value, extend] = onChange.mock.calls[2]
const item = extend.items[0]

expect(value).toEqual(['浙江', '杭州', '西湖区'])
expect(item).toHaveProperty('labelT')
expect(item).toHaveProperty('valueT')
})
test('controlled mode', async () => {
const { container } = render(
Expand Down
25 changes: 15 additions & 10 deletions src/components/cascader-view/use-cascader-value-extend.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import { useMemo } from 'react'
import memoize from 'lodash/memoize'
import {
CascaderValue,
CascaderValueExtend,
CascaderOption,
} from './cascader-view'
import { CascaderValue, CascaderValueExtend } from './cascader-view'
import type { CascaderOption } from './cascader-view'

export function useCascaderValueExtend(
options: CascaderOption[],
fieldNames: CascaderOption
) {
const { valueName, childrenName } = fieldNames

export function useCascaderValueExtend(options: CascaderOption[]) {
const generateItems = useMemo(() => {
return memoize(
(val: CascaderValue[]) => {
const ret: CascaderOption[] = []
let currentOptions = options

for (const v of val) {
const target = currentOptions.find(option => option.value === v)
const target = currentOptions.find(option => option[valueName] === v)
if (!target) {
break
}
ret.push(target)
if (!target.children) break
currentOptions = target.children
if (!target[childrenName]) break
currentOptions = target[childrenName]
}

return ret
Expand All @@ -33,7 +36,9 @@ export function useCascaderValueExtend(options: CascaderOption[]) {
(val: CascaderValue[]) => {
const children = val.reduce((currentOptions, v) => {
return (
currentOptions.find(option => option.value === v)?.children || []
currentOptions.find(option => option[valueName] === v)?.[
childrenName
] || []
)
}, options)

Expand Down
7 changes: 6 additions & 1 deletion src/components/cascader/cascader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { usePropsValue } from '../../utils/use-props-value'
import CascaderView from '../cascader-view'
import { useConfig } from '../config-provider'
import { useCascaderValueExtend } from '../cascader-view/use-cascader-value-extend'
import { useFieldNames } from '../../hooks'
import type { FieldNamesType } from '../../hooks'

const classPrefix = `adm-cascader`
Expand Down Expand Up @@ -110,7 +111,11 @@ export const Cascader = forwardRef<CascaderRef, CascaderProps>((p, ref) => {
},
})

const generateValueExtend = useCascaderValueExtend(props.options)
const [, valueName, childrenName] = useFieldNames(props.fieldNames)
const generateValueExtend = useCascaderValueExtend(props.options, {
valueName,
childrenName,
})

const [innerValue, setInnerValue] = useState<CascaderValue[]>(value)

Expand Down

0 comments on commit 61c9d27

Please sign in to comment.