Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle selected values in Autocomplete #184

Merged
merged 1 commit into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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