2020-07-14 16:04:58 +07:00
|
|
|
import React from 'react'
|
2020-07-14 14:39:07 +07:00
|
|
|
|
2020-07-14 16:04:58 +07:00
|
|
|
const BaseContext = React.createContext({})
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component : BaseContextProvider
|
|
|
|
*
|
|
|
|
* @author sl
|
|
|
|
* @time 13/2/20
|
|
|
|
* @param {{}}
|
|
|
|
* @constructor
|
|
|
|
*
|
|
|
|
*/
|
2020-07-15 08:58:13 +07:00
|
|
|
class BaseContextProvider<PROPS, STATE> extends React.Component<PROPS, STATE> {
|
2020-07-14 16:04:58 +07:00
|
|
|
getContextReturnValue() {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
|
|
|
getContext() {
|
|
|
|
return BaseContext
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const contextReturn = this.getContextReturnValue()
|
|
|
|
const Context = this.getContext()
|
|
|
|
const { children } = this.props
|
|
|
|
|
|
|
|
return <Context.Provider value={contextReturn}>{children}</Context.Provider>
|
|
|
|
}
|
2020-07-14 14:39:07 +07:00
|
|
|
}
|
|
|
|
|
2020-07-14 16:04:58 +07:00
|
|
|
export const baseContextWrap = (Provider: any) => {
|
|
|
|
return (Component: any) => (props: any) => (
|
|
|
|
<Provider children={<Component {...props} />} />
|
|
|
|
)
|
2020-07-14 14:39:07 +07:00
|
|
|
}
|
2020-07-14 16:04:58 +07:00
|
|
|
|
|
|
|
export function baseUseContext(context: any) {
|
|
|
|
return function () {
|
|
|
|
return React.useContext(context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function baseWithContext(context: any, contextPassPropKey: any) {
|
|
|
|
const Consumer = context.Consumer
|
|
|
|
return (Component: any) => (props: any) => (
|
|
|
|
<Consumer>
|
|
|
|
{(contextValue: any) => (
|
|
|
|
<Component {...{ ...props, [contextPassPropKey]: contextValue }} />
|
|
|
|
)}
|
|
|
|
</Consumer>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default BaseContextProvider
|