Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InputControl: Fix undo when changing padding values #40518

Merged
merged 11 commits into from
May 30, 2022
Merged
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 },
} );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two reasons for switching to dispatch:

  • reset is typed to require passing an event for the second argument and I didn't want to change the typing because in most cases it would be important to pass the event.
  • The exhaustive-deps lint rule knows dispatch is stable and won't complain when it's not in the dependencies.

My remaining peeve with this is I think it'd be better to not reuse RESET here and maybe reintroduce UPDATE. It's only a theoretical concern only and for now a non-issue. The concern would be that someday a component wants to specialize RESET in a way that should not happen every time the value prop has changed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation of dispatch over reset, it makes sense. I also agree regarding RESET vs UPDATE and that it is a potential future concern but that can be addressed down the line and separately to this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like a good point, if we were to keep the internal reducer!

At the same time, after this fix is merged I'd like to explore ways to simplify InputControl (and consequently its derived components), including the removal of the internal reducer (see this comment for more details)

}
}, [ 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