Skip to content

Commit cb3c484

Browse files
shiba-codesLena Rashkovan
and
Lena Rashkovan
authored
fix(text-field): fixes for controlled and uncontrolled states (#475)
* fix(text-field): fix clearing uncontrolled input * fix(text-field): fix syncing state to the external value --------- Co-authored-by: Lena Rashkovan <[email protected]>
1 parent 637b1f0 commit cb3c484

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/components/experimental/TextField/ClearButton.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const StyledButton = styled(BaseButton)`
1111
display: flex;
1212
margin: 0;
1313
padding: 0;
14+
cursor: pointer;
1415
color: ${getSemanticValue('on-surface-variant')};
1516
`;
1617

src/components/experimental/TextField/TextField.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ function TextField({
163163
const [text, setText] = React.useState(props.defaultValue || props.value || '');
164164
const inputRef = React.useRef<HTMLInputElement | HTMLTextAreaElement>(null);
165165

166+
React.useEffect(() => {
167+
if (props.value !== undefined) {
168+
setText(props.value);
169+
}
170+
}, [props.value]);
171+
166172
const handleChange = (value: string) => {
167173
setText(value);
168174
props.onChange?.(value);
@@ -175,7 +181,7 @@ function TextField({
175181
aria-label={ariaStrings.clearFieldButton}
176182
onPress={() => {
177183
inputRef.current.value = '';
178-
setText('');
184+
handleChange('');
179185
}}
180186
/>
181187
) : (

0 commit comments

Comments
 (0)