Skip to content

Commit

Permalink
Refine use of event existence in state syncing effect hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
stokesman committed May 10, 2022
1 parent 29634d7 commit 2b14b0f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
3 changes: 1 addition & 2 deletions packages/components/src/input-control/input-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ function InputField(
value: valueProp,
isPressEnterToChange,
},
onChange,
isFocused
onChange
);

const { value, isDragging, isDirty } = state;
Expand Down
12 changes: 4 additions & 8 deletions packages/components/src/input-control/reducer/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,12 @@ function inputControlStateReducer(
* @param stateReducer An external state reducer.
* @param initialState The initial state for the reducer.
* @param onChangeHandler A handler for the onChange event.
* @param isFocused Focus state.
* @return State, dispatch, and a collection of actions.
*/
export function useInputControlStateReducer(
stateReducer: StateReducer = initialStateReducer,
initialState: Partial< InputState > = initialInputControlState,
onChangeHandler: InputChangeCallback,
isFocused: Boolean
onChangeHandler: InputChangeCallback
) {
const [ state, dispatch ] = useReducer< StateReducer >(
inputControlStateReducer( stateReducer ),
Expand Down Expand Up @@ -205,9 +203,8 @@ export function useInputControlStateReducer(
currentValueProp.current = initialState.value;
} );
useLayoutEffect( () => {
if ( ! refEvent.current ) return;

if (
refEvent.current &&
state.value !== currentValueProp.current &&
! currentState.current.isDirty
) {
Expand All @@ -217,16 +214,15 @@ export function useInputControlStateReducer(
| PointerEvent< HTMLInputElement >,
} );
}
refEvent.current = null;
}, [ state.value, state.isDragging, isFocused ] );
}, [ state.value ] );
useLayoutEffect( () => {
if (
! refEvent.current &&
initialState.value !== currentState.current.value &&
! currentState.current.isDirty
) {
dispatch( { value: initialState.value } );
}
if ( refEvent.current ) refEvent.current = null;
}, [ initialState.value ] );

return {
Expand Down

0 comments on commit 2b14b0f

Please sign in to comment.