-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[Admin][UI] New select component #6190
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6cd6e69
Create web component for custom UI select element
chaimann b4a474f
Move custom validity logic in its own controller
chaimann 0ce30a5
Add new UI component for select field
chaimann 528a9c4
Standardize error message casing for store credit
chaimann e827b60
Update ui/forms/field component to render new select component
chaimann a0039f9
Add conditional render of an error for fields
chaimann 2f64450
Rename web component
chaimann 05bcce9
Move custom validity logic into separate module
chaimann 549aa60
Rename feature helper to "solidus_select"
chaimann 1513722
Use customElements polyfill to add Safari support
chaimann ba59b5a
Update single select search behaviour
chaimann 252788d
Allow select dropdown to overflow modal footer
chaimann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
15 changes: 0 additions & 15 deletions
15
admin/app/components/solidus_admin/ui/forms/input/component.js
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
21 changes: 21 additions & 0 deletions
21
admin/app/components/solidus_admin/ui/forms/select/component.html.erb
This file contains hidden or 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,21 @@ | ||
| <div class="flex flex-col gap-2 w-full"> | ||
| <div class="flex gap-1 items-center"> | ||
| <label class="text-gray-700 font-semibold text-xs" <%= tag.attributes for: @attributes[:id] %>> | ||
| <%= @label %> | ||
| </label> | ||
| <%= render component('ui/toggletip').new(text: @tip) if @tip.present? %> | ||
| </div> | ||
|
|
||
| <%= tag.select(options_for_select(@choices, @selected), **@attributes) %> | ||
|
|
||
| <% if @hint.present? || @error.present? %> | ||
| <div class=" | ||
| font-normal text-xs | ||
| [:disabled~&]:text-gray-300 text-gray-500 | ||
| flex gap-1 flex-col [&>.error]:hidden [&>.error]:peer-invalid:block | ||
| "> | ||
| <%= tag.span @hint if @hint.present? %> | ||
| <%= tag.span safe_join(@error, tag.br), class: "error text-red-600" %> | ||
| </div> | ||
| <% end %> | ||
| </div> |
98 changes: 98 additions & 0 deletions
98
admin/app/components/solidus_admin/ui/forms/select/component.rb
This file contains hidden or 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,98 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class SolidusAdmin::UI::Forms::Select::Component < SolidusAdmin::BaseComponent | ||
| FONT_SIZES = { | ||
| s: "[&>.control]:text-xs [&_.dropdown]:text-xs", | ||
| m: "[&>.control]:text-sm [&_.dropdown]:text-sm", | ||
| l: "[&>.control]:text-base [&_.dropdown]:text-base", | ||
| }.freeze | ||
|
|
||
| HEIGHTS = { | ||
| control: { | ||
| s: "[&>.control]:min-h-7", | ||
| m: "[&>.control]:min-h-9", | ||
| l: "[&>.control]:min-h-12", | ||
| }, | ||
| option: { | ||
| s: "[&_.option]:h-7", | ||
| m: "[&_.option]:h-9", | ||
| l: "[&_.option]:h-12", | ||
| }, | ||
| item: { | ||
| s: "[&_.item]:h-5", | ||
| m: "[&_.item]:h-5.5", | ||
| l: "[&_.item]:h-8", | ||
| }, | ||
| }.freeze | ||
|
|
||
| def initialize(label:, name:, choices:, size: :m, hint: nil, tip: nil, error: nil, **attributes) | ||
| @label = label | ||
| @name = name | ||
| @hint = hint | ||
| @tip = tip | ||
| @error = Array.wrap(error) | ||
|
|
||
| @choices = choices | ||
| @selected = attributes.delete(:value) | ||
|
|
||
| @attributes = attributes | ||
| @attributes[:name] = @name | ||
| @attributes[:is] = "solidus-select" | ||
| @attributes[:id] ||= "#{stimulus_id}_#{@name}" | ||
| @attributes[:"data-error-message"] = @error.presence | ||
|
|
||
| general_classes = ["w-full relative text-black font-normal #{FONT_SIZES[size]}"] | ||
| control_classes = ["[&>.control]:peer-invalid:border-red-600 [&>.control]:peer-invalid:hover:border-red-600 | ||
| [&>.control]:peer-invalid:text-red-600 [&>.control]:flex [&>.control]:flex-wrap [&>.control]:items-center | ||
| [&>.control]:gap-1 [&>.control]:rounded-sm [&>.control]:w-full [&>.control]:rounded-sm [&>.control]:pl-3 | ||
| [&>.control]:pr-10 [&>.control]:py-1.5 [&>.control]:bg-white [&>.control]:border [&>.control]:border-gray-300 | ||
| [&>.control]:hover:border-gray-500 [&>.control]:has-[:disabled]:bg-gray-50 [&>.control]:has-[:disabled]:text-gray-500 | ||
| [&>.control]:has-[:disabled]:cursor-not-allowed [&>.control]:has-[:disabled]:hover:border-gray-300 | ||
| [&>.control]:has-[:focus]:ring [&>.control]:has-[:focus]:ring-gray-300 [&>.control]:has-[:focus]:ring-0.5 | ||
| [&>.control]:has-[:focus]:bg-white [&>.control]:has-[:focus]:ring-offset-0 [&>.control]:has-[:focus]:outline-none | ||
| #{HEIGHTS[:control][size]}"] | ||
|
|
||
| unless @attributes[:multiple] | ||
| control_classes << "[&>.control]:peer-invalid:bg-arrow-down-s-fill-red-400 [&>.control]:form-select | ||
| [&>.control]:bg-arrow-down-s-fill-gray-700" | ||
| end | ||
|
|
||
| item_classes = [] | ||
| if @attributes[:multiple] | ||
| item_classes << "[&_.item]:flex [&_.item]:gap-1 [&_.item]:items-center [&_.item]:rounded-full [&_.item]:whitespace-nowrap | ||
| [&_.item]:px-2 [&_.item]:py-0.5 [&_.item]:bg-graphite-light [&_.item]:peer-invalid:bg-red-100 #{HEIGHTS[:item][size]} | ||
| [&_.item_.remove-button]:text-xl [&_.item_.remove-button]:pb-0.5 [&_.item_.remove-button]:order-first | ||
| [&_.item_.remove-button]:has-[:disabled]:cursor-not-allowed" | ||
| end | ||
|
|
||
| input_classes = ["[&_input]:has-[.item]:placeholder:invisible [&_input:disabled]:cursor-not-allowed [&_input:disabled]:bg-gray-50 | ||
| [&_input]:peer-invalid:placeholder:text-red-400"] | ||
|
|
||
| unless @attributes[:multiple] | ||
| input_classes << "[&_input]:has-[.item]:opacity-0 [&_input]:has-[.item]:cursor-default" | ||
| end | ||
|
|
||
| dropdown_classes = ["[&_.dropdown]:w-full [&_.dropdown]:absolute [&_.dropdown]:border [&_.dropdown]:border-gray-100 | ||
| [&_.dropdown]:mt-0.5 [&_.dropdown]:min-w-[10rem] [&_.dropdown]:p-2 [&_.dropdown]:rounded-sm [&_.dropdown]:z-10 | ||
| [&_.dropdown]:shadow-lg [&_.dropdown]:bg-white"] | ||
|
|
||
| dropdown_content_classes = ["[&_.dropdown-content]:flex [&_.dropdown-content]:flex-col [&_.dropdown-content]:max-h-[200px] | ||
| [&_.dropdown-content]:overflow-x-hidden [&_.dropdown-content]:overflow-y-auto [&_.dropdown-content]:scroll-smooth [&_.no-results]:text-gray-500"] | ||
|
|
||
| option_classes = ["[&_.option]:p-2 [&_.option]:rounded-sm [&_.option]:min-w-fit [&_.option.active]:bg-gray-50 | ||
| [&_.option.active]:text-gray-700 [&_.option_.highlight]:bg-yellow [&_.option_.highlight]:rounded-[1px] | ||
| #{HEIGHTS[:option][size]}"] | ||
|
|
||
| @attributes[:class] = [ | ||
| "peer", | ||
| general_classes, | ||
| control_classes, | ||
| item_classes, | ||
| input_classes, | ||
| dropdown_classes, | ||
| dropdown_content_classes, | ||
| option_classes, | ||
| @attributes[:class] | ||
| ].compact.join(" ") | ||
| end | ||
| end | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ | |
| </form> | ||
| </header> | ||
|
|
||
| <div class="p-4 overflow-auto"> | ||
| <div class="p-4"> | ||
| <%= content %> | ||
| </div> | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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,2 +1,4 @@ | ||
| import "@hotwired/turbo-rails" | ||
| import "vendor/custom_elements" | ||
| import "solidus_admin/controllers" | ||
| import "solidus_admin/web_components/solidus_select" |
12 changes: 12 additions & 0 deletions
12
admin/app/javascript/solidus_admin/controllers/custom_validity_controller.js
This file contains hidden or 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 @@ | ||
| import { Controller } from "@hotwired/stimulus" | ||
| import { setValidity } from "solidus_admin/utils"; | ||
|
|
||
| export default class extends Controller { | ||
| static values = { | ||
| errorMessage: String, | ||
| } | ||
|
|
||
| connect() { | ||
| setValidity(this.element, this.errorMessageValue); | ||
| } | ||
| } |
This file contains hidden or 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
69 changes: 69 additions & 0 deletions
69
admin/app/javascript/solidus_admin/web_components/solidus_select.js
This file contains hidden or 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,69 @@ | ||
| import TomSelect from "tom-select"; | ||
| import { setValidity } from "solidus_admin/utils"; | ||
|
|
||
| class SolidusSelect extends HTMLSelectElement { | ||
| static observedAttributes = ["synced"]; | ||
|
|
||
| connectedCallback() { | ||
| const originalSelect = this; | ||
|
|
||
| const tomselect = new TomSelect(originalSelect, { | ||
| controlClass: "control", | ||
| dropdownClass: "dropdown", | ||
| dropdownContentClass: "dropdown-content", | ||
| optionClass: "option", | ||
| wrapperClass: "wrapper", | ||
| maxOptions: null, | ||
| refreshThrottle: 0, | ||
| plugins: { | ||
| no_active_items: true, | ||
| remove_button: { | ||
| append: originalSelect.multiple, | ||
| className: "remove-button" | ||
| }, | ||
| }, | ||
| onItemAdd: function() { | ||
| this.setTextboxValue(""); | ||
| if (originalSelect.multiple) this.refreshOptions(); | ||
| }, | ||
| onType: function() { | ||
| if (!originalSelect.multiple && !this.currentResults.items.length) { | ||
| this.setTextboxValue(""); | ||
| this.refreshOptions(); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| originalSelect.setAttribute("synced", "true"); | ||
|
|
||
| // set default style for inner input field | ||
| tomselect.control_input.style = | ||
| "flex: 1 1 auto;\n" + | ||
| "line-height: inherit !important;\n" + | ||
| "max-height: none !important;\n" + | ||
| "max-width: 100% !important;\n" + | ||
| "min-height: 0 !important;\n" + | ||
| "min-width: 7rem;\n" + | ||
| "outline: none !important;" | ||
|
|
||
| originalSelect.setAttribute("hidden", "true"); | ||
| originalSelect.setAttribute("aria-hidden", "true"); | ||
|
|
||
| setValidity(originalSelect, originalSelect.dataset.errorMessage); | ||
| } | ||
|
|
||
| attributeChangedCallback(name, oldValue, newValue) { | ||
| switch (name) { | ||
| case "synced": | ||
| if (newValue === "false") { | ||
| const keepNone = function() { return false; } | ||
| this.tomselect.clearOptions(keepNone); | ||
| this.tomselect.sync(); | ||
| this.setAttribute("synced", "true"); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| customElements.define("solidus-select", SolidusSelect, { extends: "select" }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.