Back to cookbook

AI Prompt for an Accessible React Autocomplete Combobox

0 views Published

Make this prompt yours

Share

This prompt is for developers asking Claude, ChatGPT, or Gemini to generate a React autocomplete combobox component, the input-plus-dropdown pattern used for things like city pickers, tag selectors, and command inputs. It's aimed at frontend engineers who need the component to actually meet accessibility requirements (correct ARIA roles, keyboard navigation, screen reader announcements) rather than just look right visually.

Most AI-generated autocomplete components work fine with a mouse and fail under keyboard-only or screen reader use, because the model defaults to a plain text input with a floating list and skips the ARIA combobox pattern entirely. The prompt below specifies the exact roles (combobox, listbox, option), the keyboard behavior (arrow keys, Enter, Escape, Home/End), and asks for debounced filtering so the component performs well with large option lists.

If you're generating several related form components in one sitting, running the filled-in prompt through the Prompt Optimizer in Coding mode first can tighten the structure before you send it to the model, especially the accessibility and state-management constraints that are easy to leave vague.

Prompt template

Make this prompt yours

prompt-template
293 tokens
Generate a React component with the following specification. ROLE - You are a frontend engineer who writes accessible, production-ready React components. COMPONENT - A searchable autocomplete combobox named [COMPONENT NAME]. - Props: options ([OPTION SHAPE, e.g. array of {id, label}]), value, onChange, placeholder. - Controlled component: value and onChange are provided by the parent; do not manage selected value as internal-only state. ACCESSIBILITY REQUIREMENTS - Use the ARIA 1.2 combobox pattern: role="combobox" on the input, role="listbox" on the options container, role="option" on each item, and aria-activedescendant to track the highlighted option. - Support keyboard interaction: ArrowDown/ArrowUp to move highlight, Enter to select the highlighted option, Escape to close the list, Home/End to jump to first/last option. - Announce the number of matching results to screen readers when the list updates. BEHAVIOR - Filter options client-side, debounced by [DEBOUNCE MS, e.g. 150]ms. - Close the dropdown on click outside or on Escape. - If no options match, display [EMPTY STATE MESSAGE] inside the listbox instead of hiding it. OUTPUT FORMAT - A single functional component using hooks, written in [TYPESCRIPT OR JAVASCRIPT], with inline comments marking each accessibility feature.

Want it sharper? Optimize this prompt with Prompt Optimizer, check it with the Prompt Debugger or shorten it with the Token Optimizer.

Example input

example-input
112 tokens
Generate a React component: a searchable autocomplete combobox named CityPicker. Props: options (array of {id, label}), value, onChange, placeholder. Controlled component. Use the ARIA 1.2 combobox pattern with full keyboard support (arrow keys, Enter, Escape, Home/End) and aria-activedescendant. Debounce filtering by 150ms. Close on click outside or Escape. Show "No cities found" when nothing matches. Write it in TypeScript as a single functional component with inline comments marking each accessibility feature.

When to use it

  • You need a React autocomplete or combobox input that works correctly with keyboard-only navigation and screen readers
  • You're replacing a plain <select> with a searchable dropdown for a long list of options like cities, products, or tags
  • You want debounced, client-side filtering built in so the component doesn't re-render on every keystroke
  • You're standardizing a form component that multiple teams will reuse and need it to meet WCAG keyboard and ARIA requirements from the start

Best practices

  • Specify the exact ARIA roles and attributes you expect (combobox, listbox, option, aria-activedescendant) instead of leaving accessibility implicit
  • Describe every keyboard interaction explicitly: arrow keys to move, Enter to select, Escape to close, Home/End to jump to the first or last option
  • Ask for debounced filtering with a stated delay so the model doesn't default to filtering on every keystroke with no debounce at all
  • Request that the component accept controlled props (value, onChange) rather than managing all state internally, so it fits into existing forms

Common mistakes

  • Accepting a combobox built with a plain <input> and a <div> dropdown with no ARIA roles, which looks correct but is invisible to screen readers
  • Not specifying what happens when the filtered list is empty, leaving the model to either hide the dropdown silently or show nothing useful
  • Forgetting to ask for debounce, resulting in a filter function that runs on every keystroke against a large option list
  • Leaving click-outside-to-close behavior unspecified, which often gets skipped entirely in the generated code

FAQs

How do I make a React autocomplete component accessible to screen readers?

Use the ARIA 1.2 combobox pattern with role="combobox" on the input, role="listbox" and role="option" on the dropdown and its items, and aria-activedescendant to track which option is currently highlighted.

What keyboard shortcuts should an autocomplete combobox support?

At minimum, arrow keys to move between options, Enter to select the highlighted one, Escape to close the dropdown, and Home/End to jump to the first or last option.

Why does my AI-generated autocomplete work with a mouse but not a keyboard?

Most default implementations use a plain input and div dropdown with no ARIA roles or keyboard handlers, so explicitly specifying both in your prompt is what gets the model to include them.

Should autocomplete filtering be debounced?

Yes, for any list beyond a handful of options, debouncing by 100 to 200ms prevents the filter function from running on every single keystroke.

Which Cuelara tool can help me tighten this prompt before using it?

Prompt Optimizer — its Coding mode is built for turning loose component requests like this into a structured prompt with explicit accessibility and state-management constraints.

