Skip to content

Commit

Permalink
Handle selected values in Autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillplatonov committed Jan 12, 2022
1 parent 90981e6 commit c33c3f3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
11 changes: 10 additions & 1 deletion app/assets/javascripts/polaris_view_components.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ function formatBytes(bytes, decimals) {
class Autocomplete extends Controller {
static targets=[ "popover", "input", "results", "option", "emptyState" ];
static values={
url: String
url: String,
selected: Array
};
connect() {
this.inputTarget.addEventListener("input", this.onInputChange);
Expand Down Expand Up @@ -69,6 +70,7 @@ class Autocomplete extends Controller {
if (this.visibleOptions.length > 0) {
this.hideEmptyState();
this.popoverController.show();
this.checkSelected();
} else if (this.value.length > 0 && this.hasEmptyStateTarget) {
this.showEmptyState();
} else {
Expand Down Expand Up @@ -116,6 +118,13 @@ class Autocomplete extends Controller {
this.resultsTarget.classList.remove("Polaris--hidden");
}
}
checkSelected() {
this.visibleOptions.forEach((option => {
const input = option.querySelector("input");
if (!input) return;
input.checked = this.selectedValue.includes(input.value);
}));
}
}

class Button extends Controller {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { debounce } from './utils'

export default class extends Controller {
static targets = ['popover', 'input', 'results', 'option', 'emptyState']
static values = { url: String }
static values = { url: String, selected: Array }

connect() {
this.inputTarget.addEventListener("input", this.onInputChange)
Expand Down Expand Up @@ -67,6 +67,7 @@ export default class extends Controller {
if (this.visibleOptions.length > 0) {
this.hideEmptyState()
this.popoverController.show()
this.checkSelected()
} else if (this.value.length > 0 && this.hasEmptyStateTarget) {
this.showEmptyState()
} else {
Expand Down Expand Up @@ -116,4 +117,13 @@ export default class extends Controller {
this.resultsTarget.classList.remove('Polaris--hidden')
}
}

checkSelected() {
this.visibleOptions.forEach(option => {
const input = option.querySelector('input')
if (!input) return

input.checked = this.selectedValue.includes(input.value)
})
}
}
9 changes: 8 additions & 1 deletion app/components/polaris/autocomplete_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ class AutocompleteComponent < NewComponent
end
renders_one :empty_state

def initialize(multiple: false, url: nil, **system_arguments)
def initialize(
multiple: false,
url: nil,
selected: [],
**system_arguments
)
@multiple = multiple
@url = url
@selected = selected
@system_arguments = system_arguments
end

Expand All @@ -35,6 +41,7 @@ def system_arguments
if @url.present?
opts[:data][:polaris_autocomplete_url_value] = @url
end
opts[:data][:polaris_autocomplete_selected_value] = @selected
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= polaris_autocomplete(multiple: true) do |autocomplete| %>
<%= polaris_autocomplete(multiple: true, selected: ["vinyl"]) do |autocomplete| %>
<% autocomplete.text_field(label: "Tags", placeholder: "Search") do |c| %>
<% c.prefix do %>
<%= polaris_icon(name: "SearchMinor") %>
Expand Down

0 comments on commit c33c3f3

Please sign in to comment.