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