fix(striker-ui): add preset icon props in IconButton presets

main
Tsu-ba-me 2 years ago
parent e5d7014b9f
commit 17657388af
  1. 41
      striker-ui/components/IconButton/IconButton.tsx
  2. 13
      striker-ui/types/IconButton.d.ts

@ -14,6 +14,7 @@ import { createElement, FC, ReactNode, useMemo } from 'react';
import { import {
BLACK, BLACK,
BLUE,
BORDER_RADIUS, BORDER_RADIUS,
DISABLED, DISABLED,
GREY, GREY,
@ -39,19 +40,19 @@ const NormalIconButton = styled(MUIIconButton)({
color: GREY, color: GREY,
}); });
const MAP_TO_VISIBILITY_ICON: IconButtonMapToStateIcon = { const MAP_TO_VISIBILITY_ICON: IconButtonMapToStateIconBundle = {
false: MUIVisibilityIcon, false: { iconType: MUIVisibilityIcon },
true: MUIVisibilityOffIcon, true: { iconType: MUIVisibilityOffIcon },
}; };
const MAP_TO_EDIT_ICON: IconButtonMapToStateIcon = { const MAP_TO_EDIT_ICON: IconButtonMapToStateIconBundle = {
false: MUIEditIcon, false: { iconType: MUIEditIcon },
true: MUIDoneIcon, true: { iconType: MUIDoneIcon, iconProps: { sx: { color: BLUE } } },
}; };
const MAP_TO_MAP_PRESET: Record< const MAP_TO_MAP_PRESET: Record<
IconButtonPresetMapToStateIcon, IconButtonPresetMapToStateIconBundle,
IconButtonMapToStateIcon IconButtonMapToStateIconBundle
> = { > = {
edit: MAP_TO_EDIT_ICON, edit: MAP_TO_EDIT_ICON,
visibility: MAP_TO_VISIBILITY_ICON, visibility: MAP_TO_VISIBILITY_ICON,
@ -72,28 +73,36 @@ const IconButton: FC<IconButtonProps> = ({
variant = 'contained', variant = 'contained',
...restIconButtonProps ...restIconButtonProps
}) => { }) => {
const mapToIcon = useMemo<IconButtonMapToStateIcon | undefined>( const mapToIcon = useMemo<IconButtonMapToStateIconBundle | undefined>(
() => externalMapToIcon ?? (mapPreset && MAP_TO_MAP_PRESET[mapPreset]), () => externalMapToIcon ?? (mapPreset && MAP_TO_MAP_PRESET[mapPreset]),
[externalMapToIcon, mapPreset], [externalMapToIcon, mapPreset],
); );
const defaultIconBundle = useMemo<Partial<IconButtonStateIconBundle>>(
() => ({ iconType: defaultIcon }),
[defaultIcon],
);
const iconButtonContent = useMemo(() => { const iconButtonContent = useMemo(() => {
let result: ReactNode; let result: ReactNode;
if (mapToIcon) { if (mapToIcon) {
const iconElementType: CreatableComponent | undefined = state const { iconType, iconProps: presetIconProps } = state
? mapToIcon[state] ?? defaultIcon ? mapToIcon[state] ?? defaultIconBundle
: defaultIcon; : defaultIconBundle;
if (iconElementType) { if (iconType) {
result = createElement(iconElementType, iconProps); result = createElement(iconType, {
...presetIconProps,
...iconProps,
});
} }
} else { } else {
result = children; result = children;
} }
return result; return result;
}, [children, mapToIcon, state, defaultIcon, iconProps]); }, [mapToIcon, state, defaultIconBundle, iconProps, children]);
const iconButtonElementType = useMemo( const iconButtonElementType = useMemo(
() => MAP_TO_VARIANT[variant], () => MAP_TO_VARIANT[variant],
[variant], [variant],

@ -1,16 +1,21 @@
type CreatableComponent = Parameters<typeof import('react').createElement>[0]; type CreatableComponent = Parameters<typeof import('react').createElement>[0];
type IconButtonPresetMapToStateIcon = 'edit' | 'visibility'; type IconButtonPresetMapToStateIconBundle = 'edit' | 'visibility';
type IconButtonMapToStateIcon = Record<string, CreatableComponent>; type IconButtonStateIconBundle = {
iconType: CreatableComponent;
iconProps?: import('@mui/material').SvgIconProps;
};
type IconButtonMapToStateIconBundle = Record<string, IconButtonStateIconBundle>;
type IconButtonVariant = 'contained' | 'normal'; type IconButtonVariant = 'contained' | 'normal';
type IconButtonOptionalProps = { type IconButtonOptionalProps = {
defaultIcon?: CreatableComponent; defaultIcon?: CreatableComponent;
iconProps?: import('@mui/material').SvgIconProps; iconProps?: import('@mui/material').SvgIconProps;
mapPreset?: IconButtonPresetMapToStateIcon; mapPreset?: IconButtonPresetMapToStateIconBundle;
mapToIcon?: IconButtonMapToStateIcon; mapToIcon?: IconButtonMapToStateIconBundle;
state?: string; state?: string;
variant?: IconButtonVariant; variant?: IconButtonVariant;
}; };

Loading…
Cancel
Save