mirror of
https://github.com/zhigang1992/react.git
synced 2026-04-26 23:04:55 +08:00
35 lines
871 B
TypeScript
35 lines
871 B
TypeScript
import React, { useMemo, useState } from 'react'
|
|
import withDefaults from 'components/utils/with-defaults'
|
|
import { ConfigContext } from 'lib/states/config-context'
|
|
|
|
interface Props {
|
|
onChange?: Function
|
|
}
|
|
|
|
const defaultProps = {
|
|
}
|
|
|
|
export type ConfigProviderProps = Props & typeof defaultProps
|
|
|
|
const ConfigProvider: React.FC<React.PropsWithChildren<ConfigProviderProps>> = React.memo(({
|
|
onChange, children,
|
|
}) => {
|
|
const [shouldScroll, setShouldScroll] = useState<boolean>(false)
|
|
const updateShouldScroll = (next: boolean) => {
|
|
setShouldScroll(next)
|
|
}
|
|
const initialValue = useMemo(() => ({
|
|
onChange,
|
|
shouldScroll,
|
|
updateShouldScroll,
|
|
}), [onChange, shouldScroll])
|
|
|
|
return (
|
|
<ConfigContext.Provider value={initialValue}>
|
|
{children}
|
|
</ConfigContext.Provider>
|
|
)
|
|
})
|
|
|
|
export default withDefaults(ConfigProvider, defaultProps)
|