Use AutoComplete
to provide a user with a list of selectable suggestions that appear when they type into the
input field. This list is populated by server search results.
Always set an accessible label to help the user interact with the component.
label_text
is required and visible by default.visually_hide_label
to true
.
However, please note that a visible label should almost always
be used unless there is compelling reason not to. A placeholder is not a label.Name | Type | Default | Description |
---|---|---|---|
label_text | String | N/A | The label of the input. |
src | String | N/A | The route to query. |
input_id | String | N/A | Id of the input element. |
input_name | String | nil | Optional name of the input element, defaults to input_id when not set. |
list_id | String | N/A | Id of the list element. |
visually_hide_label | Boolean | false | Controls if the label is visible. If true , screen reader only text will be added. |
show_clear_button | Boolean | false | Adds optional clear button. |
system_arguments | Hash | N/A | System arguments |
size | Hash | :medium | Input size can be small, medium (default), or large |
full_width | Boolean | false | Input can be full-width or fit to content |
disabled | Boolean | false | Disabled input |
invalid | Boolean | false | Invalid input |
placeholder | String | nil | The placeholder text displayed within the input |
inset | Boolean | false | subtle input background color |
monospace | Boolean | false | monospace input font family |
results
Customizable results list.
Name | Type | Default | Description |
---|---|---|---|
system_arguments | Hash | N/A | System arguments |
leading_visual
Leading visual.
leading_visual_icon
for a Octicon.Name | Type | Default | Description |
---|---|---|---|
system_arguments | Hash | N/A | Same arguments as Octicon. |
input
Customizable input used to search for results. It is preferred to use this slot sparingly - it will be created by default if not explicity added.
Name | Type | Default | Description |
---|---|---|---|
system_arguments | Hash | N/A | System arguments |
Display any Octicon as a leading visual within the field
<%= render(Primer::Beta::AutoComplete.new(label_text: "Select a fruit", src: "/view-components/rails-app/auto_complete", input_id:"input-id-1", list_id: "list-id-1")) do |component| %><% component.with_leading_visual_icon(icon: :search) %><% end %>
Show a clear button
<%= render(Primer::Beta::AutoComplete.new(label_text: "Select a fruit", input_id: "input-id-2", list_id: "list-id-2", src: "/view-components/rails-app/auto_complete", show_clear_button: true )) %>
A non-visible label may be rendered with visually_hide_label: true
, but it is highly discouraged. See Accessibility.
<%= render(Primer::Beta::AutoComplete.new(label_text: "Select a fruit", input_id: "fruits-input--custom-results-1", list_id: "fruits-popup--custom-result-1", src: "/view-components/rails-app/auto_complete", visually_hide_label: true)) do |component| %><% component.with_leading_visual_icon(icon: :search) %><% end %>
To allow field to span width of its container, set full_width
to true
.
<%= render(Primer::Beta::AutoComplete.new(label_text: "Select a fruit", input_id: "fruits-input--custom-results-2", list_id: "fruits-popup--custom-results-2", src: "/view-components/rails-app/auto_complete", full_width: true)) do |component| %><% component.with_leading_visual_icon(icon: :search) %><% end %>
Use the inset
variant to change the input background color to be subtle.
<%= render(Primer::Beta::AutoComplete.new(label_text: "Select a fruit", input_id: "fruits-input--custom-results-2", list_id: "fruits-popup--custom-results-2", src: "/view-components/rails-app/auto_complete", inset: true)) do |component| %><% component.with_leading_visual_icon(icon: :search) %><% end %>
Use the monospace
variant to change the input font family.
<%= render(Primer::Beta::AutoComplete.new(label_text: "Select a fruit", input_id: "fruits-input--custom-results-2", list_id: "fruits-popup--custom-results-2", src: "/view-components/rails-app/auto_complete", monospace: true)) do |component| %><% component.with_leading_visual_icon(icon: :search) %><% end %>
<%= render(Primer::Beta::AutoComplete.new(label_text: "Fruits", src: "/view-components/rails-app/auto_complete", input_id: "fruits-input--custom-input", list_id: "fruits-popup--custom-input")) do |component| %><% component.with_input(classes: "custom-class") %><% end %>
<%= render(Primer::Beta::AutoComplete.new(label_text: "Fruits", src: "/view-components/rails-app/auto_complete", input_id: "fruits-input--custom-results", list_id: "fruits-popup--custom-results")) do |component| %><% component.with_results(classes: "custom-class") %><% end %>