Skip to content

Commit f1ff83f

Browse files
authored
fix: Move focus to the attribute editor add button even if currently disabled (#3035)
1 parent 95f329b commit f1ff83f

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

src/attribute-editor/__tests__/attribute-editor.test.tsx

+10-3
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,26 @@ describe('Attribute Editor', () => {
167167
test('enables the add button by default', () => {
168168
const wrapper = renderAttributeEditor({ ...defaultProps });
169169
const buttonElement = wrapper.findAddButton().getElement();
170-
expect(buttonElement).not.toHaveAttribute('disabled');
170+
expect(buttonElement).not.toHaveAttribute('aria-disabled');
171171
});
172172

173173
test('enables the add button when disableAddButton is false', () => {
174174
const wrapper = renderAttributeEditor({ ...defaultProps, disableAddButton: false });
175175
const buttonElement = wrapper.findAddButton().getElement();
176-
expect(buttonElement).not.toHaveAttribute('disabled');
176+
expect(buttonElement).not.toHaveAttribute('aria-disabled');
177177
});
178178

179179
test('disables the add button when disableAddButton is true', () => {
180180
const wrapper = renderAttributeEditor({ ...defaultProps, disableAddButton: true });
181181
const buttonElement = wrapper.findAddButton().getElement();
182-
expect(buttonElement).toHaveAttribute('disabled');
182+
expect(buttonElement).toHaveAttribute('aria-disabled');
183+
});
184+
185+
test('allows the add button to be focused manually when disableAddButton is true', () => {
186+
const ref: React.Ref<AttributeEditorProps.Ref> = React.createRef();
187+
const wrapper = renderAttributeEditor({ ...defaultProps, ref, disableAddButton: true });
188+
ref.current!.focusAddButton();
189+
expect(wrapper.findAddButton().getElement()).toHaveFocus();
183190
});
184191

185192
test('has no aria-describedby if there is no additional info', () => {

src/attribute-editor/internal.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ const InternalAttributeEditor = React.forwardRef(
103103
<InternalButton
104104
className={styles['add-button']}
105105
disabled={disableAddButton}
106+
// Using aria-disabled="true" and tabindex="-1" instead of "disabled"
107+
// because focus can be dynamically moved to this button by calling
108+
// `focusAddButton()` on the ref.
109+
__nativeAttributes={disableAddButton ? { tabIndex: -1 } : {}}
110+
__focusable={true}
106111
onClick={onAddButtonClick}
107112
formAction="none"
108113
ref={addButtonRef}

src/tag-editor/__tests__/tag-editor.test.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ describe('Tag Editor component', () => {
283283
test('is not disabled by default ', () => {
284284
const { wrapper } = renderTagEditor();
285285

286-
expect(wrapper.findAddButton().getElement()).not.toHaveAttribute('disabled');
286+
expect(wrapper.findAddButton().getElement()).not.toHaveAttribute('aria-disabled');
287287
});
288288

289289
test('is disabled when tag limit has been reached', () => {
@@ -292,7 +292,7 @@ describe('Tag Editor component', () => {
292292
tagLimit: 1,
293293
});
294294

295-
expect(wrapper.findAddButton().getElement()).toHaveAttribute('disabled');
295+
expect(wrapper.findAddButton().getElement()).toHaveAttribute('aria-disabled');
296296
});
297297

298298
test('is disabled when tag limit has been exceeded', () => {
@@ -304,7 +304,7 @@ describe('Tag Editor component', () => {
304304
tagLimit: 1,
305305
});
306306

307-
expect(wrapper.findAddButton().getElement()).toHaveAttribute('disabled');
307+
expect(wrapper.findAddButton().getElement()).toHaveAttribute('aria-disabled');
308308
});
309309

310310
test('adds an empty tag when there are no tags', () => {

0 commit comments

Comments
 (0)