diff --git a/packages/components/src/ui/utils/create-component.tsx b/packages/components/src/ui/utils/create-component.tsx deleted file mode 100644 index 8e451cffef11c..0000000000000 --- a/packages/components/src/ui/utils/create-component.tsx +++ /dev/null @@ -1,58 +0,0 @@ -/** - * External dependencies - */ -// eslint-disable-next-line no-restricted-imports -import type { Ref } from 'react'; -import type { As } from 'reakit-utils/types'; - -/** - * Internal dependencies - */ -import { contextConnect } from '../context'; -import type { - WordPressComponentProps, - WordPressComponentFromProps, -} from '../context'; -import { View } from '../../view'; - -interface Options< - A extends As, - P extends WordPressComponentProps< {}, A, any > -> { - as: A; - name: string; - useHook: ( props: P ) => any; - memo?: boolean; -} - -/** - * Factory that creates a React component from a hook - * - * @param options - * @param options.as The element to render for the component. - * @param options.name The name of the component. - * @param options.useHook The hook to use for the component - * @param options.memo Whether to memo the component. - * @return A polymorphic component that uses the hook to process props. - */ -export const createComponent = < - A extends As, - P extends WordPressComponentProps< {}, A, any > ->( { - as, - name, - useHook, - memo = false, -}: Options< A, P > ): WordPressComponentFromProps< P > => { - function Component( props: P, forwardedRef: Ref< any > ) { - const otherProps = useHook( props ); - - return ( - - ); - } - - Component.displayName = name; - - return contextConnect( Component, name, { memo } ); -}; diff --git a/packages/components/src/ui/utils/index.js b/packages/components/src/ui/utils/index.js index 26e05b9d84d7d..c6f4adde96a52 100644 --- a/packages/components/src/ui/utils/index.js +++ b/packages/components/src/ui/utils/index.js @@ -1,2 +1 @@ export { getOptimalTextShade } from './colors'; -export { createComponent } from './create-component'; diff --git a/packages/components/src/ui/utils/test/create-component.js b/packages/components/src/ui/utils/test/create-component.js deleted file mode 100644 index 970a774badf29..0000000000000 --- a/packages/components/src/ui/utils/test/create-component.js +++ /dev/null @@ -1,70 +0,0 @@ -/** - * External dependencies - */ -import { render } from '@testing-library/react'; - -/** - * WordPress dependencies - */ -import { createRef } from '@wordpress/element'; - -/** - * Internal dependencies - */ -import { createComponent } from '../create-component'; - -describe( 'createComponent', () => { - /** - * @param {import('../context').WordPressComponentProps<{}, 'output'>} props - */ - const useHook = ( props ) => ( { ...props, 'data-hook-test-prop': true } ); - const name = 'Output'; - const MemoizedOutput = createComponent( { - as: 'output', - name, - useHook, - memo: true, - } ); - const Output = createComponent( { - as: 'output', - name, - useHook, - memo: false, - } ); - - it( 'should create a output component', () => { - const { container } = render( - Example output - ); - - expect( container.firstChild.tagName ).toBe( 'OUTPUT' ); - expect( container.firstChild.innerHTML ).toBe( 'Example output' ); - } ); - - it( 'should create a memoized, ref-forwarded component', () => { - expect( MemoizedOutput.$$typeof ).toEqual( Symbol.for( 'react.memo' ) ); - const ref = createRef(); - const wrapper = render( - Example output - ); - - expect( ref.current ).toEqual( wrapper.container.firstChild ); - } ); - - it( 'should create a non-memoized ref-forwarded ouput component', () => { - expect( Output.$$typeof ).toEqual( Symbol.for( 'react.forward_ref' ) ); - const ref = createRef(); - const wrapper = render( Example output ); - - expect( ref.current ).toEqual( wrapper.container.firstChild ); - } ); - - it( 'should apply the hook to the component props', () => { - const { container } = render( Example output ); - expect( container.firstChild.dataset.hookTestProp ).toBe( 'true' ); - } ); - - it( 'should connect the component to its context', () => { - expect( MemoizedOutput.__contextSystemKey__ ).toContain( 'Output' ); - } ); -} );