Skip to content

Commit

Permalink
ActionGroup wrapper (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed May 1, 2024
1 parent 538a2dd commit f28a15f
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 27 deletions.
39 changes: 39 additions & 0 deletions plugins/ui/src/js/src/elements/ActionGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {
ActionGroup as DHActionGroup,
ActionGroupProps as DHActionGroupProps,
} from '@deephaven/components';
import {
SerializedSelectionProps,
useSelectionProps,
} from './spectrum/useSelectionProps';

export type SerializedActionGroupProps<T> = Omit<
DHActionGroupProps<T>,
'selectionMode' | 'onChange' | 'onSelectionChange'
> &
SerializedSelectionProps;

function ActionGroup<T>({
selectionMode: selectionModeMaybeUppercase,
onChange: serializedOnChange,
onSelectionChange: serializedOnSelectionChange,
...props
}: SerializedActionGroupProps<T>): JSX.Element {
const { selectionMode, onChange, onSelectionChange } = useSelectionProps({
selectionMode: selectionModeMaybeUppercase,
onChange: serializedOnChange,
onSelectionChange: serializedOnSelectionChange,
});

return (
<DHActionGroup
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
selectionMode={selectionMode}
onChange={onChange}
onSelectionChange={onSelectionChange}
/>
);
}

export default ActionGroup;
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
import type { ItemKey, ItemSelection } from '@deephaven/components';
import { useCallback } from 'react';
import type { SelectionMode } from '@react-types/shared';
import type { ItemKey, ItemSelection } from '@deephaven/components';

export type SerializedSelection = 'all' | ItemKey[];

export type SerializedSelectionEventCallback = (
event: SerializedSelection
) => void;

export interface SerializedSelectionProps {
selectionMode?: SelectionMode | Uppercase<SelectionMode>;

/** Handler that is called when selection changes */
onChange?: SerializedSelectionEventCallback;

/**
* Handler that is called when the selection changes.
* @deprecated Use `onChange` instead
*/
onSelectionChange?: SerializedSelectionEventCallback;
}

export interface SelectionProps {
selectionMode?: SelectionMode;
onChange?: (selection: ItemSelection) => void;
onSelectionChange?: (selection: ItemSelection) => void;
}

/**
* Selection can be 'all' or a Set of keys. If it is a Set, serialize it as an
* array.
Expand Down Expand Up @@ -40,3 +60,22 @@ export function useSelectionEventCallback(
[callback]
);
}

export function useSelectionProps({
selectionMode,
onChange,
onSelectionChange,
}: SerializedSelectionProps): SelectionProps {
const selectionModeLc = selectionMode?.toLowerCase() as SelectionMode;

const serializedOnChange = useSelectionEventCallback(onChange);
const serializedOnSelectionChange =
useSelectionEventCallback(onSelectionChange);

return {
selectionMode: selectionModeLc,
onChange: onChange == null ? undefined : serializedOnChange,
onSelectionChange:
onSelectionChange == null ? undefined : serializedOnSelectionChange,
};
}
40 changes: 15 additions & 25 deletions plugins/ui/src/js/src/elements/useListViewProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { ListViewProps as DHListViewProps } from '@deephaven/components';
import { ListViewProps as DHListViewJSApiProps } from '@deephaven/jsapi-components';
import { ObjectViewProps } from './ObjectView';
import {
SerializedSelectionEventCallback,
useSelectionEventCallback,
} from './spectrum/useSelectionEventCallback';
SerializedSelectionProps,
useSelectionProps,
} from './spectrum/useSelectionProps';

type Density = Required<DHListViewProps>['density'];

Expand All @@ -25,22 +25,11 @@ type WrappedDHListViewProps = Omit<
selectionMode?: SelectionMode | Uppercase<SelectionMode>;
};

export interface SerializedListViewEventProps {
/** Handler that is called when selection changes */
onChange?: SerializedSelectionEventCallback;

/**
* Handler that is called when the selection changes.
* @deprecated Use `onChange` instead
*/
onSelectionChange?: SerializedSelectionEventCallback;
}

export type SerializedListViewProps = (
| WrappedDHListViewProps
| WrappedDHListViewJSApiProps
) &
SerializedListViewEventProps;
SerializedSelectionProps;

/**
* Wrap ListView props with the appropriate serialized event callbacks.
Expand All @@ -49,23 +38,24 @@ export type SerializedListViewProps = (
*/
export function useListViewProps({
density,
selectionMode,
onChange,
onSelectionChange,
selectionMode: selectionModeMaybeUppercase,
onChange: serializedOnChange,
onSelectionChange: serializedOnSelectionChange,
...otherProps
}: SerializedListViewProps): DHListViewProps | WrappedDHListViewJSApiProps {
const densityLc = density?.toLowerCase() as Density;
const selectionModeLc = selectionMode?.toLowerCase() as SelectionMode;

const serializedOnChange = useSelectionEventCallback(onChange);
const serializedOnSelectionChange =
useSelectionEventCallback(onSelectionChange);
const { selectionMode, onChange, onSelectionChange } = useSelectionProps({
selectionMode: selectionModeMaybeUppercase,
onChange: serializedOnChange,
onSelectionChange: serializedOnSelectionChange,
});

return {
density: densityLc,
selectionMode: selectionModeLc,
onChange: serializedOnChange,
onSelectionChange: serializedOnSelectionChange,
selectionMode,
onChange,
onSelectionChange,
// The @deephaven/components `ListView` has its own normalization logic that
// handles primitive children types (string, number, boolean). It also
// handles nested children inside of `Item` and `Section` components, so
Expand Down
2 changes: 1 addition & 1 deletion plugins/ui/src/js/src/widget/WidgetUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import React, { ComponentType } from 'react';
// Importing `Item` and `Section` compnents directly since they should not be
// wrapped due to how Spectrum collection components consume them.
import {
ActionGroup,
ActionMenu,
Item,
ListActionGroup,
Expand Down Expand Up @@ -34,6 +33,7 @@ import Column from '../layout/Column';
import Dashboard from '../layout/Dashboard';
import ListView from '../elements/ListView';
import Picker from '../elements/Picker';
import ActionGroup from '../elements/ActionGroup';

/*
* Map element node names to their corresponding React components
Expand Down

0 comments on commit f28a15f

Please sign in to comment.