-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ui/system): createContext 함수의 인자를 간략화, 반환값 Provider, useCont…
…ext이 포함된 배열로 변경 (#322) * refactor(ui/system): createContext 함수의 인자를 간략화, 반환값 Provider, useContext이 포함된 배열로 변경 * refactor(ui/components): createContext 변경사항 반영
- Loading branch information
Showing
13 changed files
with
83 additions
and
78 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 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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,43 @@ | ||
import { | ||
type Context, | ||
type Provider, | ||
type ReactNode, | ||
createContext as createReactContext, | ||
useMemo, | ||
useContext as useReactContext, | ||
} from 'react'; | ||
|
||
type CreatContextOptions<T> = { | ||
name?: string; | ||
hookName?: string; | ||
providerName?: string; | ||
errorMessage?: string; | ||
defaultValue?: T; | ||
}; | ||
|
||
type CreateContextReturn<T> = [Provider<T>, () => T, Context<T>]; | ||
export function createContext<T extends object | null>( | ||
rootComponentName: string, | ||
defaultValue?: T, | ||
) { | ||
const Context = createReactContext<T | undefined>(defaultValue); | ||
|
||
function getErrorMessage(hook: string, provider: string) { | ||
return `${hook} returned \`undefined\`. Seems you forgot to wrap component within ${provider}`; | ||
} | ||
function Provider(props: T & { children: ReactNode }) { | ||
const { children, ...context } = props; | ||
|
||
export function createContext<T>(options: CreatContextOptions<T>) { | ||
const { | ||
name, | ||
hookName = 'useContext', | ||
providerName = 'Provider', | ||
errorMessage, | ||
defaultValue, | ||
} = options; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
const value = useMemo(() => context, Object.values(context)) as T; | ||
|
||
const Context = createReactContext<T | undefined>(defaultValue); | ||
return <Context.Provider value={value}>{children}</Context.Provider>; | ||
} | ||
|
||
Context.displayName = name; | ||
Provider.displayName = rootComponentName + 'Provider'; | ||
|
||
function useContext() { | ||
function useContext(consumerName: string) { | ||
const context = useReactContext(Context); | ||
|
||
if (!context) { | ||
const error = new Error( | ||
errorMessage ?? getErrorMessage(hookName, providerName), | ||
); | ||
if (context) return context; | ||
|
||
if (defaultValue !== undefined) return defaultValue; | ||
|
||
error.name = 'ContextError'; | ||
Error.captureStackTrace(error, useContext); | ||
const error = new Error( | ||
`\`${consumerName}\` must be used within \`${rootComponentName}\``, | ||
); | ||
|
||
throw error; | ||
} | ||
error.name = 'ContextError'; | ||
Error.captureStackTrace(error, useContext); | ||
|
||
return context; | ||
throw error; | ||
} | ||
|
||
return [Context.Provider, useContext, Context] as CreateContextReturn<T>; | ||
return [Provider, useContext] as const; | ||
} |