-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Beginnings of Picker Table support (#1858)
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { | ||
NormalizedPickerItemData, | ||
Picker as PickerBase, | ||
PickerProps as PickerPropsBase, | ||
} from '@deephaven/components'; | ||
import { dh } from '@deephaven/jsapi-types'; | ||
import { useCallback, useMemo } from 'react'; | ||
import { useViewportData } from '../useViewportData'; | ||
|
||
function formatValue(value: unknown): string | number | boolean { | ||
if ( | ||
typeof value === 'string' || | ||
typeof value === 'number' || | ||
typeof value === 'boolean' | ||
) { | ||
return value; | ||
} | ||
|
||
// TODO: Add support for formatting date values | ||
return String(value); | ||
} | ||
|
||
function usePickerItemRowDeserializer({ | ||
table, | ||
keyColumnName, | ||
labelColumnName, | ||
}: { | ||
table: dh.Table; | ||
keyColumnName?: string; | ||
labelColumnName?: string; | ||
}) { | ||
const keyColumn = useMemo( | ||
() => | ||
table.findColumn( | ||
keyColumnName == null ? table.columns[0].name : keyColumnName | ||
), | ||
[keyColumnName, table] | ||
); | ||
|
||
const labelColumn = useMemo( | ||
() => | ||
labelColumnName == null ? keyColumn : table.findColumn(labelColumnName), | ||
[keyColumn, labelColumnName, table] | ||
); | ||
|
||
const deserializeRow = useCallback( | ||
(row: dh.Row): NormalizedPickerItemData => { | ||
const key = formatValue(row.get(keyColumn)); | ||
const content = formatValue(row.get(labelColumn)); | ||
|
||
return { | ||
key, | ||
content, | ||
}; | ||
}, | ||
[keyColumn, labelColumn] | ||
); | ||
|
||
return deserializeRow; | ||
} | ||
|
||
export interface PickerProps extends PickerPropsBase { | ||
table: dh.Table; | ||
/* The column of values to use as item keys. Defaults to the first column. */ | ||
keyColumn?: string; | ||
/* The column of values to display as primary text. Defaults to the `keyColumn` value. */ | ||
labelColumn?: string; | ||
} | ||
|
||
export function Picker({ | ||
table, | ||
keyColumn: keyColumnName, | ||
labelColumn: labelColumnName, | ||
...props | ||
}: PickerProps): JSX.Element { | ||
const deserializeRow = usePickerItemRowDeserializer({ | ||
table, | ||
keyColumnName, | ||
labelColumnName, | ||
}); | ||
|
||
const { viewportData } = useViewportData<NormalizedPickerItemData, dh.Table>({ | ||
reuseItemsOnTableResize: true, | ||
table, | ||
deserializeRow, | ||
}); | ||
|
||
return ( | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
<PickerBase {...props}>{viewportData.items}</PickerBase> | ||
); | ||
} | ||
|
||
export default Picker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Picker'; |