Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(TextInput): keep the clear button in place when the field turns busy (#9027)

Ojas Mutreja18be9cb58c41
diff --git a/.changeset/textinput-clear-stays-put.md b/.changeset/textinput-clear-stays-put.md
new file mode 100644
index 0000000000..43d59ad54b
--- /dev/null
+++ b/.changeset/textinput-clear-stays-put.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] TextInput: the clear button no longer shifts inward when the field turns busy. The busy Spinner now sits inward of the clear button, as in Typeahead and Tokenizer, so a user reaching for the button while a search runs does not miss it. No API changes.
+
+@HelloOjasMutreja
diff --git a/packages/core/src/TextInput/TextInput.test.tsx b/packages/core/src/TextInput/TextInput.test.tsx
index 9e53fb5b6f..317b67cc11 100644
--- a/packages/core/src/TextInput/TextInput.test.tsx
+++ b/packages/core/src/TextInput/TextInput.test.tsx
@@ -823,6 +823,27 @@ describe('TextInput', () => {
});
});
+ it('keeps the clear button where it is when the field turns busy', () => {
+ // What follows the button in the lane (a `<template>` is not rendered).
+ const after = (button: HTMLElement) =>
+ Array.from(button.parentElement?.children ?? [])
+ .slice(Array.from(button.parentElement?.children ?? []).indexOf(button))
+ .filter(el => el !== button && el.tagName !== 'TEMPLATE');
+ const props = {label: 'Name', value: 'hello', onChange: () => {}};
+ const {rerender} = render(<TextInput {...props} hasClear />);
+ const idle = screen.getByRole('button', {name: 'Clear Name'});
+ const idleAfter = after(idle).length;
+
+ rerender(<TextInput {...props} hasClear isLoading />);
+ const busy = screen.getByRole('button', {name: 'Clear Name'});
+ // The Spinner joins on the inward side, before the button, so nothing new
+ // follows it and its distance from the end does not change.
+ expect(after(busy)).toHaveLength(idleAfter);
+ expect(busy.previousElementSibling?.getAttribute('aria-hidden')).toBe(
+ 'true',
+ );
+ });
+
describe('click-to-focus', () => {
it('focuses input when clicking the start icon', () => {
render(
diff --git a/packages/core/src/TextInput/TextInput.tsx b/packages/core/src/TextInput/TextInput.tsx
index 92aab932ab..e2897888e6 100644
--- a/packages/core/src/TextInput/TextInput.tsx
+++ b/packages/core/src/TextInput/TextInput.tsx
@@ -537,13 +537,15 @@ export function TextInput({
aria-labelledby={inputLabelledBy}
{...stylex.props(styles.input, isDisabled && styles.inputDisabled)}
/>
+ {/* The Spinner comes first so it appears inward of the clear button
+ and the button keeps its place when the field turns busy. */}
+ {isBusy && <Spinner size="sm" aria-hidden="true" />}
{hasClear && value !== '' && !isDisabled && !isReadOnly && (
<InputClearButton
label={t('@astryx.textInput.clearLabel', {label})}
onClick={handleClear}
/>
)}
- {isBusy && <Spinner size="sm" aria-hidden="true" />}
{statusIcon}
</div>
);