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: onChange in cascader-view unable to get value #6444

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@
(val: CascaderValue[]) => {
const children = val.reduce((currentOptions, v) => {
return (
currentOptions.find(option => option.value === v)?.children || []
currentOptions.find(option => option[valueName] === v)?.[
childrenName
] || []

Check warning on line 41 in src/components/cascader-view/use-cascader-value-extend.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/cascader-view/use-cascader-value-extend.tsx#L41

Added line #L41 was not covered by tests
)
}, 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
Loading