Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: moved overlay controls into separated context #157

Merged
merged 5 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ interface DashKitProps {
itemsStateAndParams: ItemsStateAndParams;
settings: SettingsProps;
context: ContextProps;
overlayControls?: Record<string, OverlayControlItem[]>;
overlayControls?: Record<string, OverlayControlItem[]> | null;
overlayMenuItems?: MenuItems[] | null;
noOverlay?: boolean;
focusable?: boolean;
draggableHandleClassName?: string;
Expand All @@ -51,7 +52,8 @@ interface DashKitProps {
- **itemsStateAndParams**: [itemsStateAndParams](#temsStateAndParams).
- **settings**: DashKit settings.
- **context**: Object that will be propped up on all widgets.
- **overlayControls**: Object that overrides widget controls at the time of editing. If not transmitted, basic controls will be displayed.
- **overlayControls**: Object that overrides widget controls at the time of editing. If not transmitted, basic controls will be displayed. If `null` passed only close button or custom menu will be displayed.
- **overlayMenuItems**: Custom dropdown menu items
- **noOverlay**: If `true`, overlay and controls are not displayed while editing.
- **focusable**: If `true`, grid items will be focusable.
- **draggableHandleClassName** : СSS class name of the element that makes the widget draggable.
Expand Down Expand Up @@ -343,6 +345,10 @@ type MenuItem = {
};

// use array of menu items in settings
<Dashkit overlayMenuItems={[] as Array<MenuItem> | null} />

[deprecated]
// overlayMenuItems property has greater priority over setSettings menu
DashKit.setSettings({menu: [] as Array<MenuItem>});
```

Expand Down Expand Up @@ -386,7 +392,7 @@ type ItemDropProps = {
#### Example:

```jsx
const menuItems = [
const overlayMenuItems = [
{
id: 'chart',
icon: <Icon data={ChartColumn} />,
Expand All @@ -405,7 +411,7 @@ const onDrop = (dropProps: ItemDropProps) => {

<DashKitDnDWrapper>
<DashKit editMode={true} config={config} onChange={onChange} onDrop={onDrop} />
<ActionPanel items={menuItems} />
<ActionPanel items={overlayMenuItems} />
</DashKitDnDWrapper>
```

Expand Down
4 changes: 3 additions & 1 deletion src/components/DashKit/DashKit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
AddConfigItem,
ContextProps,
DashKitGroup,
MenuItem,
Plugin,
SetConfigItem,
SetNewItemOptions,
Expand All @@ -29,7 +30,8 @@ interface DashKitGeneralProps {
config: Config;
editMode: boolean;
draggableHandleClassName?: string;
overlayControls?: Record<string, OverlayControlItem[]>;
overlayControls?: Record<string, OverlayControlItem[]> | null;
overlayMenuItems?: MenuItem[] | null;
}

interface DashKitDefaultProps {
Expand Down
28 changes: 0 additions & 28 deletions src/components/DashKit/__stories__/CssApiShowcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,11 @@ import {
import {Icon} from '@gravity-ui/uikit';

import {ActionPanel, DashKit} from '../../..';
import {MenuItems} from '../../../helpers';
import {i18n} from '../../../i18n';
import {CogIcon} from '../../../icons/CogIcon';
import {CopyIcon} from '../../../icons/CopyIcon';
import {DeleteIcon} from '../../../icons/DeleteIcon';

import {Demo, DemoRow} from './Demo';
import {getConfig} from './utils';

export const CssApiShowcase: React.FC = () => {
React.useEffect(() => {
DashKit.setSettings({
menu: [
{
id: 'settings',
title: 'Menu setting text',
icon: <Icon data={CogIcon} size={16} />,
},
{
id: MenuItems.Copy,
title: 'Menu setting copy',
icon: <Icon data={CopyIcon} size={16} />,
},
{
id: MenuItems.Delete,
title: i18n('label_delete'), // for language change check
icon: <Icon data={DeleteIcon} size={16} />,
className: 'dashkit-overlay-controls__item_danger',
},
],
});
}, []);

const items = React.useMemo(
() => [
{
Expand Down
36 changes: 7 additions & 29 deletions src/components/DashKit/__stories__/DashKitDnDShowcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,13 @@
import {Icon} from '@gravity-ui/uikit';

import {ActionPanel, DashKit, DashKitDnDWrapper, DashKitProps} from '../../..';
import {MenuItems} from '../../../helpers';
import {i18n} from '../../../i18n';
import {CogIcon} from '../../../icons/CogIcon';
import {CopyIcon} from '../../../icons/CopyIcon';
import {DeleteIcon} from '../../../icons/DeleteIcon';

import {Demo, DemoRow} from './Demo';
import {getConfig} from './utils';

export const DashKitDnDShowcase: React.FC = () => {
React.useEffect(() => {
DashKit.setSettings({
menu: [
{
id: 'settings',
title: 'Menu setting text',
icon: <Icon data={CogIcon} size={16} />,
},
{
id: MenuItems.Copy,
title: 'Menu setting copy',
icon: <Icon data={CopyIcon} size={16} />,
},
{
id: MenuItems.Delete,
title: i18n('label_delete'), // for language change check
icon: <Icon data={DeleteIcon} size={16} />,
className: 'dashkit-overlay-controls__item_danger',
},
],
});
}, []);

const onClick = () => {
console.log('click');

Check warning on line 13 in src/components/DashKit/__stories__/DashKitDnDShowcase.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
};

const items = React.useMemo(
Expand Down Expand Up @@ -100,7 +72,7 @@
);
const [config, setConfig] = React.useState(getConfig());

const onChange = React.useCallback(({config}: {config: DashKitProps['config']}) => {

Check warning on line 75 in src/components/DashKit/__stories__/DashKitDnDShowcase.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

'config' is already declared in the upper scope on line 73 column 12
setConfig(config);
}, []);

Expand Down Expand Up @@ -148,15 +120,21 @@
return (
<DashKitDnDWrapper
onDragStart={() => {
console.log('dragStarted');

Check warning on line 123 in src/components/DashKit/__stories__/DashKitDnDShowcase.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
}}
onDragEnd={() => {
console.log('dragEnded');

Check warning on line 126 in src/components/DashKit/__stories__/DashKitDnDShowcase.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
}}
>
<Demo title="Drag'n'Drop example">
<DemoRow title="Component view">
<DashKit editMode={true} config={config} onChange={onChange} onDrop={onDrop} />
<DashKit
editMode={true}
config={config}
overlayControls={null}
onChange={onChange}
onDrop={onDrop}
/>
<ActionPanel items={items} />
</DemoRow>
</Demo>
Expand Down
41 changes: 20 additions & 21 deletions src/components/DashKit/__stories__/DashKitGroupsShowcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,29 @@
export const DashKitGroupsShowcase: React.FC = () => {
const [editMode, setEditMode] = React.useState(true);

React.useEffect(() => {
DashKit.setSettings({
menu: [
{
id: 'settings',
title: 'Menu setting text',
icon: <Icon data={CogIcon} size={16} />,
},
{
id: MenuItems.Copy,
title: 'Menu setting copy',
icon: <Icon data={CopyIcon} size={16} />,
},
{
id: MenuItems.Delete,
title: i18n('label_delete'), // for language change check
icon: <Icon data={DeleteIcon} size={16} />,
className: 'dashkit-overlay-controls__item_danger',
},
],
});
const overlayMenuItems = React.useMemo(() => {
return [
{
id: 'settings',
title: 'Menu setting text',
icon: <Icon data={CogIcon} size={16} />,
},
{
id: MenuItems.Copy,
title: 'Menu setting copy',
icon: <Icon data={CopyIcon} size={16} />,
},
{
id: MenuItems.Delete,
title: i18n('label_delete'), // for language change check
icon: <Icon data={DeleteIcon} size={16} />,
className: 'dashkit-overlay-controls__item_danger',
},
];
}, []);

const onClick = () => {
console.log('click');

Check warning on line 49 in src/components/DashKit/__stories__/DashKitGroupsShowcase.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
};

const items = React.useMemo(
Expand Down Expand Up @@ -110,7 +108,7 @@
);
const [config, setConfig] = React.useState(getConfig(true));

const onChange = React.useCallback(({config}: {config: DashKitProps['config']}) => {

Check warning on line 111 in src/components/DashKit/__stories__/DashKitGroupsShowcase.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

'config' is already declared in the upper scope on line 109 column 12
setConfig(config);
}, []);

Expand Down Expand Up @@ -201,10 +199,10 @@
return (
<DashKitDnDWrapper
onDragStart={() => {
console.log('dragStarted');

Check warning on line 202 in src/components/DashKit/__stories__/DashKitGroupsShowcase.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
}}
onDragEnd={() => {
console.log('dragEnded');

Check warning on line 205 in src/components/DashKit/__stories__/DashKitGroupsShowcase.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
}}
>
<Demo title="Groups">
Expand All @@ -220,6 +218,7 @@
config={config}
onChange={onChange}
onDrop={onDrop}
overlayMenuItems={overlayMenuItems}
/>
<ActionPanel items={items} />
</DemoRow>
Expand Down
70 changes: 45 additions & 25 deletions src/components/DashKit/__stories__/DashKitShowcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import {DeleteIcon} from '../../../icons/DeleteIcon';
import {TickIcon} from '../../../icons/TickIcon';
import {WarningIcon} from '../../../icons/WarningIcon';
import type {ConfigItem} from '../../../index';
import type {ConfigItem, OverlayControlItem} from '../../../index';
import {cn} from '../../../utils/cn';

import {Demo, DemoRow} from './Demo';
Expand All @@ -38,9 +38,10 @@

lastAction: string;
customControlsActionData: number;
showCustomMenu: boolean;
enableActionPanel: boolean;
enableAnimations: boolean;
enableOverlayControls: boolean;
overlayMenuItems: DashKitProps['overlayMenuItems'];
};

export class DashKitShowcase extends React.Component<{}, DashKitDemoState> {
Expand All @@ -57,21 +58,20 @@

lastAction: 'Nothing',
customControlsActionData: 0,
showCustomMenu: true,
enableActionPanel: false,
enableAnimations: true,
enableOverlayControls: true,
overlayMenuItems: null,
};

private controls: Record<string, OverlayControlItem[]>;

private dashKitRef = React.createRef<DashKit>();

componentDidMount() {
this.toggleCustomMenu(true);
}
constructor(props: {}) {
super(props);

render() {
console.log('customControlsActionData', this.state.customControlsActionData);
const {editMode} = this.state;
const controls = {
this.controls = {
custom: [
{
title: 'Edit custom widget',
Expand All @@ -85,23 +85,31 @@
allWidgetsControls: true,
title: 'Icon tooltip 1',
icon: TickIcon,
handler: () => console.log('overlayControls::custom click'),

Check warning on line 88 in src/components/DashKit/__stories__/DashKitShowcase.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
iconSize: 16,
visible: (item) => item.type !== 'text',
},
{
allWidgetsControls: true,
excludeWidgetsTypes: ['title'],
id: MenuItems.Settings,
title: 'Settings default',
icon: WarningIcon,
visible: (item) => item.type !== 'text',
},
{
allWidgetsControls: true,
title: 'Icon tooltip 2',
handler: () => console.log('overlayControls::custom click'),

Check warning on line 103 in src/components/DashKit/__stories__/DashKitShowcase.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
visible: (item) => item.type !== 'text',
},
],
};
}

render() {
console.log('customControlsActionData', this.state.customControlsActionData);
const {editMode} = this.state;

return (
<Demo title="DashKit">
Expand All @@ -128,11 +136,22 @@
<Button
view="normal"
size="m"
onClick={() => this.toggleCustomMenu(false)}
onClick={this.toggleOverlayControls}
className={b('btn-contol')}
disabled={!editMode}
>
{this.state.showCustomMenu
{this.state.enableOverlayControls
? 'Hide left controls'
: 'Show left controls'}
</Button>
<Button
view="normal"
size="m"
onClick={this.toggleCustomMenu}
className={b('btn-contol')}
disabled={!editMode}
>
{this.state.overlayMenuItems
? 'Disable custom menu'
: 'Enable custom menu'}
</Button>
Expand Down Expand Up @@ -207,7 +226,8 @@
onChange={this.onChange}
settings={this.state.settings}
ref={this.dashKitRef}
overlayControls={controls}
overlayControls={this.state.enableOverlayControls ? this.controls : null}
overlayMenuItems={this.state.overlayMenuItems}
focusable={!editMode}
/>
</DemoRow>
Expand Down Expand Up @@ -346,41 +366,41 @@
return Boolean(this.state.config.items.find((item) => item.id === titleId));
}

private toggleCustomMenu = (init = false) => {
const {showCustomMenu} = this.state;
if (showCustomMenu) {
DashKit.setSettings({menu: []});
private toggleOverlayControls = () => {
this.setState({enableOverlayControls: !this.state.enableOverlayControls});
};

private toggleCustomMenu = () => {
if (this.state.overlayMenuItems) {
this.setState({overlayMenuItems: null});
} else {
DashKit.setSettings({
menu: [
this.setState({
overlayMenuItems: [
{
id: 'settings',
title: 'Menu setting text',
icon: <Icon data={CogIcon} size={16} />,
handler: () => {
console.log('menu::settings::click');
},
visible: (item) => item.type !== 'custom',
},
{
id: MenuItems.Copy,
title: 'Menu setting copy',
icon: <Icon data={CopyIcon} size={16} />,
visible: (item) => item.type !== 'custom',
},
{
id: MenuItems.Delete,
title: i18n('label_delete'), // for language change check
icon: <Icon data={DeleteIcon} size={16} />,
className: 'dashkit-overlay-controls__item_danger',
visible: (item) => item.type !== 'custom',
},
],
});
}
this.setState({
showCustomMenu: !showCustomMenu,
lastAction: init
? this.state.lastAction
: `[DashKit.setSettings] toggle show custom widget menu: ${new Date().toISOString()}`,
});
};

private toggleActionPanel() {
Expand Down
Loading
Loading