-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* docs: SwitchCase 문서 추가 * docs: When컴포넌트 props 수정
- Loading branch information
Showing
4 changed files
with
98 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@modern-kit/react': minor | ||
--- | ||
|
||
#100 61d6df3 Thanks @soonki-98! - feat: SwitchCase Component 추가 및 When fallback props 추가 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { useState } from 'react'; | ||
import { SwitchCase } from '@modern-kit/react'; | ||
|
||
# SwitchCase | ||
|
||
`Switch` 문을 컴포넌트 형태로 사용할 수 있는 컴포넌트입니다. | ||
|
||
`cases`에는 `key(condition):value(component)` 형태의 프로퍼티를 갖는 객체를 넘겨주고, `condition` props에 매칭되는 `value(컴포넌트)`를 렌더링합니다. | ||
|
||
`cases`에 매칭되는 `condition`이 없을 때 임시로 보여줄 수 있는 `defaultCaseComponent`를 `props`로 넘겨줄 수 있습니다. | ||
|
||
<br /> | ||
|
||
## Interface | ||
```tsx | ||
type Component = Nullable<React.ReactNode>; | ||
|
||
interface SwitchCaseProps<Condition extends string | number> { | ||
condition: Condition | null | undefined; | ||
cases: Partial<Record<Condition, Component>>; | ||
defaultCaseComponent?: Component; | ||
} | ||
|
||
const SwitchCase: <Condition extends string | number>({ | ||
condition, | ||
cases, | ||
defaultCaseComponent, | ||
}: SwitchCaseProps<Condition>) => JSX.Element; | ||
``` | ||
|
||
## Usage | ||
|
||
```tsx | ||
import { SwitchCase } from '@modern-kit/react'; | ||
|
||
|
||
const Example = () => { | ||
const [condition, setCondition] = useState(1); | ||
|
||
return ( | ||
<div> | ||
<div> | ||
<button onClick={() => setCondition(1)}>Change to case 1</button> | ||
<button onClick={() => setCondition(2)}>Change to case 2</button> | ||
<button onClick={() => setCondition(3)}>Change to case 3</button> | ||
</div> | ||
|
||
<SwitchCase | ||
condition={condition} | ||
cases={{ | ||
1: <h3>Case No.1</h3>, | ||
2: <h3>Case No.2</h3>, | ||
3: <h3>Case No.3</h3>, | ||
}} | ||
/> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
export const Example = () => { | ||
const [condition, setCondition] = useState(1); | ||
return ( | ||
<div> | ||
<div> | ||
<button onClick={() => setCondition(1)}>Change to case 1</button> | ||
<button onClick={() => setCondition(2)}>Change to case 2</button> | ||
<button onClick={() => setCondition(3)}>Change to case 3</button> | ||
</div> | ||
|
||
<SwitchCase | ||
condition={condition} | ||
cases={{ | ||
1: <h3>Case No.1</h3>, | ||
2: <h3>Case No.2</h3>, | ||
3: <h3>Case No.3</h3>, | ||
}} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
<Example /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters