From 33a85dbe02e31f5cfcc637e38f92bf6045418ea0 Mon Sep 17 00:00:00 2001 From: metacode22 Date: Fri, 6 Jun 2025 20:48:05 +0900 Subject: [PATCH 1/2] feat(SwitchCase): add support for symbol values in case rendering --- src/components/SwitchCase/SwitchCase.spec.tsx | 17 +++++++++++++++++ src/components/SwitchCase/SwitchCase.tsx | 13 +++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/components/SwitchCase/SwitchCase.spec.tsx b/src/components/SwitchCase/SwitchCase.spec.tsx index 4dfb9d09..7e72ad49 100644 --- a/src/components/SwitchCase/SwitchCase.spec.tsx +++ b/src/components/SwitchCase/SwitchCase.spec.tsx @@ -60,6 +60,23 @@ describe('SwitchCase', () => { expect(screen.getByText('One')).toBeInTheDocument(); }); + it('should render correct component for symbol value', () => { + const symbolA = Symbol('symbol-a'); + const symbolB = Symbol('symbol-b'); + + render( +
Symbol A Component
, + [symbolB]: () =>
Symbol B Component
, + }} + /> + ); + + expect(screen.getByText('Symbol A Component')).toBeInTheDocument(); + }); + it('should render default component when case not found', () => { const getStringValue = () => { const value = 'c'; diff --git a/src/components/SwitchCase/SwitchCase.tsx b/src/components/SwitchCase/SwitchCase.tsx index c266ba38..af7aba0e 100644 --- a/src/components/SwitchCase/SwitchCase.tsx +++ b/src/components/SwitchCase/SwitchCase.tsx @@ -1,9 +1,6 @@ import { ReactElement } from 'react'; -type StringifiedValue = - | (T extends boolean ? 'true' | 'false' : never) - | (T extends number ? `${T}` : never) - | (T extends string ? T : never); +type StringifiedValue = (T extends boolean ? 'true' | 'false' : never) | (T extends PropertyKey ? T : never); type Props = { value: Case; @@ -45,6 +42,10 @@ type Props = { * } */ export function SwitchCase({ value, caseBy, defaultComponent = () => null }: Props): ReactElement | null { - const stringifiedValue = String(value) as StringifiedValue; - return (caseBy[stringifiedValue] ?? defaultComponent)(); + if (typeof value === 'boolean') { + const stringifiedValue = String(value) as StringifiedValue; + return (caseBy[stringifiedValue] ?? defaultComponent)(); + } + + return (caseBy[value as StringifiedValue] ?? defaultComponent)(); } From c37091c0ed951b00028f523f3081c91f55a08cf6 Mon Sep 17 00:00:00 2001 From: metacode22 Date: Fri, 6 Jun 2025 20:53:11 +0900 Subject: [PATCH 2/2] test(SwitchCase): add test for rendering nothing with unmatched boolean case --- src/components/SwitchCase/SwitchCase.spec.tsx | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/components/SwitchCase/SwitchCase.spec.tsx b/src/components/SwitchCase/SwitchCase.spec.tsx index 7e72ad49..f146901b 100644 --- a/src/components/SwitchCase/SwitchCase.spec.tsx +++ b/src/components/SwitchCase/SwitchCase.spec.tsx @@ -128,6 +128,22 @@ describe('SwitchCase', () => { expect(screen.getByText('True Case')).toBeInTheDocument(); }); + it('should render nothing when boolean value has no matching case', () => { + const value = false; + + const { container } = render( +
True Case
, + }} + defaultComponent={() => null} + /> + ); + + expect(container.firstChild).toBeNull(); + }); + it('should render nothing when no matching case and default is null', () => { const getValue = () => { const value = undefined;