Skip to content

Commit

Permalink
Use event existence to eliminate extraneous onChange calls
Browse files Browse the repository at this point in the history
  • Loading branch information
stokesman committed May 29, 2022
1 parent 32eb218 commit bb6b932
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
18 changes: 10 additions & 8 deletions packages/components/src/input-control/reducer/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ function inputControlStateReducer(
break;
}

if ( action.payload.event ) {
nextState._event = action.payload.event;
}
nextState._event = action.payload.event;

/**
* Send the nextState + action to the composedReducers via
Expand Down Expand Up @@ -211,7 +209,11 @@ export function useInputControlStateReducer(
currentValueProp.current = initialState.value;
} );
useLayoutEffect( () => {
if ( state.value !== currentValueProp.current && ! state.isDirty ) {
if (
currentState.current._event !== undefined &&
state.value !== currentValueProp.current &&
! state.isDirty
) {
onChangeHandler( state.value ?? '', {
event: currentState.current._event as
| ChangeEvent< HTMLInputElement >
Expand All @@ -224,10 +226,10 @@ export function useInputControlStateReducer(
initialState.value !== currentState.current.value &&
! currentState.current.isDirty
) {
reset(
initialState.value,
currentState.current._event as SyntheticEvent
);
dispatch( {
type: actions.RESET,
payload: { value: initialState.value },
} );
}
}, [ initialState.value ] );

Expand Down
5 changes: 2 additions & 3 deletions packages/components/src/input-control/reducer/state.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* External dependencies
*/
import type { Reducer } from 'react';
import type { Reducer, SyntheticEvent } from 'react';

/**
* Internal dependencies
*/
import type { InputAction } from './actions';

export interface InputState {
_event: Event | {};
_event?: SyntheticEvent;
error: unknown;
initialValue?: string;
isDirty: boolean;
Expand All @@ -24,7 +24,6 @@ export type StateReducer = Reducer< InputState, InputAction >;
export const initialStateReducer: StateReducer = ( state: InputState ) => state;

export const initialInputControlState: InputState = {
_event: {},
error: null,
initialValue: '',
isDirty: false,
Expand Down

0 comments on commit bb6b932

Please sign in to comment.