Skip to content

Commit 9c9e317

Browse files
committed
ActionGroup wrapper (#445)
1 parent 258bef0 commit 9c9e317

4 files changed

Lines changed: 95 additions & 28 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {
2+
ActionGroup as DHActionGroup,
3+
ActionGroupProps as DHActionGroupProps,
4+
} from '@deephaven/components';
5+
import {
6+
SerializedSelectionProps,
7+
useSelectionProps,
8+
} from './spectrum/useSelectionProps';
9+
10+
export type SerializedActionGroupProps<T> = Omit<
11+
DHActionGroupProps<T>,
12+
'selectionMode' | 'onChange' | 'onSelectionChange'
13+
> &
14+
SerializedSelectionProps;
15+
16+
function ActionGroup<T>({
17+
selectionMode: selectionModeMaybeUppercase,
18+
onChange: serializedOnChange,
19+
onSelectionChange: serializedOnSelectionChange,
20+
...props
21+
}: SerializedActionGroupProps<T>): JSX.Element {
22+
const { selectionMode, onChange, onSelectionChange } = useSelectionProps({
23+
selectionMode: selectionModeMaybeUppercase,
24+
onChange: serializedOnChange,
25+
onSelectionChange: serializedOnSelectionChange,
26+
});
27+
28+
return (
29+
<DHActionGroup
30+
// eslint-disable-next-line react/jsx-props-no-spreading
31+
{...props}
32+
selectionMode={selectionMode}
33+
onChange={onChange}
34+
onSelectionChange={onSelectionChange}
35+
/>
36+
);
37+
}
38+
39+
export default ActionGroup;

plugins/ui/src/js/src/elements/spectrum/useSelectionEventCallback.ts renamed to plugins/ui/src/js/src/elements/spectrum/useSelectionProps.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
1-
import type { ItemKey, ItemSelection } from '@deephaven/components';
21
import { useCallback } from 'react';
2+
import type { SelectionMode } from '@react-types/shared';
3+
import type { ItemKey, ItemSelection } from '@deephaven/components';
34

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

67
export type SerializedSelectionEventCallback = (
78
event: SerializedSelection
89
) => void;
910

11+
export interface SerializedSelectionProps {
12+
selectionMode?: SelectionMode | Uppercase<SelectionMode>;
13+
14+
/** Handler that is called when selection changes */
15+
onChange?: SerializedSelectionEventCallback;
16+
17+
/**
18+
* Handler that is called when the selection changes.
19+
* @deprecated Use `onChange` instead
20+
*/
21+
onSelectionChange?: SerializedSelectionEventCallback;
22+
}
23+
24+
export interface SelectionProps {
25+
selectionMode?: SelectionMode;
26+
onChange?: (selection: ItemSelection) => void;
27+
onSelectionChange?: (selection: ItemSelection) => void;
28+
}
29+
1030
/**
1131
* Selection can be 'all' or a Set of keys. If it is a Set, serialize it as an
1232
* array.
@@ -40,3 +60,22 @@ export function useSelectionEventCallback(
4060
[callback]
4161
);
4262
}
63+
64+
export function useSelectionProps({
65+
selectionMode,
66+
onChange,
67+
onSelectionChange,
68+
}: SerializedSelectionProps): SelectionProps {
69+
const selectionModeLc = selectionMode?.toLowerCase() as SelectionMode;
70+
71+
const serializedOnChange = useSelectionEventCallback(onChange);
72+
const serializedOnSelectionChange =
73+
useSelectionEventCallback(onSelectionChange);
74+
75+
return {
76+
selectionMode: selectionModeLc,
77+
onChange: onChange == null ? undefined : serializedOnChange,
78+
onSelectionChange:
79+
onSelectionChange == null ? undefined : serializedOnSelectionChange,
80+
};
81+
}

plugins/ui/src/js/src/elements/useListViewProps.ts

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { ListViewProps as DHListViewProps } from '@deephaven/components';
44
import { ListViewProps as DHListViewJSApiProps } from '@deephaven/jsapi-components';
55
import { ObjectViewProps } from './ObjectView';
66
import {
7-
SerializedSelectionEventCallback,
8-
useSelectionEventCallback,
9-
} from './spectrum/useSelectionEventCallback';
7+
SerializedSelectionProps,
8+
useSelectionProps,
9+
} from './spectrum/useSelectionProps';
1010

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

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

28-
export interface SerializedListViewEventProps {
29-
/** Handler that is called when selection changes */
30-
onChange?: SerializedSelectionEventCallback;
31-
32-
/**
33-
* Handler that is called when the selection changes.
34-
* @deprecated Use `onChange` instead
35-
*/
36-
onSelectionChange?: SerializedSelectionEventCallback;
37-
}
38-
3928
export type SerializedListViewProps = (
4029
| WrappedDHListViewProps
4130
| WrappedDHListViewJSApiProps
4231
) &
43-
SerializedListViewEventProps;
32+
SerializedSelectionProps;
4433

4534
/**
4635
* Wrap ListView props with the appropriate serialized event callbacks.
@@ -49,24 +38,24 @@ export type SerializedListViewProps = (
4938
*/
5039
export function useListViewProps({
5140
density,
52-
selectionMode,
53-
onChange,
54-
onSelectionChange,
41+
selectionMode: selectionModeMaybeUppercase,
42+
onChange: serializedOnChange,
43+
onSelectionChange: serializedOnSelectionChange,
5544
...otherProps
5645
}: SerializedListViewProps): DHListViewProps | WrappedDHListViewJSApiProps {
5746
const densityLc = density?.toLowerCase() as Density;
58-
const selectionModeLc = selectionMode?.toLowerCase() as SelectionMode;
5947

60-
const serializedOnChange = useSelectionEventCallback(onChange);
61-
const serializedOnSelectionChange =
62-
useSelectionEventCallback(onSelectionChange);
48+
const { selectionMode, onChange, onSelectionChange } = useSelectionProps({
49+
selectionMode: selectionModeMaybeUppercase,
50+
onChange: serializedOnChange,
51+
onSelectionChange: serializedOnSelectionChange,
52+
});
6353

6454
return {
6555
density: densityLc,
66-
selectionMode: selectionModeLc,
67-
onChange: onChange == null ? undefined : serializedOnChange,
68-
onSelectionChange:
69-
onSelectionChange == null ? undefined : serializedOnSelectionChange,
56+
selectionMode,
57+
onChange,
58+
onSelectionChange,
7059
// The @deephaven/components `ListView` has its own normalization logic that
7160
// handles primitive children types (string, number, boolean). It also
7261
// handles nested children inside of `Item` and `Section` components, so

plugins/ui/src/js/src/widget/WidgetUtils.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import React, { ComponentType } from 'react';
44
// Importing `Item` and `Section` compnents directly since they should not be
55
// wrapped due to how Spectrum collection components consume them.
66
import {
7-
ActionGroup,
87
ActionMenu,
98
Item,
109
ListActionGroup,
@@ -34,6 +33,7 @@ import Column from '../layout/Column';
3433
import Dashboard from '../layout/Dashboard';
3534
import ListView from '../elements/ListView';
3635
import Picker from '../elements/Picker';
36+
import ActionGroup from '../elements/ActionGroup';
3737

3838
/*
3939
* Map element node names to their corresponding React components

0 commit comments

Comments
 (0)