Update toolbar service and add active icon in nested button
This commit is contained in:
parent
67508f6667
commit
911f93d378
@ -2,32 +2,31 @@ import React, { useEffect, useState } from 'react';
|
|||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { ToolbarButton } from '@ohif/ui';
|
import { ToolbarButton } from '@ohif/ui';
|
||||||
|
|
||||||
function NestedMenu({ children }) {
|
function NestedMenu({ children, label, icon }) {
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
const toggleNestedMenu = () => setIsOpen(!isOpen);
|
||||||
function closeNestedMenu() {
|
|
||||||
|
const closeNestedMenu = () => {
|
||||||
if (isOpen) {
|
if (isOpen) {
|
||||||
setIsOpen(false);
|
setIsOpen(false);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
window.addEventListener('click', closeNestedMenu);
|
window.addEventListener('click', closeNestedMenu);
|
||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener('click', closeNestedMenu);
|
window.removeEventListener('click', closeNestedMenu);
|
||||||
};
|
};
|
||||||
}, [isOpen]);
|
}, [isOpen]);
|
||||||
|
|
||||||
const dropdownContent = isOpen ? children : undefined;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ToolbarButton
|
<ToolbarButton
|
||||||
id="More"
|
id="NestedMenu"
|
||||||
label="More"
|
label={label}
|
||||||
icon="tool-more-menu"
|
icon={icon}
|
||||||
onClick={() => {
|
onClick={toggleNestedMenu}
|
||||||
setIsOpen(!isOpen);
|
dropdownContent={isOpen && children}
|
||||||
}}
|
|
||||||
dropdownContent={dropdownContent}
|
|
||||||
isActive={isOpen}
|
isActive={isOpen}
|
||||||
type="primary"
|
type="primary"
|
||||||
/>
|
/>
|
||||||
@ -36,6 +35,13 @@ function NestedMenu({ children }) {
|
|||||||
|
|
||||||
NestedMenu.propTypes = {
|
NestedMenu.propTypes = {
|
||||||
children: PropTypes.any.isRequired,
|
children: PropTypes.any.isRequired,
|
||||||
|
icon: PropTypes.string,
|
||||||
|
label: PropTypes.string,
|
||||||
|
};
|
||||||
|
|
||||||
|
NestedMenu.defaultProps = {
|
||||||
|
icon: "tool-more-menu",
|
||||||
|
label: "More",
|
||||||
};
|
};
|
||||||
|
|
||||||
export default NestedMenu;
|
export default NestedMenu;
|
||||||
|
|||||||
@ -55,7 +55,13 @@ function ViewerLayout({
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const defaultTool = { icon: 'tool-more-menu', label: 'More' };
|
||||||
const [toolbars, setToolbars] = useState({ primary: [], secondary: [] });
|
const [toolbars, setToolbars] = useState({ primary: [], secondary: [] });
|
||||||
|
const [activeTool, setActiveTool] = useState(defaultTool);
|
||||||
|
const onSecondaryClickHandler = () => setActiveTool(defaultTool);
|
||||||
|
const onPrimaryClickHandler = (evt, btn) => {
|
||||||
|
setActiveTool(btn.props.isActive ? btn.props : defaultTool);
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const { unsubscribe } = ToolBarService.subscribe(
|
const { unsubscribe } = ToolBarService.subscribe(
|
||||||
@ -63,8 +69,8 @@ function ViewerLayout({
|
|||||||
() => {
|
() => {
|
||||||
console.warn('~~~ TOOL BAR MODIFIED EVENT CAUGHT');
|
console.warn('~~~ TOOL BAR MODIFIED EVENT CAUGHT');
|
||||||
const updatedToolbars = {
|
const updatedToolbars = {
|
||||||
primary: ToolBarService.getButtonSection('primary'),
|
primary: ToolBarService.getButtonSection('primary', { onClick: onPrimaryClickHandler }),
|
||||||
secondary: ToolBarService.getButtonSection('secondary'),
|
secondary: ToolBarService.getButtonSection('secondary', { onClick: onSecondaryClickHandler }),
|
||||||
};
|
};
|
||||||
setToolbars(updatedToolbars);
|
setToolbars(updatedToolbars);
|
||||||
}
|
}
|
||||||
@ -86,11 +92,10 @@ function ViewerLayout({
|
|||||||
|
|
||||||
if (!isNested) {
|
if (!isNested) {
|
||||||
const { id, Component, componentProps } = toolDef;
|
const { id, Component, componentProps } = toolDef;
|
||||||
|
|
||||||
return <Component key={id} id={id} {...componentProps} />;
|
return <Component key={id} id={id} {...componentProps} />;
|
||||||
} else {
|
} else {
|
||||||
return (
|
return (
|
||||||
<NestedMenu>
|
<NestedMenu icon={activeTool.icon} label={activeTool.label}>
|
||||||
<div className="flex">
|
<div className="flex">
|
||||||
{toolDef.map(x => {
|
{toolDef.map(x => {
|
||||||
const { id, Component, componentProps } = x;
|
const { id, Component, componentProps } = x;
|
||||||
|
|||||||
@ -62,7 +62,7 @@ export default class ToolBarService {
|
|||||||
this._broadcastChange(this.EVENTS.TOOL_BAR_MODIFIED, {});
|
this._broadcastChange(this.EVENTS.TOOL_BAR_MODIFIED, {});
|
||||||
}
|
}
|
||||||
|
|
||||||
getButtonSection(key) {
|
getButtonSection(key, props) {
|
||||||
const buttonSectionIds = this.buttonSections[key];
|
const buttonSectionIds = this.buttonSections[key];
|
||||||
const buttonsInSection = [];
|
const buttonsInSection = [];
|
||||||
|
|
||||||
@ -79,7 +79,7 @@ export default class ToolBarService {
|
|||||||
|
|
||||||
btnIds.forEach(nestedBtnId => {
|
btnIds.forEach(nestedBtnId => {
|
||||||
const nestedBtn = this.buttons[nestedBtnId];
|
const nestedBtn = this.buttons[nestedBtnId];
|
||||||
const mappedNestedBtn = this._mapButtonToDisplay(nestedBtn, key);
|
const mappedNestedBtn = this._mapButtonToDisplay(nestedBtn, key, props);
|
||||||
|
|
||||||
nestedButtons.push(mappedNestedBtn);
|
nestedButtons.push(mappedNestedBtn);
|
||||||
});
|
});
|
||||||
@ -90,7 +90,7 @@ export default class ToolBarService {
|
|||||||
} else {
|
} else {
|
||||||
const btnId = btnIdOrArray;
|
const btnId = btnIdOrArray;
|
||||||
const btn = this.buttons[btnId];
|
const btn = this.buttons[btnId];
|
||||||
const mappedBtn = this._mapButtonToDisplay(btn, key);
|
const mappedBtn = this._mapButtonToDisplay(btn, key, props);
|
||||||
|
|
||||||
buttonsInSection.push(mappedBtn);
|
buttonsInSection.push(mappedBtn);
|
||||||
}
|
}
|
||||||
@ -136,8 +136,8 @@ export default class ToolBarService {
|
|||||||
* @param {*} btn
|
* @param {*} btn
|
||||||
* @param {*} btnSection
|
* @param {*} btnSection
|
||||||
*/
|
*/
|
||||||
_mapButtonToDisplay(btn, btnSection) {
|
_mapButtonToDisplay(btn, btnSection, props) {
|
||||||
const { id, type, component, props } = btn;
|
const { id, type, component } = btn;
|
||||||
const buttonType = this._buttonTypes()[type];
|
const buttonType = this._buttonTypes()[type];
|
||||||
|
|
||||||
if (!buttonType) {
|
if (!buttonType) {
|
||||||
@ -154,12 +154,15 @@ export default class ToolBarService {
|
|||||||
if (btn.props.clickHandler) {
|
if (btn.props.clickHandler) {
|
||||||
btn.clickHandler(evt, btn, btnSection);
|
btn.clickHandler(evt, btn, btnSection);
|
||||||
}
|
}
|
||||||
|
if (props && props.onClick) {
|
||||||
|
props.onClick(evt, btn, btnSection, props);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id,
|
id,
|
||||||
Component: component || buttonType.defaultComponent,
|
Component: component || buttonType.defaultComponent,
|
||||||
componentProps: Object.assign({}, props, { onClick }), //
|
componentProps: Object.assign({}, btn.props, props, { onClick }), //
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,6 +25,7 @@ const ToolbarButton = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
const shouldShowDropdown = !!isActive && !!dropdownContent;
|
const shouldShowDropdown = !!isActive && !!dropdownContent;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div key={id}>
|
<div key={id}>
|
||||||
<Tooltip
|
<Tooltip
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user