Skip to content

Commit

Permalink
Add 'selected' option to Select field and update specs
Browse files Browse the repository at this point in the history
Introduced a 'selected' option in the Select field to specify a default value when no data is available. Updated the form partial to prioritize 'selected' over data and added related test cases to ensure correct behavior. Updated .gitignore and added .tool-versions for environment settings.

Add max_items, selected, multiple and include_blank.

Added functionality to select, updated simuluscontroller to listen to the added data attributes, Updated Trix editor
  • Loading branch information
Thrizian authored and nickcharlton committed Jan 20, 2025
1 parent ccf0703 commit 335523d
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 8 deletions.
23 changes: 22 additions & 1 deletion app/assets/builds/administrate/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -22146,8 +22146,29 @@
// app/assets/javascripts/administrate/controllers/select_controller.js
var import_jquery2 = __toESM(require_jquery());
var select_controller_default = class extends Controller {
static values = {
selected: { Array, default: [] },
// Use an array to support multiple selected values
includeBlank: { type: Boolean, default: false },
multiple: { type: Boolean, default: false },
maxItems: { type: Number }
};
connect() {
(0, import_jquery2.default)(this.element).selectize({});
var optionsCount = null;
if (this.multipleValue) {
optionsCount = 1;
}
if (this.maxItemsValue) {
optionsCount = this.maxItemsValue;
}
const selectElement = (0, import_jquery2.default)(this.element).selectize({
allowEmptyOption: this.includeBlankValue,
maxItems: optionsCount
});
const selectizeInstance = selectElement[0].selectize;
if (this.selectedValue.length > 0) {
selectizeInstance.setValue(this.selectedValue);
}
}
};

Expand Down
4 changes: 2 additions & 2 deletions app/assets/builds/administrate/application.js.map

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,28 @@ import { Controller } from "@hotwired/stimulus";
import $ from "jquery";

export default class extends Controller {
static values = {
selected: { Array , default: [] }, // Use an array to support multiple selected values
includeBlank: { type: Boolean, default: false },
multiple: { type: Boolean, default: false },
maxItems: { type: Number }
}

connect() {
$(this.element).selectize({});
var optionsCount = null
if (this.multipleValue) { optionsCount = 1 }
if (this.maxItemsValue) { optionsCount = this.maxItemsValue }

const selectElement = $(this.element).selectize({
allowEmptyOption: this.includeBlankValue,
maxItems: optionsCount
});

const selectizeInstance = selectElement[0].selectize;

// Set the default selected value(s)
if (this.selectedValue.length > 0) {
selectizeInstance.setValue(this.selectedValue);
}
}
};
}
17 changes: 14 additions & 3 deletions app/views/fields/select/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,21 @@ to be displayed on a resource's edit form page.
field.attribute,
options_for_select(
field.selectable_options,
field.data,
field.data
),
{include_blank: field.include_blank_option},
data: {controller: field.html_controller}
{
include_blank: field.include_blank_option,
multiple: field.options[:multiple],
},
data: {
controller: field.html_controller,
select: {
selected_value: Array.wrap(field.selected).to_json,
include_blank_value: field.include_blank_option,
multiple_value: field.options[:multiple],
max_items_value: field.options[:max_items]
}
}
)
%>
</div>
8 changes: 8 additions & 0 deletions lib/administrate/field/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ def include_blank_option
options.fetch(:include_blank, false)
end

def selected
options.fetch(:selected, nil)
end

def max_items
options.fetch(:max_items, nil)
end

def active_record_enum?
resource.class.defined_enums.key?(attribute.to_s)
end
Expand Down
27 changes: 27 additions & 0 deletions spec/administrate/views/fields/select/_edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
data: false,
selectable_options: [true, false, nil],
include_blank_option: false,
selected: nil,
html_controller: "select"
)

Expand All @@ -34,6 +35,7 @@
data: "Yes",
selectable_options: ["Yes", "No"],
include_blank_option: "Unknown",
selected: nil,
html_controller: "select"
)

Expand All @@ -49,4 +51,29 @@
text: "Unknown"
)
end

it "uses the selected value when given" do
customer = build(:customer)
select = instance_double(
"Administrate::Field::Select",
attribute: :email_subscriber,
data: nil,
selectable_options: ["Yes", "No"],
include_blank_option: false,
selected: "No",
html_controller: "select"
)

fields model: customer do |f|
render(
partial: "fields/select/form",
locals: {field: select, f: f}
)
end

expect(rendered).to have_css(
%(select[name="customer[email_subscriber]"][data-controller~="select"] option[value="No"][selected="selected"]),
text: "No"
)
end
end

0 comments on commit 335523d

Please sign in to comment.