diff --git a/packages/smarthr-ui/src/components/Badge/Badge.tsx b/packages/smarthr-ui/src/components/Badge/Badge.tsx index 972b424487..061806dbef 100644 --- a/packages/smarthr-ui/src/components/Badge/Badge.tsx +++ b/packages/smarthr-ui/src/components/Badge/Badge.tsx @@ -92,42 +92,62 @@ const badge = tv({ ], }) -export const Badge: React.FC = ({ +export const Badge = React.memo(({ count, showZero, ...rest }) => { + // ドット表示の場合、数値表示は無いため、早期returnする + if (rest.dot) { + return + } + + const actualCount = count && count > 0 ? count : showZero ? 0 : undefined + + // 0値を表示するでもない場合は何も表示しない + if (actualCount === undefined && !rest.children) { + return null + } + + return +}) + +const ActualBadge: React.FC> = ({ count, - overflowCount = 99, - showZero = false, - type = 'blue', - dot = false, + overflowCount, + type, + dot, children, className, - ...props + ...rest }) => { - const actualCount = count && count > 0 ? count : showZero ? 0 : undefined + // HINT: boolean化することでmemoが有効になる可能性を高くする + const withChildren = !!children + const styles = useMemo(() => { + const { wrapper, pill, dotElement } = badge({ color: type || 'blue', withChildren }) - const { wrapperStyle, pillStyle, dotStyle } = useMemo(() => { - const { wrapper, pill, dotElement } = badge({ color: type, withChildren: !!children }) return { - wrapperStyle: wrapper({ className }), - pillStyle: pill(), - dotStyle: dotElement(), + wrapper: wrapper({ className }), + pill: pill(), + dot: dotElement(), } - }, [children, className, type]) - - // ドット表示でもなく、0値を表示するでもない場合は何も表示しない - if (!dot && !children && actualCount === undefined) return null + }, [withChildren, type, className]) return ( - + {children} {dot ? ( - + ) : ( - actualCount !== undefined && ( - - {actualCount > overflowCount ? `${overflowCount}+` : actualCount} - - ) + )} ) } + +const Dot = React.memo<{ className: string }>(({ className }) => ) + +const CountText = React.memo & { className: string }>( + ({ count, overflowCount = 99, className }) => + count !== undefined && ( + + {count > overflowCount ? `${overflowCount}+` : count} + + ), +) diff --git a/packages/smarthr-ui/src/components/Balloon/Balloon.tsx b/packages/smarthr-ui/src/components/Balloon/Balloon.tsx index e6078d6e92..04f5493b18 100644 --- a/packages/smarthr-ui/src/components/Balloon/Balloon.tsx +++ b/packages/smarthr-ui/src/components/Balloon/Balloon.tsx @@ -1,4 +1,4 @@ -import React, { ComponentPropsWithoutRef, FC, PropsWithChildren, useMemo } from 'react' +import React, { ComponentPropsWithoutRef, PropsWithChildren, useMemo } from 'react' import { VariantProps, tv } from 'tailwind-variants' // HINT: trianble部分はRetinaディスプレイなどで途切れてしまう場合があるので @@ -119,17 +119,13 @@ type Props = PropsWithChildren< type ElementProps = Omit, keyof Props> -export const Balloon: FC = ({ - horizontal, - vertical, - className, - as: Component = 'div', - ...props -}) => { - const styles = useMemo( - () => balloon({ horizontal, vertical, className }), - [className, horizontal, vertical], - ) +export const Balloon = React.memo( + ({ horizontal, vertical, className, as: Component = 'div', ...props }) => { + const styles = useMemo( + () => balloon({ horizontal, vertical, className }), + [horizontal, vertical, className], + ) - return -} + return + }, +) diff --git a/packages/smarthr-ui/src/components/Balloon/useClassNames.ts b/packages/smarthr-ui/src/components/Balloon/useClassNames.ts deleted file mode 100644 index e413e3f130..0000000000 --- a/packages/smarthr-ui/src/components/Balloon/useClassNames.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { useMemo } from 'react' - -import { useClassNameGenerator } from '../../hooks/useClassNameGenerator' - -export function useClassNames() { - const generate = useClassNameGenerator('Balloon') - return useMemo( - () => ({ - wrapper: generate(), - }), - [generate], - ) -} diff --git a/packages/smarthr-ui/src/components/Base/Base.tsx b/packages/smarthr-ui/src/components/Base/Base.tsx index 7c00642d80..18e57f6db9 100644 --- a/packages/smarthr-ui/src/components/Base/Base.tsx +++ b/packages/smarthr-ui/src/components/Base/Base.tsx @@ -114,22 +114,20 @@ export const Base = forwardRef( ref, ) => { const styles = useMemo(() => { - const paddingBlock = padding instanceof Object ? padding.block : padding - const paddingInline = padding instanceof Object ? padding.inline : padding - - const overflowBlock = overflow instanceof Object ? overflow.y : overflow - const overflowInline = overflow instanceof Object ? overflow.x : overflow + const actualPadding = + padding instanceof Object ? padding : { block: padding, inline: padding } + const actualOverflow = overflow instanceof Object ? overflow : { x: overflow, y: overflow } return base({ - paddingBlock, - paddingInline, + paddingBlock: actualPadding.block, + paddingInline: actualPadding.inline, radius, - overflowBlock, - overflowInline, + overflowBlock: actualOverflow.y, + overflowInline: actualOverflow.x, layer, className, }) - }, [className, layer, overflow, padding, radius]) + }, [layer, overflow, padding, radius, className]) const Wrapper = useSectionWrapper(Component) diff --git a/packages/smarthr-ui/src/components/Base/BaseColumn/BaseColumn.tsx b/packages/smarthr-ui/src/components/Base/BaseColumn/BaseColumn.tsx index 476ecab087..07e4326a3d 100644 --- a/packages/smarthr-ui/src/components/Base/BaseColumn/BaseColumn.tsx +++ b/packages/smarthr-ui/src/components/Base/BaseColumn/BaseColumn.tsx @@ -38,5 +38,6 @@ export const BaseColumn: React.FC = ({ ...props }) => { const styles = useMemo(() => baseColumn({ bgColor, className }), [bgColor, className]) + return } diff --git a/packages/smarthr-ui/src/components/Input/CurrencyInput/CurrencyInput.tsx b/packages/smarthr-ui/src/components/Input/CurrencyInput/CurrencyInput.tsx index 54788bbd78..8015fe5dd9 100644 --- a/packages/smarthr-ui/src/components/Input/CurrencyInput/CurrencyInput.tsx +++ b/packages/smarthr-ui/src/components/Input/CurrencyInput/CurrencyInput.tsx @@ -13,7 +13,6 @@ import React, { import { Input } from '../Input' import { formatCurrency } from './currencyInputHelper' -import { useClassNames } from './useClassNames' type Props = Omit, 'type' | 'value' | 'defaultValue'> & { /** 通貨の値 */ @@ -96,8 +95,6 @@ export const CurrencyInput = forwardRef( [onBlur], ) - const classNames = useClassNames(className) - return ( ( onFocus={handleFocus} onBlur={handleBlur} ref={innerRef} - className={classNames.wrapper} + className={`smarthr-ui-CurrencyInput${className ? ` ${className}` : ''}`} /> ) }, diff --git a/packages/smarthr-ui/src/components/Input/CurrencyInput/useClassNames.ts b/packages/smarthr-ui/src/components/Input/CurrencyInput/useClassNames.ts deleted file mode 100644 index 79518911fd..0000000000 --- a/packages/smarthr-ui/src/components/Input/CurrencyInput/useClassNames.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { useMemo } from 'react' - -import { useClassNameGenerator } from '../../../hooks/useClassNameGenerator' - -import { CurrencyInput } from './CurrencyInput' - -export function useClassNames(className: string) { - const generate = useClassNameGenerator(CurrencyInput.displayName || 'CurrencyInput') - - return useMemo( - () => ({ - wrapper: `${className} ${generate()}`, - }), - [generate, className], - ) -} diff --git a/packages/smarthr-ui/src/hooks/useClassNameGenerator.ts b/packages/smarthr-ui/src/hooks/useClassNameGenerator.ts deleted file mode 100644 index b1b8a360bd..0000000000 --- a/packages/smarthr-ui/src/hooks/useClassNameGenerator.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { useCallback } from 'react' - -const PREFIX = 'smarthr-ui' - -export function useClassNameGenerator(componentName: string) { - return useCallback( - (partName?: string) => { - if (!partName) { - return `${PREFIX}-${componentName}` - } - return `${PREFIX}-${componentName}-${partName}` - }, - [componentName], - ) -}