Skip to content

Commit

Permalink
Support for null values in hooks (deephaven#2074)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Jun 18, 2024
1 parent 516eef0 commit d8ecd8e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useCallback, useMemo } from 'react';
import { NormalizedItemData } from '@deephaven/components';
import { dh } from '@deephaven/jsapi-types';
import { assertNotNull } from '@deephaven/utils';
import { getItemKeyColumn, getItemLabelColumn } from './itemUtils';

function defaultFormatKey(value: unknown): string | number | boolean {
Expand Down Expand Up @@ -37,38 +38,52 @@ export function useItemRowDeserializer({
labelColumnName,
formatValue = defaultFormatValue,
}: {
table: dh.Table;
table?: dh.Table | null;
descriptionColumnName?: string;
iconColumnName?: string;
keyColumnName?: string;
labelColumnName?: string;
formatValue?: (value: unknown, columnType: string) => string;
}): (row: dh.Row) => NormalizedItemData {
const keyColumn = useMemo(
() => getItemKeyColumn(table, keyColumnName),
() => (table == null ? null : getItemKeyColumn(table, keyColumnName)),
[keyColumnName, table]
);

const labelColumn = useMemo(
() => getItemLabelColumn(table, keyColumn, labelColumnName),
() =>
table == null || keyColumn == null
? null
: getItemLabelColumn(table, keyColumn, labelColumnName),
[keyColumn, labelColumnName, table]
);

const descriptionColumn = useMemo(
() =>
descriptionColumnName == null
table == null || descriptionColumnName == null
? null
: table.findColumn(descriptionColumnName),
[descriptionColumnName, table]
);

const iconColumn = useMemo(
() => (iconColumnName == null ? null : table.findColumn(iconColumnName)),
() =>
table == null || iconColumnName == null
? null
: table.findColumn(iconColumnName),
[iconColumnName, table]
);

const deserializeRow = useCallback(
(row: dh.Row): NormalizedItemData => {
// `deserializeRow` can be created on a null `table` which results in null
// `keyColumn` + `labelColumn`, but it should never actually be called.
// The assumption is that the `table` will eventually be non-null,
// `deserializeRow` will be recreated, and then applied to the non-null
// table.
assertNotNull(keyColumn, 'keyColumn cannot be null.');
assertNotNull(labelColumn, 'labelColumn cannot be null.');

const key = defaultFormatKey(row.get(keyColumn));
const content = formatValue(row.get(labelColumn), labelColumn.type);

Expand Down
4 changes: 2 additions & 2 deletions packages/jsapi-components/src/useGetItemIndexByValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ export function useGetItemIndexByValue<TValue>({
value,
table,
}: {
columnName: string;
columnName: string | null;
table: dh.Table | null;
value: TValue | null | undefined;
}): () => Promise<number | null> {
const tableUtils = useTableUtils();

return useCallback(async () => {
if (table == null || value == null) {
if (table == null || value == null || columnName == null) {
return null;
}

Expand Down

0 comments on commit d8ecd8e

Please sign in to comment.