-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement customizable elements (#80)
- Loading branch information
1 parent
88b82fc
commit 65366dd
Showing
10 changed files
with
160 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module HotwireCombobox::Component::Customizable | ||
CUSTOMIZABLE_ELEMENTS = %i[ | ||
fieldset | ||
hidden_field | ||
input | ||
handle | ||
listbox | ||
dialog | ||
dialog_wrapper | ||
dialog_label | ||
dialog_input | ||
dialog_listbox | ||
].freeze | ||
|
||
PROTECTED_ATTRS = %i[ | ||
id | ||
name | ||
value | ||
open | ||
role | ||
hidden | ||
].freeze | ||
|
||
CUSTOMIZABLE_ELEMENTS.each do |element| | ||
define_method "customize_#{element}" do |**attrs| | ||
customize element, **attrs | ||
end | ||
end | ||
|
||
private | ||
def custom_attrs | ||
@custom_attrs ||= Hash.new { |h, k| h[k] = {} } | ||
end | ||
|
||
def customize(element, **attrs) | ||
element = element.to_sym.presence_in(CUSTOMIZABLE_ELEMENTS) | ||
sanitized_attrs = attrs.deep_symbolize_keys.except(*PROTECTED_ATTRS) | ||
|
||
custom_attrs.store element, sanitized_attrs | ||
end | ||
|
||
def apply_customizations_to(element, base: {}) | ||
custom = custom_attrs[element] | ||
coalesce = ->(k, v) { v.is_a?(String) ? view.token_list(v, custom.delete(k)) : v } | ||
default = base.map { |k, v| [ k, coalesce.(k, v) ] }.to_h | ||
|
||
custom.deep_merge default | ||
end | ||
end |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<%= combobox_tag "state", State.all do |combobox| %> | ||
<% combobox.customize_fieldset class: "custom-class--fieldset", data: { customized_fieldset: "" } %> | ||
<% combobox.customize_hidden_field class: "custom-class--hidden_field", data: { customized_hidden_field: "" } %> | ||
<% combobox.customize_input class: "custom-class--input", data: { customized_input: "" } %> | ||
<% combobox.customize_handle class: "custom-class--handle", data: { customized_handle: "" } %> | ||
<% combobox.customize_listbox class: "custom-class--listbox", data: { customized_listbox: "" } %> | ||
<% combobox.customize_dialog class: "custom-class--dialog", data: { customized_dialog: "" } %> | ||
<% combobox.customize_dialog_wrapper class: "custom-class--dialog_wrapper", data: { customized_dialog_wrapper: "" } %> | ||
<% combobox.customize_dialog_label class: "custom-class--dialog_label", data: { customized_dialog_label: "" } %> | ||
<% combobox.customize_dialog_input class: "custom-class--dialog_input", data: { customized_dialog_input: "" } %> | ||
<% combobox.customize_dialog_listbox class: "custom-class--dialog_listbox", data: { customized_dialog_listbox: "" } %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters