diff --git a/packages/smarthr-ui/src/components/Switch/Switch.tsx b/packages/smarthr-ui/src/components/Switch/Switch.tsx index 121f9d243f..d5e3d126ca 100644 --- a/packages/smarthr-ui/src/components/Switch/Switch.tsx +++ b/packages/smarthr-ui/src/components/Switch/Switch.tsx @@ -1,4 +1,12 @@ -import React, { InputHTMLAttributes, ReactNode, forwardRef, useId, useMemo } from 'react' +import React, { + InputHTMLAttributes, + PropsWithChildren, + ReactNode, + forwardRef, + memo, + useId, + useMemo, +} from 'react' import { tv } from 'tailwind-variants' import { FaCheckIcon } from '../Icon' @@ -57,38 +65,54 @@ export const Switch = forwardRef( const defaultId = useId() const inputId = id || defaultId - const { wrapperStyle, inputStyle, iconStyle, iconWrapperStyle } = useMemo(() => { + const classNames = useMemo(() => { const { wrapper, input, icon, iconWrapper } = switchStyle() return { - wrapperStyle: wrapper({ className }), - inputStyle: input(), - iconStyle: icon(), - iconWrapperStyle: iconWrapper(), + wrapper: wrapper({ className }), + input: input(), + icon: icon(), + iconWrapper: iconWrapper(), } }, [className]) - const ActualLabelComponent = dangerouslyLabelHidden ? VisuallyHiddenText : Text - return ( - + {children} - - + + - - - + ) }, ) + +const MemoizedLabel = memo< + Pick & PropsWithChildren<{ htmlFor: string }> +>(({ dangerouslyLabelHidden, htmlFor, children }) => { + const Component = dangerouslyLabelHidden ? VisuallyHiddenText : Text + + return ( + + {children} + + ) +}) + +const MemoizedSuffixIcon = memo<{ className: string; iconClassName: string }>( + ({ className, iconClassName }) => ( + + + + ), +) diff --git a/packages/smarthr-ui/src/components/TabBar/TabBar.tsx b/packages/smarthr-ui/src/components/TabBar/TabBar.tsx index 0fd02a746f..8ed0357be9 100644 --- a/packages/smarthr-ui/src/components/TabBar/TabBar.tsx +++ b/packages/smarthr-ui/src/components/TabBar/TabBar.tsx @@ -3,7 +3,7 @@ import { tv } from 'tailwind-variants' import { Reel } from '../Layout' -const tabBar = tv({ +const classNameGenerator = tv({ slots: { wrapper: 'smarthr-ui-TabBar', inner: 'shr-grow', @@ -26,23 +26,19 @@ type Props = PropsWithChildren<{ }> type ElementProps = Omit, keyof Props | 'role'> -export const TabBar: FC = ({ - className, - bordered = true, - children, - ...props -}) => { - const { wrapperStyle, innerStyle } = useMemo(() => { - const { wrapper, inner } = tabBar() +export const TabBar: FC = ({ className, bordered, children, ...props }) => { + const classNames = useMemo(() => { + const { wrapper, inner } = classNameGenerator() + return { - wrapperStyle: wrapper({ className }), - innerStyle: inner({ bordered }), + wrapper: wrapper({ className }), + inner: inner({ bordered: bordered ?? true }), } }, [bordered, className]) return ( - -
{children}
+ +
{children}
) } diff --git a/packages/smarthr-ui/src/components/TabBar/TabItem.tsx b/packages/smarthr-ui/src/components/TabBar/TabItem.tsx index 5d536114ad..ef48bd4a99 100644 --- a/packages/smarthr-ui/src/components/TabBar/TabItem.tsx +++ b/packages/smarthr-ui/src/components/TabBar/TabItem.tsx @@ -1,4 +1,4 @@ -import React, { ComponentProps, FC, PropsWithChildren, useMemo } from 'react' +import React, { ComponentProps, FC, PropsWithChildren, memo, useCallback, useMemo } from 'react' import { tv } from 'tailwind-variants' import { isTouchDevice } from '../../libs/ua' @@ -6,7 +6,7 @@ import { UnstyledButton } from '../Button' import { FaCircleInfoIcon } from '../Icon' import { Tooltip } from '../Tooltip' -const tabItem = tv({ +const classNameGenerator = tv({ slots: { wrapper: [ 'smarthr-ui-TabItem', @@ -76,6 +76,7 @@ export const TabItem: FC = ({ if (rest.disabled && disabledDetail) { const Icon = disabledDetail.icon || + return ( = ({ className, ...rest }) => { - const { wrapperStyle, labelStyle, suffixStyle } = useMemo(() => { - const { wrapper, label, suffixWrapper } = tabItem({ isTouchDevice }) + const classNames = useMemo(() => { + const { wrapper, label, suffixWrapper } = classNameGenerator({ isTouchDevice }) + return { - wrapperStyle: wrapper({ className }), - labelStyle: label(), - suffixStyle: suffixWrapper(), + wrapper: wrapper({ className }), + label: label(), + suffixWrapper: suffixWrapper(), } }, [className]) + const actualOnClick = useCallback( + (e: React.MouseEvent) => onClick(e.currentTarget.value), + [onClick], + ) + return ( onClick(id)} + className={classNames.wrapper} + onClick={actualOnClick} > - {children} - {suffix && {suffix}} + {children} + {suffix} ) } + +const TabLabel = memo>(({ children, className }) => ( + {children} +)) +const TabButtonSuffix = memo>( + ({ children, className }) => children && {children}, +)