Found this prompt useful? Share it.

Share

More in React Prompt Recipes

React Prompt Recipes

AI Prompt to Build a React Command Palette (Cmd+K) Component

This AI prompt generates a complete React command palette component, the kind of Cmd+K (or Ctrl+K) search and action overlay popularized by…

ROLE: You are a senior frontend engineer who writes accessible, reusable React components with TypeScript.

TASK: Build a command palette (Cmd+K / Ctrl+K) component for a React application with the following behavior:
1. A global keyboard shortcut ([SHORTCUT, E.G. CMD+K]) opens the palette from anywhere in the app.
2. The palette renders as a centered modal overlay with a search input focused automatically on open.
3. Below the input, show a filtered, scrollable list of commands based on a prop-driven array of {id, label, icon, action, group} items.
4. Support fuzzy or substring matching on the label as the user types, updating the list in real time.
5. Support arrow-up/arrow-down to move the active selection and Enter to run the selected command's action and close the palette.
6. Escape closes the palette and returns focus to whatever element triggered it.
7. Group commands by their 'group' field with a small header label per group in the results list.

CONSTRAINTS:
- Use React hooks only, no class components.
- Trap focus inside the palette while it is open.
- Use ARIA roles appropriate for a combobox/listbox pattern.
- Style with [STYLING APPROACH, E.G. TAILWIND CSS / CSS MODULES].

OUTPUT FORMAT: A single CommandPalette.tsx component file plus a short usage example showing how to pass the command list and mount the component, with brief inline comments explaining the keyboard and focus logic.

APP CONTEXT: [BRIEF DESCRIPTION OF THE APP AND EXAMPLE COMMANDS, E.G. 'PROJECT MANAGEMENT TOOL WITH COMMANDS LIKE NEW TASK, GO TO SETTINGS, INVITE TEAMMATE']

Make this prompt yours

React Prompt Recipes

AI Prompt to Build a React Infinite Scroll List With Virtualization

This prompt is for developers who need a React infinite scroll list that stays fast even with thousands of rows, by combining lazy loaded pa…

ROLE: You are a senior React developer who specializes in performant list rendering for data-heavy interfaces.

CONTEXT:
Framework: [REACT VERSION, e.g. React 18 with hooks]
Data source: [DESCRIBE THE API OR DATA SOURCE, e.g. a paginated REST endpoint returning 50 items per page]
List item shape: [DESCRIBE THE FIELDS EACH ROW NEEDS TO DISPLAY]
Expected list size: [APPROXIMATE TOTAL ROWS, e.g. up to 10,000]

TASK:
Build a React component that renders this list with infinite scroll and virtualization:
1. A custom hook that manages paginated fetching, tracks loading and error state, and triggers the next page fetch when the user nears the bottom of the list.
2. A virtualized list renderer that only mounts rows near the current viewport, using a windowing technique, with a small buffer of extra rows above and below the visible area.
3. Loading placeholders for in-flight pages and a clear end-of-list indicator when there is no more data.
4. Basic error handling that lets the user retry a failed page fetch.

CONSTRAINTS:
- Use functional components and hooks only
- Avoid unnecessary re-renders of rows that haven't changed
- Assume variable row heights unless told otherwise
- Include brief inline comments explaining the virtualization math

OUTPUT FORMAT:
1. A short explanation of the approach
2. The custom data-fetching hook as a complete code block
3. The virtualized list component as a complete code block
4. Notes on any assumptions made about row height or page size

Make this prompt yours

React Prompt Recipes

ChatGPT Prompt to Build a React Drag-and-Drop File Upload Component With Progress Bar

This React drag and drop file upload component prompt is built for developers who need a working uploader with a drop zone, file previews, a…

ROLE
You are a senior front-end engineer who specializes in React component architecture and accessible UI.

CONTEXT
I'm building a [FORM / DASHBOARD / ADMIN PANEL] in React using [REACT VERSION, e.g. React 18 with function components] and [STYLING APPROACH, e.g. Tailwind CSS / CSS Modules / styled-components]. I need a file upload component that supports drag-and-drop in addition to a standard click-to-browse input.

REQUIREMENTS
- Accept these file types: [ALLOWED FILE TYPES, e.g. .png, .jpg, .pdf]
- Enforce a max file size of [MAX FILE SIZE] and a max file count of [MAX FILE COUNT]
- Show a visually distinct drop zone state when a file is dragged over it
- Display a progress bar per file while it uploads, using [UPLOAD METHOD, e.g. an onUploadProgress callback from axios, or XMLHttpRequest progress events]
- Show clear success and error states per file, including a retry option on failure
- Accept the actual upload function as a prop so the component is backend-agnostic
- Be keyboard accessible: the file input must be triggerable without a mouse, and status changes must be announced via an ARIA live region

CONSTRAINTS
- Use only [ALLOWED LIBRARIES, e.g. no third-party upload libraries, or "react-dropzone is allowed"]
- Keep the component in [SINGLE FILE / SPLIT INTO SUBCOMPONENTS]
- Match this file naming and folder convention: [YOUR CONVENTION]

OUTPUT FORMAT
1. The complete component code in a single fenced code block, with inline comments explaining the drag event handlers and progress logic
2. A short list of the props the component expects and what each one does
3. One sentence noting any edge case the implementation does not handle, if any

Make this prompt yours