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

feature: Implement multiple option for select field #3572

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<%= field_wrapper(**field_wrapper_args) do %>
<%= @form.select @field.id, options_for_select(@field.options_for_select, selected: @field.value), {
include_blank: @field.include_blank
include_blank: @field.include_blank,
multiple: @field.multiple
},
aria: {
placeholder: @field.placeholder
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20250103192051_add_sizes_to_products.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddSizesToProducts < ActiveRecord::Migration[6.1]
def change
add_column :products, :sizes, :string, array: true, default: []
end
end
15 changes: 14 additions & 1 deletion lib/avo/fields/select_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Fields
class SelectField < BaseField
include Avo::Fields::FieldExtensions::HasIncludeBlank

attr_reader :display_value
attr_reader :display_value, :multiple

def initialize(id, **args, &block)
args[:placeholder] ||= I18n.t("avo.choose_an_option")
Expand All @@ -19,6 +19,7 @@ def initialize(id, **args, &block)
end

@enum = args[:enum]
@multiple = args[:multiple]
@display_value = args[:display_value] || false
end

Expand Down Expand Up @@ -55,6 +56,18 @@ def label
display_value ? value : options.invert[value]
end

def to_permitted_param
@multiple ? {"#{id}": []} : id
end

def fill_field(record, key, value, params)
if @multiple
value = value.reject(&:blank?)
end

super
end

private

def options
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/app/avo/resources/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ def fields
field :description, as: :tiptap, placeholder: "Enter text", always_show: false
field :image, as: :file, is_image: true
field :category, as: :select, enum: ::Product.categories
field :sizes, as: :select, multiple: true, options: {Large: :large, Medium: :medium, Small: :small}
end
end
3 changes: 2 additions & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[8.0].define(version: 2024_11_14_165947) do
ActiveRecord::Schema[8.0].define(version: 2025_01_03_192051) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_catalog.plpgsql"

Expand Down Expand Up @@ -173,6 +173,7 @@
t.datetime "updated_at", null: false
t.integer "price_cents", default: 0, null: false
t.string "price_currency", default: "'USD'::character varying", null: false
t.string "sizes", default: [], array: true
end

create_table "projects", force: :cascade do |t|
Expand Down
30 changes: 30 additions & 0 deletions spec/features/avo/select_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,34 @@ def test_array
end
end
end
describe "when options are multiple select" do
context "new" do
it "allow selection of multiple values" do
visit avo.new_resources_product_path
expect(page).to have_select "product_sizes", multiple: true, options: ["Large", "Medium", "Small"]
select "Large", from: "product_sizes"
select "Medium", from: "product_sizes"
save
expect(Product.last.sizes).to match_array(["large", "medium"])
end
end
context "edit" do
let(:product) { create :product, sizes: [:large] }
it "allow changing of selected values" do
visit avo.edit_resources_product_path(product)
expect(page).to have_select "product_sizes", selected: ["Large"], multiple: true, options: ["Large", "Medium", "Small"]
select "Medium", from: "product_sizes"
select "Small", from: "product_sizes"
save
expect(Product.last.sizes).to match_array(["medium", "small", "large"])
end
it "allow deselecting of previously selected values" do
visit avo.edit_resources_product_path(product)
expect(page).to have_select "product_sizes", selected: ["Large"], multiple: true, options: ["Large", "Medium", "Small"]
page.unselect "Large", from: "Sizes"
save
expect(Product.last.sizes).to match_array([])
end
end
end
end