Skip to content

Commit

Permalink
Add max_items, selected, multiple and include_blank.
Browse files Browse the repository at this point in the history
Added functionality to select, updated simuluscontroller to listen to the added data attributes, Updated Trix editor
  • Loading branch information
Thrizian committed Oct 11, 2024
1 parent 854fe27 commit 0cce175
Show file tree
Hide file tree
Showing 6 changed files with 557 additions and 461 deletions.
960 changes: 510 additions & 450 deletions app/assets/builds/administrate/application.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 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.selected.presence || 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>
4 changes: 4 additions & 0 deletions lib/administrate/field/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ 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
4 changes: 2 additions & 2 deletions spec/example_app/app/views/admin/stats/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div style="padding: 20px">
<h1>Stats</h1>
<br>
<p><b>Total Customers:</b> <%= @stats[:customer_count] %></h1>
<p><b>Total Customers:</b> <%= @stats[:customer_count] %></h1></p>
<br>
<p><b>Total Orders:</b> <%= @stats[:order_count] %></h1>
<p><b>Total Orders:</b> <%= @stats[:order_count] %></h1></p>
</div>

0 comments on commit 0cce175

Please sign in to comment.