-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor Switch components. #50
- Loading branch information
1 parent
976b4c1
commit e750f70
Showing
6 changed files
with
45 additions
and
95 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
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,26 +1,40 @@ | ||
import { type FC, type PropsWithChildren } from 'react'; | ||
import { DispatchSwitch, Context, useSwitch, useSwitchStore } from './switch.store'; | ||
|
||
export * from './case'; | ||
import type { FC, PropsWithChildren } from 'react'; | ||
|
||
type Child = typeof Case | typeof Default; | ||
export const Switch: FC<PropsWithChildren<{}>> = ({ children }) => { | ||
const [state, dispatch] = useSwitch(); | ||
return ( | ||
<Context.Provider value={state}> | ||
<DispatchSwitch.Provider value={dispatch}>{children}</DispatchSwitch.Provider> | ||
<Render /> | ||
</Context.Provider> | ||
); | ||
}; | ||
let matchChild: Child | null = null; | ||
let defaultCase: typeof Default | null = null; | ||
|
||
const Render = () => { | ||
const state = useSwitchStore(); | ||
let activeKey; | ||
for (var key in state.active) { | ||
if (state.active[key] === true) { | ||
activeKey = key; | ||
break; | ||
const childs = Array.isArray(children) ? children : [children]; | ||
childs.some((child) => { | ||
if (!defaultCase && child && child.type === Default) { | ||
defaultCase = child; | ||
} | ||
} | ||
return state[activeKey ?? 'default'] ?? null; | ||
if (child && child.type === Case) { | ||
const { condition } = child.props; | ||
const conditionIsTrue = Boolean(condition); | ||
if (conditionIsTrue) { | ||
matchChild = child; | ||
return true; | ||
} | ||
} | ||
return false; | ||
}); | ||
return <>{matchChild ?? defaultCase ?? null}</>; | ||
}; | ||
|
||
type TagType = React.ElementType | keyof JSX.IntrinsicElements; | ||
interface CaseElementProps<T extends TagType> { | ||
as?: T; | ||
readonly condition?: boolean; | ||
} | ||
|
||
export type CaseProps<T extends TagType> = CaseElementProps<T> & React.ComponentPropsWithoutRef<T>; | ||
|
||
export const Case = <T extends TagType>(props: CaseProps<T>) => { | ||
const { children, condition, as: Comp, ...reset } = props; | ||
const Elm = Comp as TagType; | ||
return Elm ? <Elm {...reset}>{children}</Elm> : children; | ||
}; | ||
|
||
export const Default = <T extends TagType>(props: CaseProps<T>) => <Case {...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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
declare module '@uiw/react-only-when/switch' { | ||
import { FC, PropsWithChildren } from 'react'; | ||
export const Switch: FC<PropsWithChildren<{}>>; | ||
export interface CaseProps { | ||
export declare const Switch: <T extends TagType>({ children }: PropsWithChildren) => null; | ||
type TagType = React.ElementType | keyof JSX.IntrinsicElements; | ||
interface CaseElementProps<T extends TagType> { | ||
as?: T; | ||
readonly condition?: boolean; | ||
} | ||
export const Case: FC<PropsWithChildren<CaseProps>>; | ||
export const Default: FC<PropsWithChildren>; | ||
export type CaseProps<T extends TagType> = CaseElementProps<T> & React.ComponentPropsWithoutRef<T>; | ||
export declare const Case: <T extends TagType>(props: CaseProps<T>) => any; | ||
export declare const Default: <T extends TagType>(props: CaseProps<T>) => any; | ||
} |