Skip to content

Commit

Permalink
SearchBox: fix event handling regressions in v8 (#21268)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecraig12345 committed Jan 13, 2022
1 parent d2fbf6b commit 6a1cfa0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "SearchBox: properly pass the new value to onChange, and only call onChange once",
"packageName": "@fluentui/react",
"email": "elcraig@microsoft.com",
"dependentChangeType": "patch"
}
21 changes: 14 additions & 7 deletions packages/react/src/components/SearchBox/SearchBox.base.tsx
Expand Up @@ -61,18 +61,25 @@ export const SearchBoxBase: React.FunctionComponent<ISearchBoxProps> = React.for
} = props;

const [hasFocus, setHasFocus] = React.useState(false);

const prevChangeTimestamp = React.useRef<number | undefined>();
const [uncastValue, setValue] = useControllableValue(
props.value,
defaultValue,
React.useCallback(
(ev: React.ChangeEvent<HTMLInputElement> | undefined) => {
onChange?.(ev, ev?.target.value);
onChanged?.(ev?.target.value);
},
[onChange, onChanged],
),
(ev: React.ChangeEvent<HTMLInputElement> | undefined, newValue: string) => {
if (ev && ev.timeStamp === prevChangeTimestamp.current) {
// For historical reasons, SearchBox handles both onInput and onChange (we can't modify this
// outside a major version due to potential to break partners' tests and possibly apps).
// Only call props.onChange for one of the events.
return;
}
prevChangeTimestamp.current = ev?.timeStamp;
onChange?.(ev, newValue);
onChanged?.(newValue);
},
);
const value = String(uncastValue);

const rootElementRef = React.useRef<HTMLDivElement>(null);
const inputElementRef = React.useRef<HTMLInputElement>(null);
const mergedRootRef = useMergedRefs(rootElementRef, forwardedRef);
Expand Down

0 comments on commit 6a1cfa0

Please sign in to comment.