-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compose: Try typing
withState
in a naïve way
- Loading branch information
1 parent
506d074
commit 79f798e
Showing
4 changed files
with
79 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
// eslint-disable-next-line no-restricted-imports | ||
import type { ComponentType } from 'react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import createHigherOrderComponent from '../../utils/create-higher-order-component'; | ||
// eslint-disable-next-line no-duplicate-imports | ||
import type { HigherOrderComponent } from '../../utils/create-higher-order-component'; | ||
|
||
/** | ||
* A Higher Order Component used to provide and manage internal component state | ||
* via props. | ||
* | ||
* @param initialState Optional initial state of the component. | ||
* | ||
* @return A higher order component wrapper accepting a component that takes the state props + its own props + `setState` and returning a component that only accepts the own props. | ||
*/ | ||
export default function withState< | ||
TProps extends object, | ||
TStateProps extends object | ||
>( | ||
initialState: TStateProps = {} as TStateProps | ||
): HigherOrderComponent< | ||
TProps & | ||
TStateProps & { | ||
setState: Component< TProps, TStateProps >[ 'setState' ]; | ||
}, | ||
TProps | ||
> { | ||
return createHigherOrderComponent( | ||
( | ||
OriginalComponent: ComponentType< | ||
TProps & | ||
TStateProps & { | ||
setState: Component< | ||
TProps, | ||
TStateProps | ||
>[ 'setState' ]; | ||
} | ||
> | ||
) => { | ||
return class WrappedComponent extends Component< | ||
TProps, | ||
TStateProps | ||
> { | ||
constructor( props: any ) { | ||
super( props ); | ||
|
||
this.setState = this.setState.bind( this ); | ||
|
||
this.state = initialState; | ||
} | ||
|
||
render() { | ||
return ( | ||
<OriginalComponent | ||
{ ...( this.props as TProps ) } | ||
{ ...( this.state as TStateProps ) } | ||
setState={ this.setState } | ||
/> | ||
); | ||
} | ||
}; | ||
}, | ||
'withState' | ||
); | ||
} |
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