-
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.
Metaprogram customizable element methods
- Loading branch information
1 parent
bcc97f3
commit 6a5c98d
Showing
4 changed files
with
79 additions
and
67 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,63 @@ | ||
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) || | ||
raise(ArgumentError, <<~MSG) | ||
[ACTION NEEDED] – Message from HotwireCombobox: | ||
You tried to customize an element called `#{element}`, but | ||
HotwireCombobox does not recognize that element. | ||
Please choose one of the valid elements: #{CUSTOMIZABLE_ELEMENTS.join(", ")}. | ||
MSG | ||
|
||
custom_attrs[element] = attrs.deep_symbolize_keys.delete_if do |key, _| | ||
PROTECTED_ATTRS.include? key | ||
end | ||
end | ||
|
||
def apply_customizations_to(element, base: {}) | ||
custom = custom_attrs[element] | ||
default = base.deep_symbolize_keys.map do |key, value| | ||
if value.is_a?(String) || value.is_a?(Symbol) | ||
[ key, view.token_list(value.to_s, custom.delete(key)) ] | ||
else | ||
[ key, value ] | ||
end | ||
end.to_h | ||
|
||
custom.deep_merge default | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +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: "" } %> | ||
<% 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