diff --git a/packages/components/src/autocomplete/index.tsx b/packages/components/src/autocomplete/index.tsx index 7a75be8eca5c84..55e81d08c9797f 100644 --- a/packages/components/src/autocomplete/index.tsx +++ b/packages/components/src/autocomplete/index.tsx @@ -40,35 +40,7 @@ import type { UseAutocompleteProps, WPCompleter, } from './types'; - -const getNodeText = ( node: React.ReactNode ): string => { - if ( node === null ) { - return ''; - } - - switch ( typeof node ) { - case 'string': - case 'number': - return node.toString(); - break; - case 'boolean': - return ''; - break; - case 'object': { - if ( node instanceof Array ) { - return node.map( getNodeText ).join( '' ); - } - if ( 'props' in node ) { - return getNodeText( node.props.children ); - } - break; - } - default: - return ''; - } - - return ''; -}; +import getNodeText from '../utils/get-node-text'; const EMPTY_FILTERED_OPTIONS: KeyedOption[] = []; diff --git a/packages/components/src/utils/get-node-text.ts b/packages/components/src/utils/get-node-text.ts new file mode 100644 index 00000000000000..eb3bc045c3a653 --- /dev/null +++ b/packages/components/src/utils/get-node-text.ts @@ -0,0 +1,24 @@ +const getNodeText = ( node: React.ReactNode ): string => { + if ( node === null ) { + return ''; + } + + switch ( typeof node ) { + case 'string': + case 'number': + return node.toString(); + case 'object': { + if ( node instanceof Array ) { + return node.map( getNodeText ).join( '' ); + } + if ( 'props' in node ) { + return getNodeText( node.props.children ); + } + return ''; + } + default: + return ''; + } +}; + +export default getNodeText; diff --git a/packages/components/src/utils/test/get-node-text.js b/packages/components/src/utils/test/get-node-text.js new file mode 100644 index 00000000000000..903b6158f74b27 --- /dev/null +++ b/packages/components/src/utils/test/get-node-text.js @@ -0,0 +1,37 @@ +/** + * Internal dependencies + */ +import getNodeText from '../get-node-text'; + +describe( 'getNodeText', () => { + it( 'should return an empty string for null', () => { + expect( getNodeText( null ) ).toBe( '' ); + } ); + + it( 'should return the string representation of a string node', () => { + expect( getNodeText( 'Hello' ) ).toBe( 'Hello' ); + } ); + + it( 'should return the string representation of a number node', () => { + expect( getNodeText( 123 ) ).toBe( '123' ); + } ); + + it( 'should return an empty string for a boolean node', () => { + expect( getNodeText( true ) ).toBe( '' ); + } ); + + it( 'should concatenate text from an array of nodes', () => { + expect( getNodeText( [ 'Hello', ' ', 'World' ] ) ).toBe( + 'Hello World' + ); + } ); + + it( 'should return text from a React element with children', () => { + const element = ( +