Skip to content

Commit ce06390

Browse files
committed
manage SearchField internally controlled to allow onClearanceHandler independently from given value
1 parent c493458 commit ce06390

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
7777
- Refactored data structure position and dimension (breaking change)
7878
- `<ActivityControlWidget />`
7979
- display running time after label if there is an status info to prevent a third line
80+
- `<SearchField />`
81+
- internally forced to be managed controlled to keep `onClearanceHandler` independent from outer `value` property
8082

8183
### Deprecated
8284

src/components/TextField/SearchField.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,37 @@ export const SearchField = ({
3636
emptySearchInputMessage = "Enter search term",
3737
onClearanceHandler,
3838
onClearanceText = "Clear current search term",
39+
onChange,
3940
leftIcon = <Icon name="operation-search" />,
4041
rightElement,
4142
...otherProps
4243
}: SearchFieldProps) => {
44+
const [value, setValue] = React.useState<string>("");
45+
4346
const clearanceButton =
44-
onClearanceHandler && otherProps.value ? (
47+
onClearanceHandler && value ? (
4548
<IconButton
4649
data-test-id={otherProps["data-test-id"] && `${otherProps["data-test-id"]}-clear-btn`}
4750
name="operation-clear"
4851
text={onClearanceText}
49-
onClick={onClearanceHandler}
52+
onClick={() => {
53+
setValue("");
54+
onClearanceHandler();
55+
}}
5056
/>
5157
) : undefined;
5258

59+
const changeHandlerProcess = (e: React.ChangeEvent<HTMLInputElement>) => {
60+
setValue(e.target.value);
61+
if (onChange) {
62+
onChange(e);
63+
}
64+
};
65+
66+
React.useEffect(() => {
67+
setValue(otherProps.value ?? otherProps.defaultValue ?? "");
68+
}, [otherProps.value, otherProps.defaultValue]);
69+
5370
return (
5471
<TextField
5572
className={
@@ -68,7 +85,9 @@ export const SearchField = ({
6885
</>
6986
)
7087
}
88+
onChange={changeHandlerProcess}
7189
{...otherProps}
90+
value={value}
7291
type={"search"}
7392
leftIcon={leftIcon}
7493
round={true}

src/components/TextField/stories/SearchField.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ const SearchFieldWithClearanceIconTemplate: StoryFn<typeof SearchField> = (args)
4646
export const SearchFieldWithClearanceIcon = SearchFieldWithClearanceIconTemplate.bind({});
4747
SearchFieldWithClearanceIcon.args = {
4848
onClearanceHandler: null,
49-
onClearanceText: "",
49+
onClearanceText: "Clear field",
5050
};

0 commit comments

Comments
 (0)