Skip to content

Commit

Permalink
Beginnings of Picker Table support (#1858)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Mar 19, 2024
1 parent 1ca0015 commit b9dc6a5
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/jsapi-components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './HookTestUtils';
export { default as TableInput } from './TableInput';
export * from './RefreshTokenBootstrap';
export * from './RefreshTokenUtils';
export * from './spectrum';
export * from './TableDropdown';
export { default as useBroadcastChannel } from './useBroadcastChannel';
export { default as useBroadcastLoginListener } from './useBroadcastLoginListener';
Expand Down
94 changes: 94 additions & 0 deletions packages/jsapi-components/src/spectrum/Picker.tsx
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;
1 change: 1 addition & 0 deletions packages/jsapi-components/src/spectrum/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Picker';

0 comments on commit b9dc6a5

Please sign in to comment.