import { FC, useMemo } from 'react'; import Grid from '@mui/material/Grid'; import Typography from '@mui/material/Typography'; import IconButton from '@mui/material/IconButton'; import CustomButton from '../customButton/CustomButton'; import Icons from '../icons/Icons'; import SearchInput from '../customInput/SearchInput'; import { throwErrorForDev } from '../../utils/Common'; import CustomIconButton from '../customButton/CustomIconButton'; import type { ToolBarConfig, ToolBarType } from './Types'; const CustomToolBar: FC = props => { const { toolbarConfigs, selected = [], hideOnDisable = false } = props; // remove hidden button const leftConfigs = useMemo(() => { return toolbarConfigs.filter( (c: ToolBarConfig) => !c.hidden && c.icon !== 'search' && c.type !== 'switch' && c.position !== 'right' ); }, [toolbarConfigs]); const rightConfigs = useMemo(() => { return toolbarConfigs.filter( c => c.icon === 'search' || c.type === 'switch' || c.position === 'right' ); }, [toolbarConfigs]); return ( <> ({ marginBottom: theme.spacing(1) })} > {leftConfigs.map((c, i) => { const isSelect = c.type === 'select' || c.type === 'groupSelect'; if (isSelect) { return c.component; } const Icon = c.icon ? Icons[c.icon!]() : ''; const disabled = c.disabled ? c.disabled(selected) : false; if ( disabled && // Check if the component is disabled hideOnDisable && // Check if the component should be hidden when disabled !c.alwaysShow && // Check if the button is not marked to always be shown (typeof c.hideOnDisable === 'undefined' || c.hideOnDisable()) // Check if hideOnDisable on button is undefined or returns true ) { return null; // Return null to hide the component } // when disabled "disabledTooltip" will replace "tooltip" const tooltip = disabled && c.disabledTooltip ? c.disabledTooltip : c.tooltip; const isIcon = c.type === 'iconBtn'; const btn = ( ({ marginRight: theme.spacing(0.5) })} role="button" > {c.label} ); const iconBtn = ( {Icon} ); return isIcon ? iconBtn : btn; })} {rightConfigs.length > 0 && ( {rightConfigs.map((c, i) => { if (c.icon === 'search') { if (!c.onSearch) { return throwErrorForDev( `if icon is search onSearch event handler is required` ); } return ( ); } switch (c.type) { case 'select': case 'groupSelect': if (!c.component) { return throwErrorForDev(`component prop is required`); } return c.component; default: const Icon = c.icon ? Icons[c.icon]() : ''; return Icon ? ( {Icon} ) : (
Need Icon
); } })}
)}
); }; export default CustomToolBar;