Skip to content

Commit cf00999

Browse files
committed
add onValueChange prevValue in Input
1 parent 72076d3 commit cf00999

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/scripts/Input.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import React, {
66
useEffect,
77
useContext,
88
Ref,
9+
useRef,
910
} from 'react';
1011
import classnames from 'classnames';
1112
import keycoder from 'keycoder';
@@ -91,7 +92,7 @@ export type InputProps = {
9192
addonRight?: string;
9293
elementRef?: Ref<HTMLDivElement>;
9394
inputRef?: Ref<HTMLInputElement>;
94-
onValueChange?: (value: string) => void;
95+
onValueChange?: (value: string, prevValue?: string) => void;
9596
} & Omit<InputHTMLAttributes<HTMLInputElement>, 'value' | 'defaultValue'>;
9697

9798
/**
@@ -108,6 +109,8 @@ export const Input = createFC<InputProps, { isFormElement: boolean }>(
108109

109110
useInitComponentStyle();
110111

112+
const prevValueRef = useRef<string>();
113+
111114
const onKeyDown = useEventCallback((e: KeyboardEvent<HTMLInputElement>) => {
112115
if (symbolPattern) {
113116
const { keyCode, shiftKey } = e;
@@ -122,7 +125,8 @@ export const Input = createFC<InputProps, { isFormElement: boolean }>(
122125

123126
const onChange = useEventCallback((e: ChangeEvent<HTMLInputElement>) => {
124127
onChange_?.(e);
125-
onValueChange?.(e.target.value);
128+
onValueChange?.(e.target.value, prevValueRef.current);
129+
prevValueRef.current = e.target.value;
126130
});
127131

128132
const {
@@ -162,6 +166,16 @@ export const Input = createFC<InputProps, { isFormElement: boolean }>(
162166
value,
163167
defaultValue,
164168
htmlReadOnly,
169+
iconLeft,
170+
iconRight,
171+
addonLeft,
172+
addonRight,
173+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
174+
onChange: _unused_1,
175+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
176+
onValueChange: _unused_2,
177+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
178+
onKeyDown: _unused_3,
165179
...rprops2
166180
} = rprops;
167181
const inputClassNames = classnames(
@@ -192,7 +206,6 @@ export const Input = createFC<InputProps, { isFormElement: boolean }>(
192206
/>
193207
);
194208

195-
const { iconLeft, iconRight, addonLeft, addonRight } = props;
196209
if (iconLeft || iconRight || addonLeft || addonRight) {
197210
const wrapperClassName = classnames(
198211
'slds-form-element__control',

stories/Input.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const meta: ComponentMeta<typeof Input> = {
99
component: Input,
1010
argTypes: {
1111
onChange: { action: 'change' },
12+
onValueChange: { action: 'valueChange' },
1213
onBlur: { action: 'blur' },
1314
},
1415
};

0 commit comments

Comments
 (0)