Skip to content

Commit d843186

Browse files
committed
Fix disabled items still selectable via hint (#835, #865)
1 parent a0e2346 commit d843186

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

src/components/Typeahead/Typeahead.stories.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ AllowNew.args = {
9595
allowNew: true,
9696
};
9797

98+
export const DisabledItem = Template.bind({});
99+
DisabledItem.args = {
100+
...defaultProps,
101+
options: options.map((option) =>
102+
option.name === 'Alabama'
103+
? { ...option, disabled: true }
104+
: { ...option, disabled: false }
105+
),
106+
};
107+
98108
export const CustomInput = Template.bind({});
99109
CustomInput.args = {
100110
...defaultProps,

src/components/Typeahead/Typeahead.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const {
5757
InputValidation,
5858
Pagination,
5959
AllowNew,
60+
DisabledItem,
6061
CustomMenu,
6162
Controlled,
6263
} = composeStories(stories);
@@ -736,6 +737,22 @@ describe('<Typeahead>', () => {
736737
expect(getMenu()).not.toBeInTheDocument();
737738
expect(hint).toHaveValue('');
738739
});
740+
741+
it('only displays a hint for non-disabled items', async () => {
742+
const user = userEvent.setup();
743+
const { container } = render(<DisabledItem />);
744+
const input = getInput();
745+
const hint = getHint(container);
746+
747+
await user.type(input, 'Ala');
748+
749+
// The hint should not display if the initial item is disabled.
750+
expect(hint).toHaveValue('');
751+
752+
await user.clear(input);
753+
await user.type(input, 'Ari');
754+
expect(hint).toHaveValue('Arizona');
755+
});
739756
});
740757

741758
describe('behavior when selecting the hinted result', () => {

src/components/Typeahead/__snapshots__/Typeahead.test.tsx.snap

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,38 @@ exports[`<Typeahead> Default story renders snapshot 1`] = `
223223
</div>
224224
`;
225225
226+
exports[`<Typeahead> DisabledItem story renders snapshot 1`] = `
227+
<div
228+
class="rbt"
229+
style="outline: none; position: relative;"
230+
tabindex="-1"
231+
>
232+
<div
233+
style="display: flex; flex: 1; height: 100%; position: relative;"
234+
>
235+
<input
236+
aria-autocomplete="both"
237+
aria-expanded="false"
238+
aria-haspopup="listbox"
239+
autocomplete="off"
240+
class="rbt-input-main form-control rbt-input"
241+
placeholder="Choose a state..."
242+
role="combobox"
243+
type="text"
244+
value=""
245+
/>
246+
<input
247+
aria-hidden="true"
248+
class="rbt-input-hint"
249+
readonly=""
250+
style="background-color: transparent; border-color: transparent; box-shadow: none; color: rgba(0, 0, 0, 0.54); left: 0px; pointer-events: none; position: absolute; top: 0px; width: 100%;"
251+
tabindex="-1"
252+
value=""
253+
/>
254+
</div>
255+
</div>
256+
`;
257+
226258
exports[`<Typeahead> InputGrouping story renders snapshot 1`] = `
227259
<div
228260
class="input-group"

src/utils/getHintText.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ describe('getHintText', () => {
6060
expect(hintText).toBe('');
6161
});
6262

63+
it('returns an empty string when the initial item is disabled', () => {
64+
const initialItem = { ...props.initialItem, disabled: true };
65+
const hintText = getHintText({ ...props, initialItem });
66+
expect(hintText).toBe('');
67+
});
68+
69+
it('returns the hint string when the initial item is not disabled', () => {
70+
const initialItem = { ...props.initialItem, disabled: false };
71+
const hintText = getHintText({ ...props, initialItem });
72+
expect(hintText).toBe('alAbama');
73+
});
74+
6375
it('handles string with composed diacritical marks', () => {
6476
const hintText = getHintText({
6577
...props,

src/utils/getHintText.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ function getHintText({
3838
!initialItem ||
3939
// The initial item is a custom option.
4040
(!isString(initialItem) && hasOwnProperty(initialItem, 'customOption')) ||
41+
// The initial item is disabled
42+
(!isString(initialItem) && initialItem.disabled) ||
4143
// One of the menu items is active.
4244
activeIndex > -1 ||
4345
// There's already a selection in single-select mode.

0 commit comments

Comments
 (0)