make toolbar reusable to use as secondary

This commit is contained in:
Rodrigo Antinarelli 2020-04-17 17:25:59 -03:00 committed by James A. Petts
parent 9154f36477
commit b651ba02ff
9 changed files with 53 additions and 86 deletions

View File

@ -28,7 +28,6 @@ export {
MeasurementTable, MeasurementTable,
NavBar, NavBar,
Notification, Notification,
PrimaryToolbar,
Select, Select,
SegmentationTable, SegmentationTable,
SidePanel, SidePanel,
@ -47,6 +46,7 @@ export {
Thumbnail, Thumbnail,
ThumbnailSR, ThumbnailSR,
ThumbnailList, ThumbnailList,
Toolbar,
ToolbarButton, ToolbarButton,
Tooltip, Tooltip,
Typography, Typography,

View File

@ -1,2 +0,0 @@
import PrimaryToolbar from './PrimaryToolbar';
export default PrimaryToolbar;

View File

@ -4,9 +4,16 @@ import classnames from 'classnames';
import { ToolbarButton, IconButton, Icon, Tooltip } from '@ohif/ui'; import { ToolbarButton, IconButton, Icon, Tooltip } from '@ohif/ui';
const PrimaryToolbar = ({ activeTool, tools, moreTools }) => { const classes = {
type: {
primary: '',
secondary: 'w-full items-center bg-primary-dark px-3',
},
};
const Toolbar = ({ activeTool, tools, moreTools, type }) => {
return ( return (
<div className="flex"> <div className={classnames('flex', classes.type[type])}>
{tools.map((tool) => { {tools.map((tool) => {
const { id, onClick, icon, label, dropdownContent } = tool; const { id, onClick, icon, label, dropdownContent } = tool;
const isActive = activeTool === tool.id; const isActive = activeTool === tool.id;
@ -19,13 +26,14 @@ const PrimaryToolbar = ({ activeTool, tools, moreTools }) => {
icon={icon} icon={icon}
label={label} label={label}
dropdownContent={dropdownContent} dropdownContent={dropdownContent}
type={type}
/> />
</div> </div>
); );
})} })}
{!!moreTools.length && ( {!!moreTools.length && (
<> <>
<span className="w-1 border-l py-4 mx-2 border-common-dark" /> <span className="w-1 border-l h-8 self-center mx-2 border-common-dark" />
<Tooltip position="bottom" content="More tools"> <Tooltip position="bottom" content="More tools">
<IconButton <IconButton
className={classnames( className={classnames(
@ -42,12 +50,14 @@ const PrimaryToolbar = ({ activeTool, tools, moreTools }) => {
); );
}; };
PrimaryToolbar.defaultProps = { Toolbar.defaultProps = {
activeTool: '', activeTool: '',
moreTools: [], moreTools: [],
type: 'primary',
}; };
PrimaryToolbar.propTypes = { Toolbar.propTypes = {
type: PropTypes.oneOf(['primary', 'secondary']),
activeTool: PropTypes.string, activeTool: PropTypes.string,
tools: PropTypes.arrayOf( tools: PropTypes.arrayOf(
PropTypes.shape({ PropTypes.shape({
@ -76,4 +86,4 @@ PrimaryToolbar.propTypes = {
), ),
}; };
export default PrimaryToolbar; export default Toolbar;

View File

@ -1,22 +1,22 @@
--- ---
name: Primary Toolbar name: Toolbar
menu: Data Display menu: Data Display
route: components/primaryToolbar route: components/toolbar
--- ---
import { useState } from 'react'; import { useState } from 'react';
import { Playground } from 'docz'; import { Playground, Props } from 'docz';
import classnames from 'classnames'; import classnames from 'classnames';
import { PrimaryToolbar } from '@ohif/ui'; import { Toolbar } from '@ohif/ui';
# Primary Toolbar # Toolbar
Primary Toolbar is used to create a list of tools. A Toolbar is used to create a list of tools.
## Import ## Import
```javascript ```javascript
import { PrimaryToolbar } from '@ohif/ui'; import { Toolbar } from '@ohif/ui';
``` ```
<Playground> <Playground>
@ -112,11 +112,7 @@ import { PrimaryToolbar } from '@ohif/ui';
]; ];
return ( return (
<div className="py-10 flex justify-center"> <div className="py-10 flex justify-center">
<PrimaryToolbar <Toolbar moreTools={moreTools} activeTool={activeTool} tools={tools} />
moreTools={moreTools}
activeTool={activeTool}
tools={tools}
/>
</div> </div>
); );
}} }}
@ -124,4 +120,4 @@ import { PrimaryToolbar } from '@ohif/ui';
## Properties ## Properties
<Props of={PrimaryToolbar} /> <Props of={Toolbar} />

View File

@ -0,0 +1,2 @@
import Toolbar from './Toolbar';
export default Toolbar;

View File

@ -5,6 +5,7 @@ import classnames from 'classnames';
import { IconButton, Icon, Tooltip } from '@ohif/ui'; import { IconButton, Icon, Tooltip } from '@ohif/ui';
const ToolbarButton = ({ const ToolbarButton = ({
type,
id, id,
isActive, isActive,
onClick, onClick,
@ -12,6 +13,17 @@ const ToolbarButton = ({
label, label,
dropdownContent, dropdownContent,
}) => { }) => {
const classes = {
type: {
primary: isActive
? 'text-black'
: 'text-common-bright hover:bg-primary-dark hover:text-primary-light',
secondary: isActive
? 'text-black'
: 'text-white hover:bg-secondary-dark hover:text-white focus:bg-secondary-dark focus:text-white',
},
};
const shouldShowDropdown = !!isActive && !!dropdownContent; const shouldShowDropdown = !!isActive && !!dropdownContent;
return ( return (
<div key={id}> <div key={id}>
@ -21,10 +33,7 @@ const ToolbarButton = ({
> >
<IconButton <IconButton
variant={isActive ? 'contained' : 'text'} variant={isActive ? 'contained' : 'text'}
className={classnames('mx-1', { className={classnames('mx-1', classes.type[type])}
'text-black': isActive,
'text-common-bright hover:bg-primary-dark hover:text-primary-light': !isActive,
})}
onClick={onClick} onClick={onClick}
key={id} key={id}
> >
@ -38,9 +47,11 @@ const ToolbarButton = ({
ToolbarButton.defaultProps = { ToolbarButton.defaultProps = {
dropdownContent: null, dropdownContent: null,
isActive: false, isActive: false,
type: 'primary',
}; };
ToolbarButton.propTypes = { ToolbarButton.propTypes = {
type: PropTypes.oneOf(['primary', 'secondary']),
id: PropTypes.string.isRequired, id: PropTypes.string.isRequired,
isActive: PropTypes.bool, isActive: PropTypes.bool,
onClick: PropTypes.func.isRequired, onClick: PropTypes.func.isRequired,

View File

@ -15,7 +15,6 @@ import MeasurementTable from './MeasurementTable';
import NavBar from './NavBar'; import NavBar from './NavBar';
import Notification from './Notification'; import Notification from './Notification';
import Select from './Select'; import Select from './Select';
import PrimaryToolbar from './PrimaryToolbar';
import SegmentationTable from './SegmentationTable'; import SegmentationTable from './SegmentationTable';
import SidePanel from './SidePanel'; import SidePanel from './SidePanel';
import StudyBrowser from './StudyBrowser'; import StudyBrowser from './StudyBrowser';
@ -33,6 +32,7 @@ import ThemeWrapper from './ThemeWrapper/';
import Thumbnail from './Thumbnail'; import Thumbnail from './Thumbnail';
import ThumbnailSR from './ThumbnailSR'; import ThumbnailSR from './ThumbnailSR';
import ThumbnailList from './ThumbnailList'; import ThumbnailList from './ThumbnailList';
import Toolbar from './Toolbar';
import ToolbarButton from './ToolbarButton'; import ToolbarButton from './ToolbarButton';
import Tooltip from './Tooltip'; import Tooltip from './Tooltip';
import Typography from './Typography'; import Typography from './Typography';
@ -56,7 +56,6 @@ export {
MeasurementTable, MeasurementTable,
NavBar, NavBar,
Notification, Notification,
PrimaryToolbar,
Select, Select,
SegmentationTable, SegmentationTable,
SidePanel, SidePanel,
@ -75,6 +74,7 @@ export {
Thumbnail, Thumbnail,
ThumbnailSR, ThumbnailSR,
ThumbnailList, ThumbnailList,
Toolbar,
ToolbarButton, ToolbarButton,
Tooltip, Tooltip,
Typography, Typography,

View File

@ -1,5 +1,5 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { NavBar, Svg, Icon, IconButton, PrimaryToolbar } from '@ohif/ui'; import { NavBar, Svg, Icon, IconButton, Toolbar } from '@ohif/ui';
const Header = () => { const Header = () => {
const [activeTool, setActiveTool] = useState('Zoom'); const [activeTool, setActiveTool] = useState('Zoom');
@ -89,11 +89,7 @@ const Header = () => {
</div> </div>
</div> </div>
<div className="flex items-center"> <div className="flex items-center">
<PrimaryToolbar <Toolbar tools={tools} activeTool={activeTool} moreTools={tools} />
tools={tools}
activeTool={activeTool}
moreTools={tools}
/>
</div> </div>
<div className="flex items-center"> <div className="flex items-center">
<span className="mr-3 text-common-light text-lg"> <span className="mr-3 text-common-light text-lg">

View File

@ -1,10 +1,9 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import classnames from 'classnames'; import classnames from 'classnames';
import { Icon, IconButton } from '@ohif/ui'; import { Toolbar } from '@ohif/ui';
const ViewportToolbar = () => { const ViewportToolbar = () => {
const [activeTool, setActiveTool] = useState(null);
const tools = [ const tools = [
{ {
id: 'Annotate', id: 'Annotate',
@ -13,6 +12,7 @@ const ViewportToolbar = () => {
type: null, type: null,
commandName: 'setToolActive', commandName: 'setToolActive',
commandOptions: { toolName: 'Annotate' }, commandOptions: { toolName: 'Annotate' },
onClick: () => console.log('Activate Annotate'),
}, },
{ {
id: 'Bidirectional', id: 'Bidirectional',
@ -21,6 +21,7 @@ const ViewportToolbar = () => {
type: null, type: null,
commandName: 'setToolActive', commandName: 'setToolActive',
commandOptions: { toolName: 'Bidirectional' }, commandOptions: { toolName: 'Bidirectional' },
onClick: () => console.log('Activate Bidirectional'),
}, },
{ {
id: 'Elipse', id: 'Elipse',
@ -29,6 +30,7 @@ const ViewportToolbar = () => {
type: null, type: null,
commandName: 'setToolActive', commandName: 'setToolActive',
commandOptions: { toolName: 'Elipse' }, commandOptions: { toolName: 'Elipse' },
onClick: () => console.log('Activate Elipse'),
}, },
{ {
id: 'Length', id: 'Length',
@ -37,58 +39,10 @@ const ViewportToolbar = () => {
type: null, type: null,
commandName: 'setToolActive', commandName: 'setToolActive',
commandOptions: { toolName: 'Length' }, commandOptions: { toolName: 'Length' },
onClick: () => console.log('Activate Length'),
}, },
]; ];
const renderToolbar = () => { return <Toolbar type="secondary" tools={tools} />;
return tools.map((tool, i) => {
const isActive = activeTool === tool.id;
return (
<div
className="relative flex justify-center showTooltipOnHover"
key={tool.id}
>
<IconButton
variant={isActive ? 'contained' : 'text'}
className={classnames('mx-1', {
'text-black': isActive,
'text-white hover:bg-secondary-dark hover:text-white focus:bg-secondary-dark focus:text-white': !isActive,
})}
onClick={(e) => {
setActiveTool(tool.id);
}}
>
<Icon name={tool.icon} />
</IconButton>
<div
className={classnames(
'tooltip tooltip-up bg-primary-dark border border-secondary-main text-white text-base rounded py-1 px-4 inset-x-auto top-full mt-2 w-max-content'
)}
>
{tool.label}
<svg
className="absolute text-primary-dark w-full h-4 left-0 stroke-secondary-main"
style={{ top: -15 }}
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
>
<path fill="currentColor" d="M24 22h-24l12-20z" />
</svg>
</div>
</div>
);
});
};
return (
<div
className={classnames(
'flex w-full items-center bg-primary-dark px-3 py-2'
)}
>
<div className="flex items-center">{renderToolbar()}</div>
</div>
);
}; };
export default ViewportToolbar; export default ViewportToolbar;