Skip to content

Commit

Permalink
Have InputControl favor entered value for a render cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
stokesman committed May 29, 2022
1 parent 0de33ab commit 8952d45
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
10 changes: 8 additions & 2 deletions packages/components/src/input-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useState, forwardRef } from '@wordpress/element';
import InputBase from './input-base';
import InputField from './input-field';
import type { InputControlProps } from './types';
import { useDraft } from './utils';

function useUniqueId( idProp?: string ) {
const instanceId = useInstanceId( InputControl );
Expand Down Expand Up @@ -52,6 +53,12 @@ export function UnforwardedInputControl(
const id = useUniqueId( idProp );
const classes = classNames( 'components-input-control', className );

const draftHookProps = useDraft( {
value,
onBlur: props.onBlur,
onChange,
} );

return (
<InputBase
__unstableInputWidth={ __unstableInputWidth }
Expand All @@ -75,14 +82,13 @@ export function UnforwardedInputControl(
id={ id }
isFocused={ isFocused }
isPressEnterToChange={ isPressEnterToChange }
onChange={ onChange }
onKeyDown={ onKeyDown }
onValidate={ onValidate }
ref={ ref }
setIsFocused={ setIsFocused }
size={ size }
stateReducer={ stateReducer }
value={ value }
{ ...draftHookProps }
/>
</InputBase>
);
Expand Down
56 changes: 55 additions & 1 deletion packages/components/src/input-control/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
/**
* External dependencies
*/
import type { FocusEventHandler } from 'react';

/**
* WordPress dependencies
*/
import { useEffect } from '@wordpress/element';
import {
useEffect,
useLayoutEffect,
useRef,
useState,
} from '@wordpress/element';

/**
* Internal dependencies
*/
import type { InputChangeCallback } from './types';

/**
* Gets a CSS cursor value based on a drag direction.
Expand Down Expand Up @@ -52,3 +67,42 @@ export function useDragCursor(

return dragCursor;
}

export function useDraft( props: {
value: string | undefined;
onBlur?: FocusEventHandler;
onChange: InputChangeCallback;
} ) {
const refPreviousValue = useRef( props.value );
const [ draft, setDraft ] = useState< {
value?: string;
isStale?: boolean;
} >( {} );
const value = draft.value !== undefined ? draft.value : props.value;

// Determines when to discard the draft value to restore controlled status.
// To do so, it tracks the previous value and marks the draft value as stale
// after each render.
useLayoutEffect( () => {
const { current: previousValue } = refPreviousValue;
refPreviousValue.current = props.value;
if ( draft.value !== undefined && ! draft.isStale )
setDraft( { ...draft, isStale: true } );
else if ( draft.isStale && props.value !== previousValue )
setDraft( {} );
}, [ props.value, draft ] );

const onChange: InputChangeCallback = ( nextValue, extra ) => {
// Mutates the draft value to avoid an extra effect run.
setDraft( ( current ) =>
Object.assign( current, { value: nextValue, isStale: false } )
);
props.onChange( nextValue, extra );
};
const onBlur: FocusEventHandler = ( event ) => {
setDraft( {} );
props.onBlur?.( event );
};

return { value, onBlur, onChange };
}

0 comments on commit 8952d45

Please sign in to comment.