Skip to content

Commit

Permalink
feat: 支持配置字段别名的组件兼容原有字段名(XiaoMi#2965)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiamiao committed Aug 8, 2024
1 parent a90f1e9 commit d2f5396
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 16 deletions.
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

0 comments on commit d2f5396

Please sign in to comment.