Skip to content

Commit

Permalink
Change to not use withSafeTimeout. ref: #19109
Browse files Browse the repository at this point in the history
  • Loading branch information
torounit committed Jun 2, 2022
1 parent 513e8fa commit 5911062
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
18 changes: 13 additions & 5 deletions packages/components/src/form-token-field/suggestions-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import { withSafeTimeout, useRefEffect } from '@wordpress/compose';
import { useRefEffect } from '@wordpress/compose';

/**
* Internal dependencies
Expand All @@ -21,7 +21,7 @@ const handleMouseDown = ( e ) => {
e.preventDefault();
};

export const SuggestionsList = withSafeTimeout( function SuggestionsList( {
export function SuggestionsList( {
selectedIndex,
scrollIntoView,
match = '',
Expand All @@ -30,14 +30,16 @@ export const SuggestionsList = withSafeTimeout( function SuggestionsList( {
suggestions = [],
displayTransform,
instanceId,
setTimeout,
}: SuggestionsListProps ) {
const [ scrollingIntoView, setScrollingIntoView ] = useState( false );

const listRef = useRefEffect< HTMLUListElement >(
( listNode ) => {
// only have to worry about scrolling selected suggestion into view
// when already expanded.
const { ownerDocument } = listNode;
const { defaultView } = ownerDocument;
let id;
if (
selectedIndex > -1 &&
scrollIntoView &&
Expand All @@ -47,10 +49,16 @@ export const SuggestionsList = withSafeTimeout( function SuggestionsList( {
scrollView( listNode.children[ selectedIndex ], listNode, {
onlyScrollIfNeeded: true,
} );
setTimeout( () => {
id = defaultView.setTimeout( () => {
setScrollingIntoView( false );
}, 100 );
}

return () => {
if ( id !== undefined ) {
defaultView.clearTimeout( id );
}
};
},
[ selectedIndex, scrollIntoView ]
);
Expand Down Expand Up @@ -146,6 +154,6 @@ export const SuggestionsList = withSafeTimeout( function SuggestionsList( {
} ) }
</ul>
);
} );
}

export default SuggestionsList;
2 changes: 0 additions & 2 deletions packages/components/src/form-token-field/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ export interface SuggestionsListProps< T = Suggestion > {
suggestions: T[];
displayTransform: ( value: T ) => string;
instanceId: string;
setTimeout: ( fn: () => void, delay: number ) => number;
clearTimeout: ( id: number ) => void;
}

export interface TokenProps< T = Suggestion > {
Expand Down

0 comments on commit 5911062

Please sign in to comment.