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

feat: 支持配置字段别名的组件兼容原有字段名(#2965) #2967

Closed
Closed
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 packages/ui/breadcrumb/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export const transformData = (
*/
const getKeyFields = (node: BreadcrumbDataItem, key: HiBaseFieldNameKeys) => {
if (fieldNames) {
return node[(fieldNames[key] || key) as keyof BreadcrumbDataItem]
return (
node[(fieldNames[key] || key) as keyof BreadcrumbDataItem] ??
node[key as keyof BreadcrumbDataItem]
)
}
return node[key as keyof BreadcrumbDataItem]
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/cascader/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const flattenTreeData = (treeData: CascaderDataItem[], fieldNames?: HiBas
*/
const getKeyFields = (node: any, key: any) => {
if (fieldNames) {
return node[(fieldNames as any)[key] || key]
return node[(fieldNames as any)[key] || key] ?? node[key]
}
return node[key]
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/check-cascader/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const flattenTreeData = (
*/
const getKeyFields = (node: any, key: any) => {
if (fieldNames) {
return node[(fieldNames as any)[key] || key]
return node[(fieldNames as any)[key] || key] ?? node[key]
}
return node[key]
}
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/check-select/src/hooks/use-flatten-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const useFlattenData = ({ data = NOOP_ARRAY, fieldNames = DEFAULT_FIELD_N

export const flattenData = ({ data = NOOP_ARRAY, fieldNames = DEFAULT_FIELD_NAMES }: any) => {
// 转换对象
const getKeyFields = (node: any, key: HiBaseFieldNameKeys) => node[fieldNames[key] || key]
const getKeyFields = (node: any, key: HiBaseFieldNameKeys) =>
node[fieldNames[key] || key] ?? node[key]

return baseFlattenTree({
tree: data,
Expand Down
5 changes: 2 additions & 3 deletions packages/ui/check-tree-select/src/CheckTreeSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export const CheckTreeSelect = forwardRef<HTMLDivElement | null, CheckTreeSelect
*/
const getKeyFields = useCallback(
(node: any, key: string) => {
return node[fieldNames[key] || key]
return node[fieldNames[key] || key] ?? node[key]
},
[fieldNames]
)
Expand All @@ -132,12 +132,11 @@ export const CheckTreeSelect = forwardRef<HTMLDivElement | null, CheckTreeSelect
flattedNode.title = getKeyFields(raw, 'title')
flattedNode.disabled = getKeyFields(raw, 'disabled') ?? false
flattedNode.isLeaf = getKeyFields(raw, 'isLeaf') ?? false

return flattedNode
},
}) as FlattedCheckTreeSelectDataItem[]
}, [data, getKeyFields])

console.log('flattenedData', flattedData)
// TODO: 抽离展开hook
// TODO: onLoadChildren 和 defaultExpandAll 共存时
const [expandedIds, tryChangeExpandedIds] = useUncontrolledState(
Expand Down
5 changes: 4 additions & 1 deletion packages/ui/descriptions/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export const transformData = (data: DescriptionsItemProps[], fieldNames?: HiBase
*/
const getKeyFields = (node: DescriptionsItemProps, key: HiBaseFieldNameKeys) => {
if (fieldNames) {
return node[(fieldNames[key] || key) as keyof DescriptionsItemProps]
return (
node[(fieldNames[key] || key) as keyof DescriptionsItemProps] ??
node[key as keyof DescriptionsItemProps]
)
}
return node[key as keyof DescriptionsItemProps]
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/filter/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const transformTreeData = (
): FilterDataItem[] => {
const getKeyFields = (node: FilterDataItem, key: HiBaseFieldNameKeys) => {
if (fieldNames) {
return node[(fieldNames[key] || key) as keyof FilterDataItem]
return node[(fieldNames[key] || key) as keyof FilterDataItem] ?? node[key]
}
return node[key]
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/list/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const transformData = (
): ListDataItem[] => {
const getKeyFields = (node: ListDataItem, key: HiBaseFieldNameKeys) => {
if (fieldNames) {
return node[(fieldNames[key] || key) as keyof ListDataItem]
return node[(fieldNames[key] || key) as keyof ListDataItem] ?? node[key as keyof ListDataItem]
}
return node[key as keyof ListDataItem]
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/menu/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const transformTreeData = (data: MenuDataItem[], fieldNames?: HiBaseField
*/
const getKeyFields = (node: MenuDataItem, key: HiBaseFieldNameKeys) => {
if (fieldNames) {
return node[(fieldNames[key] || key) as keyof MenuDataItem]
return node[(fieldNames[key] || key) as keyof MenuDataItem] ?? node[key]
}
return node[key]
}
Expand Down
4 changes: 3 additions & 1 deletion packages/ui/search/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export const transformData = (
*/
const getKeyFields = (node: SearchDataItem, key: HiBaseFieldNameKeys) => {
if (fieldNames) {
return node[(fieldNames[key] || key) as keyof SearchDataItem]
return (
node[(fieldNames[key] || key) as keyof SearchDataItem] ?? node[key as keyof SearchDataItem]
)
}
return node[key as keyof SearchDataItem]
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/select/src/hooks/use-flatten-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const DEFAULT_FIELD_NAMES = {} as any

export const useFlattenData = ({ data = NOOP_ARRAY, fieldNames = DEFAULT_FIELD_NAMES }: any) => {
const flattedData = useMemo(() => {
const getKeyFields = (node: any, key: string) => node[fieldNames[key] || key]
const getKeyFields = (node: any, key: string) => node[fieldNames[key] || key] ?? node[key]

return baseFlattenTree({
tree: data,
Expand Down
5 changes: 4 additions & 1 deletion packages/ui/stepper/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export const transformData = (
): StepperDataItem[] => {
const getKeyFields = (node: StepperDataItem, key: HiBaseFieldNameKeys) => {
if (fieldNames) {
return node[(fieldNames[key] || key) as keyof StepperDataItem]
return (
node[(fieldNames[key] || key) as keyof StepperDataItem] ??
node[key as keyof StepperDataItem]
)
}
return node[key as keyof StepperDataItem]
}
Expand Down
5 changes: 4 additions & 1 deletion packages/ui/transfer/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export const transformData = (
): TransferDataItem[] => {
const getKeyFields = (node: TransferDataItem, key: HiBaseFieldNameKeys) => {
if (fieldNames) {
return node[(fieldNames[key] || key) as keyof TransferDataItem]
return (
node[(fieldNames[key] || key) as keyof TransferDataItem] ??
node[key as keyof TransferDataItem]
)
}
return node[key as keyof TransferDataItem]
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/tree-select/src/TreeSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const TreeSelect = forwardRef<HTMLDivElement | null, TreeSelectProps>(
*/
const getKeyFields = useCallback(
(node: any, key: string) => {
return node[fieldNames[key] || key]
return node[fieldNames[key] || key] ?? node[key]
},
[fieldNames]
)
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/tree/src/utils/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const flattenTreeData = (treeData: TreeDataItem[], fieldNames: any = EMPT
* 转换对象
*/
const getKeyFields = (node: any, key: string) => {
return node[fieldNames[key] || key]
return node[fieldNames[key] || key] ?? node[key]
}

return baseFlattenTree({
Expand Down