Skip to content

Commit 0a0d7bc

Browse files
committed
Wired up search (#2074)
1 parent e5135b8 commit 0a0d7bc

3 files changed

Lines changed: 61 additions & 24 deletions

File tree

packages/jsapi-components/src/spectrum/ComboBox.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
NormalizedItem,
44
SpectrumComboBoxProps,
55
} from '@deephaven/components';
6+
import { useCallback } from 'react';
67
import { PickerWithTableProps } from './PickerProps';
78
import { usePickerProps } from './utils';
89

@@ -11,11 +12,25 @@ export type ComboBoxProps = PickerWithTableProps<
1112
>;
1213

1314
export function ComboBox(props: ComboBoxProps): JSX.Element {
14-
const pickerProps = usePickerProps(props);
15+
const {
16+
onInputChange: onInputChangeInternal,
17+
onSearchTextChange,
18+
...pickerProps
19+
} = usePickerProps<ComboBoxProps>(props);
20+
21+
const onInputChange = useCallback(
22+
(value: string) => {
23+
onInputChangeInternal?.(value);
24+
onSearchTextChange(value);
25+
},
26+
[onInputChangeInternal, onSearchTextChange]
27+
);
28+
1529
return (
1630
<ComboBoxNormalized
1731
// eslint-disable-next-line react/jsx-props-no-spreading
1832
{...pickerProps}
33+
onInputChange={onInputChange}
1934
/>
2035
);
2136
}

packages/jsapi-components/src/spectrum/Picker.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { PickerProps } from './PickerProps';
33
import { usePickerProps } from './utils';
44

55
export function Picker(props: PickerProps): JSX.Element {
6-
const pickerProps = usePickerProps(props);
6+
const pickerProps = usePickerProps<PickerProps>(props);
77

88
return (
99
<PickerNormalized

packages/jsapi-components/src/spectrum/utils/usePickerProps.ts

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@ import { useCallback, useEffect, useMemo, useState } from 'react';
22
import {
33
ItemKey,
44
NormalizedItem,
5-
NormalizedItemData,
65
NormalizedSection,
7-
NormalizedSectionData,
86
usePickerItemScale,
97
} from '@deephaven/components';
10-
import { dh as DhType } from '@deephaven/jsapi-types';
8+
import { TableUtils } from '@deephaven/jsapi-utils';
119
import Log from '@deephaven/log';
10+
import { usePromiseFactory } from '@deephaven/react-hooks';
1211
import { PICKER_TOP_OFFSET } from '@deephaven/utils';
1312
import useFormatter from '../../useFormatter';
1413
import type { PickerWithTableProps } from '../PickerProps';
15-
import { getItemKeyColumn } from './itemUtils';
16-
import useItemRowDeserializer from './useItemRowDeserializer';
17-
import useGetItemIndexByValue from '../../useGetItemIndexByValue';
18-
import useViewportData from '../../useViewportData';
14+
import { getItemKeyColumn, getItemLabelColumn } from './itemUtils';
15+
import { useItemRowDeserializer } from './useItemRowDeserializer';
16+
import { useGetItemIndexByValue } from '../../useGetItemIndexByValue';
17+
import useSearchableViewportData from '../../useSearchableViewportData';
1918

2019
const log = Log.module('jsapi-components.usePickerProps');
2120

@@ -26,6 +25,7 @@ export type UsePickerDerivedProps = {
2625
getInitialScrollPosition: () => Promise<number | null>;
2726
onChange: (key: ItemKey | null) => void;
2827
onScroll: (event: Event) => void;
28+
onSearchTextChange: (searchText: string) => void;
2929
};
3030

3131
/**
@@ -49,7 +49,7 @@ export type UsePickerProps<TProps> = UsePickerDerivedProps &
4949
UsePickerPassthroughProps<TProps>;
5050

5151
export function usePickerProps<TProps>({
52-
table,
52+
table: tableSource,
5353
keyColumn: keyColumnName,
5454
labelColumn: labelColumnName,
5555
iconColumn: iconColumnName,
@@ -69,22 +69,44 @@ export function usePickerProps<TProps>({
6969
ItemKey | null | undefined
7070
>(props.defaultSelectedKey);
7171

72+
// Copy table so we can apply filters without affecting the original table.
73+
// (Note that this call is not actually applying any filters. Filter will be
74+
// applied in `useSearchableViewportData`.)
75+
const { data: tableCopy } = usePromiseFactory(
76+
TableUtils.copyTableAndApplyFilters,
77+
[tableSource]
78+
);
79+
7280
const keyColumn = useMemo(
73-
() => getItemKeyColumn(table, keyColumnName),
74-
[keyColumnName, table]
81+
() =>
82+
tableCopy == null ? null : getItemKeyColumn(tableCopy, keyColumnName),
83+
[keyColumnName, tableCopy]
84+
);
85+
86+
const labelColumn = useMemo(
87+
() =>
88+
tableCopy == null || keyColumn == null
89+
? null
90+
: getItemLabelColumn(tableCopy, keyColumn, labelColumnName),
91+
[keyColumn, labelColumnName, tableCopy]
92+
);
93+
94+
const searchColumnNames = useMemo(
95+
() => (labelColumn == null ? [] : [labelColumn.name]),
96+
[labelColumn]
7597
);
7698

7799
const deserializeRow = useItemRowDeserializer({
78-
table,
100+
table: tableCopy,
79101
iconColumnName,
80102
keyColumnName,
81103
labelColumnName,
82104
formatValue,
83105
});
84106

85107
const getItemIndexByValue = useGetItemIndexByValue({
86-
table,
87-
columnName: keyColumn.name,
108+
table: tableCopy,
109+
columnName: keyColumn?.name ?? null,
88110
value: isUncontrolled ? uncontrolledSelectedKey : props.selectedKey,
89111
});
90112

@@ -98,15 +120,14 @@ export function usePickerProps<TProps>({
98120
return index * itemHeight + PICKER_TOP_OFFSET;
99121
}, [getItemIndexByValue, itemHeight]);
100122

101-
const { viewportData, onScroll, setViewport } = useViewportData<
102-
NormalizedItemData | NormalizedSectionData,
103-
DhType.Table
104-
>({
105-
reuseItemsOnTableResize: true,
106-
table,
107-
itemHeight,
108-
deserializeRow,
109-
});
123+
const { onScroll, onSearchTextChange, setViewport, viewportData } =
124+
useSearchableViewportData({
125+
reuseItemsOnTableResize: true,
126+
table: tableCopy,
127+
itemHeight,
128+
deserializeRow,
129+
searchColumnNames,
130+
});
110131

111132
const normalizedItems = viewportData.items as (
112133
| NormalizedItem
@@ -158,6 +179,7 @@ export function usePickerProps<TProps>({
158179
getInitialScrollPosition,
159180
onChange: onSelectionChangeInternal,
160181
onScroll,
182+
onSearchTextChange,
161183
};
162184
}
163185

0 commit comments

Comments
 (0)