Skip to content

feat(SwitchCase): add support for symbol type #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/components/SwitchCase/SwitchCase.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<SwitchCase
value={symbolA}
caseBy={{
[symbolA]: () => <div>Symbol A Component</div>,
[symbolB]: () => <div>Symbol B Component</div>,
}}
/>
);

expect(screen.getByText('Symbol A Component')).toBeInTheDocument();
});

it('should render default component when case not found', () => {
const getStringValue = () => {
const value = 'c';
Expand Down Expand Up @@ -111,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(
<SwitchCase
value={value}
caseBy={{
true: () => <div>True Case</div>,
}}
defaultComponent={() => null}
/>
);

expect(container.firstChild).toBeNull();
});

it('should render nothing when no matching case and default is null', () => {
const getValue = () => {
const value = undefined;
Expand Down
13 changes: 7 additions & 6 deletions src/components/SwitchCase/SwitchCase.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { ReactElement } from 'react';

type StringifiedValue<T> =
| (T extends boolean ? 'true' | 'false' : never)
| (T extends number ? `${T}` : never)
| (T extends string ? T : never);
type StringifiedValue<T> = (T extends boolean ? 'true' | 'false' : never) | (T extends PropertyKey ? T : never);

type Props<Case> = {
value: Case;
Expand Down Expand Up @@ -45,6 +42,10 @@ type Props<Case> = {
* }
*/
export function SwitchCase<Case>({ value, caseBy, defaultComponent = () => null }: Props<Case>): ReactElement | null {
const stringifiedValue = String(value) as StringifiedValue<Case>;
return (caseBy[stringifiedValue] ?? defaultComponent)();
if (typeof value === 'boolean') {
const stringifiedValue = String(value) as StringifiedValue<Case>;
return (caseBy[stringifiedValue] ?? defaultComponent)();
}

return (caseBy[value as StringifiedValue<Case>] ?? defaultComponent)();
}