Skip to content

Commit

Permalink
ListView table support (deephaven#1909)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Apr 2, 2024
1 parent b94c865 commit 8e96800
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/components/src/spectrum/listView/ListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import {
ListView as SpectrumListView,
SpectrumListViewProps,
} from '@adobe/react-spectrum';
import { EMPTY_FUNCTION } from '@deephaven/utils';
import {
extractSpectrumHTMLElement,
useOnScrollRef,
} from '@deephaven/react-hooks';
import cl from 'classnames';
import {
ItemElementOrPrimitive,
Expand Down Expand Up @@ -33,6 +38,9 @@ export type ListViewProps = {
*/
onChange?: (keys: 'all' | Set<ItemKey>) => void;

/** Handler that is called when the picker is scrolled. */
onScroll?: (event: Event) => void;

/**
* Handler that is called when the selection changes.
* @deprecated Use `onChange` instead
Expand All @@ -56,6 +64,7 @@ export function ListView({
disabledKeys,
UNSAFE_className,
onChange,
onScroll = EMPTY_FUNCTION,
onSelectionChange,
...spectrumListViewProps
}: ListViewProps): JSX.Element {
Expand Down Expand Up @@ -84,10 +93,13 @@ export function ListView({
onChange: onChange ?? onSelectionChange,
});

const scrollRef = useOnScrollRef(onScroll, extractSpectrumHTMLElement);

return (
<SpectrumListView
// eslint-disable-next-line react/jsx-props-no-spreading
{...spectrumListViewProps}
ref={scrollRef}
UNSAFE_className={cl('dh-list-view', UNSAFE_className)}
items={normalizedItems}
selectedKeys={selectedStringKeys}
Expand Down
1 change: 0 additions & 1 deletion packages/components/src/spectrum/picker/Picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ export function Picker({
<SpectrumPicker
// eslint-disable-next-line react/jsx-props-no-spreading
{...spectrumPickerProps}
// The `ref` prop type defined by React Spectrum is incorrect here
ref={scrollRef as unknown as DOMRef<HTMLDivElement>}
onOpenChange={onOpenChangeInternal}
UNSAFE_className={cl('dh-picker', UNSAFE_className)}
Expand Down
62 changes: 62 additions & 0 deletions packages/jsapi-components/src/spectrum/ListView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
ListView as ListViewBase,
ListViewProps as ListViewPropsBase,
NormalizedItemData,
} from '@deephaven/components';
import { dh as DhType } from '@deephaven/jsapi-types';
import { Settings } from '@deephaven/jsapi-utils';
import { LIST_VIEW_ROW_HEIGHT } from '@deephaven/utils';
import useFormatter from '../useFormatter';
import useViewportData from '../useViewportData';
import { useItemRowDeserializer } from './utils';

export interface ListViewProps extends Omit<ListViewPropsBase, 'children'> {
table: DhType.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;

// TODO #1890 : descriptionColumn, iconColumn

settings?: Settings;
}

export function ListView({
table,
keyColumn: keyColumnName,
labelColumn: labelColumnName,
settings,
...props
}: ListViewProps): JSX.Element {
const { getFormattedString: formatValue } = useFormatter(settings);

const deserializeRow = useItemRowDeserializer({
table,
keyColumnName,
labelColumnName,
formatValue,
});

const { viewportData, onScroll } = useViewportData<
NormalizedItemData,
DhType.Table
>({
reuseItemsOnTableResize: true,
table,
itemHeight: LIST_VIEW_ROW_HEIGHT,
deserializeRow,
});

return (
<ListViewBase
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
onScroll={onScroll}
>
{viewportData.items}
</ListViewBase>
);
}

export default ListView;
1 change: 1 addition & 0 deletions packages/jsapi-components/src/spectrum/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './ListView';
export * from './Picker';

0 comments on commit 8e96800

Please sign in to comment.