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: add public method for hiding overlayControl item by id #93

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 11 additions & 0 deletions src/components/DashKit/DashKit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
settings: SettingsProps;
context: ContextProps;
noOverlay: boolean;
hideWidgetOverlayControl: (args: {id: string; controlId: string}) => void;
}

export interface DashKitProps extends DashKitGeneralProps, Partial<DashKitDefaultProps> {}
Expand All @@ -45,6 +46,7 @@
static defaultProps: DashKitDefaultProps = {
onItemEdit: noop,
onChange: noop,
hideWidgetOverlayControl: noop,
defaultGlobalParams: {},
globalParams: {},
itemsStateAndParams: {},
Expand Down Expand Up @@ -125,4 +127,13 @@
getItemsMeta() {
return this.metaRef.current?.getItemsMeta();
}

/**

Check warning on line 131 in src/components/DashKit/DashKit.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Missing JSDoc @returns for function

Check warning on line 131 in src/components/DashKit/DashKit.tsx

View workflow job for this annotation

GitHub Actions / build

Missing JSDoc @returns for function
* public method for hiding overlayControl item by id
* for ex. can be triggered after data render
* @param args
*/
hideWidgetOverlayControl(args: {id: string; controlId: string}) {
return this.metaRef.current?.hideWidgetOverlayControl(args);
}
}
17 changes: 17 additions & 0 deletions src/components/DashKit/__stories__/DashKitShowcase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
}

render() {
console.log('customControlsActionData', this.state.customControlsActionData);

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

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement

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

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
const {editMode} = this.state;
const controls = {
custom: [
Expand All @@ -78,7 +78,7 @@
allWidgetsControls: true,
title: 'Icon tooltip 1',
icon: TickIcon,
handler: () => console.log('overlayControls::custom click'),

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

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement

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

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
iconSize: 16,
},
{
Expand All @@ -91,7 +91,7 @@
{
allWidgetsControls: true,
title: 'Icon tooltip 2',
handler: () => console.log('overlayControls::custom click'),

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

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement

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

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
},
],
};
Expand Down Expand Up @@ -170,6 +170,17 @@
? 'Disable action panel'
: 'Enable action panel'}
</Button>
<Button
view="normal"
size="m"
onClick={() =>
this.hideWidgetOverlayControl({id: 'Ea', controlId: 'settings'})
}
className={b('btn-contol')}
disabled={!editMode}
>
hide custom setting from widget item
</Button>
</div>
</DemoRow>
<DemoRow title="Last action in DashKit">{this.state.lastAction}</DemoRow>
Expand Down Expand Up @@ -204,10 +215,10 @@
};

private onItemEdit = ({id, type, namespace, data}: ConfigItem) => {
console.log('id', id);

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

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement

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

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
console.log('type', type);

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

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement

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

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
console.log('namespace', namespace);

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

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement

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

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
console.log('data', data);

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

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement

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

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
this.setState({
lastAction: `[onItemEdit] Widget (id = '${id}') has been changed: ${new Date().toISOString()}`,
});
Expand All @@ -220,8 +231,8 @@
config: DashKitProps['config'];
itemsStateAndParams: DashKitProps['itemsStateAndParams'];
}) => {
console.log('config', config);

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

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement

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

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
console.log('itemsStateAndParams', itemsStateAndParams);

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

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement

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

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement

this.setState({
lastAction: `[onChange] config or itemsStateAndParams has been changed: ${new Date().toISOString()}`,
Expand Down Expand Up @@ -366,6 +377,12 @@
this.setState({enableActionPanel: !this.state.enableActionPanel});
}

private hideWidgetOverlayControl(args: {id: string; controlId: string}) {
if (this.dashKitRef.current) {
this.dashKitRef.current.hideWidgetOverlayControl(args);
}
}

private getActionPanelItems() {
return [
{
Expand Down
6 changes: 6 additions & 0 deletions src/components/GridLayout/GridLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ export default class GridLayout extends React.PureComponent {
return getItemsMeta(this.pluginsRefs);
};

hideWidgetOverlayControl = (args) => {
const {hideWidgetOverlayControl} = this.context;
const plugin = this.pluginsRefs.find((item) => item.props.id);
return hideWidgetOverlayControl({...args, plugin});
};

reloadItems() {
const {
editMode,
Expand Down
24 changes: 23 additions & 1 deletion src/hocs/withContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function useMemoStateContext(props) {
const originalLayouts = React.useRef({});
const adjustedLayouts = React.useRef({});

const [filteredOverlayControls, setFilteredOverlayControls] = React.useState(
props.overlayControls || [],
);

// TODO: need move originalLayouts, adjustedLayouts to state
const [layoutUpdateCounter, forceUpdateLayout] = React.useState(0);

Expand Down Expand Up @@ -156,6 +160,19 @@ function useMemoStateContext(props) {
.filter(Boolean);
}, []);

const hideWidgetOverlayControl = React.useCallback(
(args) => {
const newOverlayControls = props.overlayControls.allWidgetsControls.filter(
(item) => item.id !== args.controlId,
);
setFilteredOverlayControls({
...props.overlayControls,
allWidgetsControls: newOverlayControls,
});
},
[props.overlayControls],
);

const resultLayout = React.useMemo(() => {
return props.layout.map((item) => {
if (item.i in adjustedLayouts.current) {
Expand Down Expand Up @@ -193,6 +210,8 @@ function useMemoStateContext(props) {
editItem: props.onItemEdit,
layoutChange: onLayoutChange,
getItemsMeta,
hideWidgetOverlayControl,
filteredOverlayControls,
reloadItems,
memorizeOriginalLayout,
revertToOriginalLayout,
Expand All @@ -215,6 +234,8 @@ function useMemoStateContext(props) {
props.onItemEdit,
onLayoutChange,
getItemsMeta,
hideWidgetOverlayControl,
filteredOverlayControls,
reloadItems,
memorizeOriginalLayout,
revertToOriginalLayout,
Expand All @@ -227,10 +248,11 @@ function useMemoStateContext(props) {
export function withContext(Component) {
const WithContext = (props) => {
const contextValue = useMemoStateContext(props);
const overlayControls = contextValue.filteredOverlayControls;

return (
<DashKitContext.Provider value={contextValue}>
<Component overlayControls={props.overlayControls} />
<Component overlayControls={overlayControls} />
</DashKitContext.Provider>
);
};
Expand Down
Loading