-
Notifications
You must be signed in to change notification settings - Fork 14
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
feat(#100): SwitchCase 컴포넌트 추가 #101
Conversation
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #101 +/- ##
==========================================
+ Coverage 82.60% 82.82% +0.21%
==========================================
Files 66 67 +1
Lines 552 559 +7
Branches 115 118 +3
==========================================
+ Hits 456 463 +7
Misses 84 84
Partials 12 12
|
export default function SwitchCase<Condition extends string | number>({ | ||
condition, | ||
cases, | ||
defaultCase = null, | ||
}: Props<Condition>) { | ||
try { | ||
if (condition === null || condition === undefined) { | ||
return defaultCase; | ||
} | ||
|
||
return cases[condition] ?? defaultCase; | ||
} catch { | ||
return defaultCase; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export default function SwitchCase<Condition extends string | number>({ | |
condition, | |
cases, | |
defaultCase = null, | |
}: Props<Condition>) { | |
try { | |
if (condition === null || condition === undefined) { | |
return defaultCase; | |
} | |
return cases[condition] ?? defaultCase; | |
} catch { | |
return defaultCase; | |
} | |
} | |
import { Nullable } from '@modern-kit/types'; | |
type Component = Nullable<JSX.Element>; | |
// @modern-kit/types의 Nullable을 재활용해도 될 것 같아요! | |
// Props말고 {ComponentName}Props로 변경해주세요! | |
interface SwitchCaseProps<Condition extends string | number> { | |
condition: Condition | null | undefined; | |
cases: Partial<Record<Condition, Component>>; | |
defaultComponent?: Component; // 타입명이라던가, 역할을 봤을 때 defaultComponent가 더욱 의미가 적절한 것 같습니다! | |
} | |
// named export와 default export 혼합은 권장되지 않은데 현재 대부분의 모듈들이 named export로 구성되어 있습니다. | |
export const SwitchCase = <Condition extends string | number>({ | |
condition, | |
cases, | |
defaultComponent = null, | |
}: SwitchCaseProps<Condition>) => { | |
try { | |
if (condition == null) { // == null로 두 조건식을 모두 검증할 수 있을 것 같습니다 | |
return defaultComponent; | |
} | |
return cases[condition] ?? defaultComponent; | |
} catch { | |
return defaultComponent; | |
} | |
}; |
/** | ||
* @example | ||
* ```tsx | ||
* const [testValue, setTestValue] = useState(0); | ||
* | ||
* <SwitchCase | ||
* condition={testValue} | ||
* cases={{ | ||
* 0: <Comp1 />, | ||
* 1: <Comp2 />, | ||
* 2: <Comp3 /> | ||
* }} | ||
* defaultCase={<Comp4 />} | ||
* /> | ||
* ``` | ||
* | ||
* @param props.condition - 비교 조건 | ||
* @param props.cases - 조건에 부합하면 렌더링될 컴포넌트 | ||
* @param props.defaultCase - 조건에 부합하는 값이 없으면 렌더링할 컴포넌트 | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 이런 주석들은 현재 코드에서 제외하고 문서로 작성하면 좋을 것 같습니다
import { render } from '@testing-library/react'; | ||
import SwitchCase from '.'; | ||
|
||
describe('SwitchCase', () => { | ||
it('SwitchCase는 조건을 받을 수 있어야 한다.', () => { | ||
const { container } = render( | ||
<SwitchCase condition={0 as number} cases={{ 0: <div>1</div> }} /> | ||
); | ||
expect(container.querySelector('div')).toBeInTheDocument(); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import { render } from '@testing-library/react'; | |
import SwitchCase from '.'; | |
describe('SwitchCase', () => { | |
it('SwitchCase는 조건을 받을 수 있어야 한다.', () => { | |
const { container } = render( | |
<SwitchCase condition={0 as number} cases={{ 0: <div>1</div> }} /> | |
); | |
expect(container.querySelector('div')).toBeInTheDocument(); | |
}); | |
}); | |
import { screen } from '@testing-library/react'; | |
import { renderSetup } from '../../utils/test/renderSetup'; | |
import { SwitchCase } from '.'; | |
describe('SwitchCase', () => { | |
it('should render the component corresponding to the condition passed through props.', () => { | |
renderSetup( | |
<SwitchCase | |
condition={0 as number} | |
cases={{ | |
0: <button>Test Button 0</button>, | |
1: <button>Test Button 1</button>, | |
}} | |
/> | |
); | |
const testButtonZero = screen.queryByRole('button', { | |
name: 'Test Button 0', | |
}); | |
const testButtonOne = screen.queryByRole('button', { | |
name: 'Test Button 1', | |
}); | |
expect(testButtonZero).toBeInTheDocument(); | |
expect(testButtonOne).not.toBeInTheDocument(); | |
}); | |
it('should render the component corresponding to the condition passed through props.', () => { | |
renderSetup( | |
<SwitchCase | |
condition={null} | |
defaultComponent={<p role="paragraph">Default Text</p>} | |
cases={{}} | |
/> | |
); | |
const testText = screen.queryByRole('paragraph'); | |
expect(testText).toBeInTheDocument(); | |
}); | |
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Overview
SwitchCase 컴포넌트 추가
PR Checklist
Contributing Guide