Skip to content

Commit

Permalink
refactor: fieldName index change and tree-select use hook (#6248)
Browse files Browse the repository at this point in the history
* refactor: fieldName index change and tree-select use hook

* chore: type optional
  • Loading branch information
1587315093 authored Jul 14, 2023
1 parent 45b7bb6 commit 5c37f06
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/components/cascader-view/cascader-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type BaseOptionType = {
[key: string]: any
}
export type CascaderOption = {
label: string
value: string
label?: string
value?: string
disabled?: boolean
children?: CascaderOption[]
} & BaseOptionType
Expand Down Expand Up @@ -54,7 +54,7 @@ export const CascaderView: FC<CascaderViewProps> = p => {

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

Expand Down
15 changes: 9 additions & 6 deletions src/components/tree-select/tree-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ import { NativeProps, withNativeProps } from '../../utils/native-props'
import { getTreeDeep } from '../../utils/tree'
import { mergeProps } from '../../utils/with-default-props'
import { usePropsValue } from '../../utils/use-props-value'
import { useFieldNames } from '../../hooks'
import type { FieldNamesType } from '../../hooks'

const classPrefix = `adm-tree-select`

export interface TreeSelectOption {
export type TreeSelectOption = {
label?: string
value?: string
children?: TreeSelectOption[]
} & {
[key: string]: any
}

Expand All @@ -17,7 +23,7 @@ export type TreeSelectProps = {
defaultValue?: string[]
value?: string[]
onChange?: (value: string[], extend: { options: TreeSelectOption[] }) => void
fieldNames?: { label: string; value: string; children: string }
fieldNames?: FieldNamesType
} & NativeProps

const defaultProps = {
Expand All @@ -28,10 +34,7 @@ const defaultProps = {

export const TreeSelect: FC<TreeSelectProps> = p => {
const props = mergeProps(defaultProps, p)
const labelName = props.fieldNames.label || 'label'
const valueName = props.fieldNames.value || 'value'
const childrenName = props.fieldNames.children || 'children'

const [labelName, valueName, childrenName] = useFieldNames(props.fieldNames)
const [value, setValue] = usePropsValue({
value: props.value,
defaultValue: props.defaultValue,
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useFieldNames.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const useFieldNames = (fieldNames: FieldNamesType = {}) => {
children = 'children',
} = fieldNames

return [label, value, disabled, children] as string[]
return [label, value, children, disabled] as string[]
}, [JSON.stringify(fieldNames)])

return fields
Expand Down

0 comments on commit 5c37f06

Please sign in to comment.