Skip to content

Commit

Permalink
compose: Try typing withState in a naïve way
Browse files Browse the repository at this point in the history
  • Loading branch information
sarayourfriend committed May 28, 2021
1 parent 506d074 commit 79f798e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 43 deletions.
4 changes: 2 additions & 2 deletions packages/compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,11 @@ via props.

_Parameters_

- _initialState_ `?Object`: Optional initial state of the component.
- _initialState_ `TStateProps`: Optional initial state of the component.

_Returns_

- `WPComponent`: Wrapped component.
- `HigherOrderComponent< TProps & TStateProps & { setState: Component[ 'setState' ]; }, TProps >`: 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.


<!-- END TOKEN(Autogenerated API docs) -->
Expand Down
41 changes: 0 additions & 41 deletions packages/compose/src/higher-order/with-state/index.js

This file was deleted.

76 changes: 76 additions & 0 deletions packages/compose/src/higher-order/with-state/index.tsx
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'
);
}
1 change: 1 addition & 0 deletions packages/compose/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"src/higher-order/pure/**/*",
"src/higher-order/with-instance-id/**/*",
"src/higher-order/with-global-events/**/*",
"src/higher-order/with-state/**/*",
"src/hooks/use-async-list/**/*",
"src/hooks/use-constrained-tabbing/**/*",
"src/hooks/use-debounce/**/*",
Expand Down

0 comments on commit 79f798e

Please sign in to comment.