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

Сhange the display of inputs in constructors form #995

Merged
merged 1 commit into from
Dec 1, 2024
Merged
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
@@ -0,0 +1,26 @@
import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="constructors-form-indexing"
export default class extends Controller {
static targets = ["index"];

afterInsert(event) {
const fieldsets = this.element.querySelectorAll(":scope > .nested-fields");
const span = fieldsets[fieldsets.length - 1].querySelector("[data-constructors-form-indexing-target='index']")

if (span) {
span.textContent = `${fieldsets.length}`;
}
}

afterRemove(event) {
const fieldsets = this.element.querySelectorAll(":scope > .nested-fields");

fieldsets.forEach((fieldset, index) => {
const span = fieldset.querySelector("[data-constructors-form-indexing-target='index']");
if (span) {
span.textContent = `${index + 1}`;
}
});
}
}
17 changes: 10 additions & 7 deletions app/views/account/calculators/partials/_category_fields.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<div class="nested-fields">
<%= f.input :en_name, label: "Category Name:" %>
<%= f.input :uk_name, label: "Uk Category Name:" %>
<%= f.input :price, label: "Category Price:" %>

<%= link_to_remove_association "- Remove Category", f, class: "text-red-500 underline" %>
</div>
<fieldset class="bordered nested-fields">
<legend class="admin-legend">Category <span data-constructors-form-indexing-target="index">1</span></legend>
<div>
<%= f.input :en_name, label: "Category Name:" %>
<%= f.input :uk_name, label: "Uk Category Name:" %>
<%= f.input :price, label: "Category Price:" %>

<%= link_to_remove_association "- Remove Category", f, class: "text-red-500 underline" %>
</div>
</fieldset>
47 changes: 29 additions & 18 deletions app/views/account/calculators/partials/_field_fields.html.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
<div class="nested-fields" data-controller="field-type">
<%= f.input :en_label, label: "Field Label:" %>
<%= f.input :uk_label, label: "Uk Field Label:" %>
<%= f.input :var_name, label: "Variable Name:" %>
<%= f.input(
:kind,
collection: Field.kinds.keys.map { |key| [key.humanize, key] },
prompt: "Select Field Type",
label: "Field Type:",
input_html: { data: { field_type_target: "fieldTypeSelect" } }
) %>
<fieldset class="bordered nested-fields">
<legend class="admin-legend">Field <span data-constructors-form-indexing-target="index">1</span></legend>

<div data-controller="field-type">
<%= f.input :en_label, label: "Field Label:" %>
<%= f.input :uk_label, label: "Uk Field Label:" %>
<%= f.input :var_name, label: "Variable Name:" %>
<%= f.input(
:kind,
collection: Field.kinds.keys.map { |key| [key.humanize, key] },
prompt: "Select Field Type",
label: "Field Type:",
input_html: { data: { field_type_target: "fieldTypeSelect" } }
) %>

<div class="hidden category-fields" data-field-type-target="categoryFields">
<%= f.simple_fields_for :categories do |category_fields| %>
<%= render "account/calculators/partials/category_fields", f: category_fields %>
<% end %>
<%= link_to_add_association "+ Add Category", f, :categories, partial: "account/calculators/partials/category_fields", class: "underline" %>
<fieldset class="bordered" data-controller="constructors-form-indexing"
data-action="cocoon:after-insert->constructors-form-indexing#afterInsert cocoon:after-remove->constructors-form-indexing#afterRemove">
<legend class="admin-legend">Categories</legend>

<%= f.simple_fields_for :categories do |category_fields| %>
<%= render "account/calculators/partials/category_fields", f: category_fields %>
<% end %>
<div class="links">
<%= link_to_add_association "+ Add Category", f, :categories, partial: "account/calculators/partials/category_fields", class: "underline" %>
</div>
</fieldset>
</div>

<%= link_to_remove_association "- Remove Field", f, class: "text-red-500 underline" %>
</div>

<%= link_to_remove_association "- Remove Field", f, class: "text-red-500 underline" %>
</div>
</fieldset>
37 changes: 23 additions & 14 deletions app/views/account/calculators/partials/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
<%= simple_form_for(@calculator, url: account_calculators_path) do |f| %>
<div class="form-group row">
<div class="my-auto col-12 has-float-label">
<%= f.input :en_name, label: "Calculator Name:", class: 'form-control' %>
<%= f.input :uk_name, label: "Uk Calculator Name:", class: 'form-control' %>
<fieldset class="bordered">
<legend class="admin-legend">Calculator</legend>
<%= f.input :en_name, label: "Calculator Name:", class: 'form-control' %>
<%= f.input :uk_name, label: "Uk Calculator Name:", class: 'form-control' %>
</fieldset>

<!-- formula input-->
<div id="formulas" class="space-y-4">
<%= f.simple_fields_for :formulas do |formula_fields| %>
<%= render "account/calculators/partials/formula_fields", f: formula_fields %>
<% end %>
<div class="links">
<%= link_to_add_association "+ Add Formula", f, :formulas, partial: "account/calculators/partials/formula_fields", class: "underline" %>
</div>
<fieldset class="bordered" data-controller="constructors-form-indexing"
data-action="cocoon:after-insert->constructors-form-indexing#afterInsert cocoon:after-remove->constructors-form-indexing#afterRemove">
<%= f.simple_fields_for :formulas do |formula_fields| %>
<%= render "account/calculators/partials/formula_fields", f: formula_fields %>
<% end %>
<div class="links">
<%= link_to_add_association "+ Add Formula", f, :formulas, partial: "account/calculators/partials/formula_fields", class: "underline" %>
</div>
</fieldset>
</div>

<!-- field input-->
<div id="fields" class="space-y-4">
<%= f.simple_fields_for :fields do |field_fields| %>
<%= render "account/calculators/partials/field_fields", f: field_fields %>
<% end %>
<div class="links">
<%= link_to_add_association "Add Field", f, :fields, partial: "account/calculators/partials/field_fields", class: "underline" %>
</div>
<fieldset class="bordered" data-controller="constructors-form-indexing"
data-action="cocoon:after-insert->constructors-form-indexing#afterInsert cocoon:after-remove->constructors-form-indexing#afterRemove">
<%= f.simple_fields_for :fields do |field_fields| %>
<%= render "account/calculators/partials/field_fields", f: field_fields %>
<% end %>
<div class="links">
<%= link_to_add_association "+ Add Field", f, :fields, partial: "account/calculators/partials/field_fields", class: "underline" %>
</div>
</fieldset>
</div>
</div>
</div>
Expand Down
19 changes: 11 additions & 8 deletions app/views/account/calculators/partials/_formula_fields.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<div class="nested-fields">
<%= f.input :en_label, label: "Formula Label:" %>
<%= f.input :uk_label, label: "Uk Formula Label:" %>
<%= f.input :expression, label: "Formula Expression:" %>
<fieldset class="bordered nested-fields">
<legend class="admin-legend">Formula <span data-constructors-form-indexing-target="index">1</span></legend>
<div>
<%= f.input :en_label, label: "Formula Label:" %>
<%= f.input :uk_label, label: "Uk Formula Label:" %>
<%= f.input :expression, label: "Formula Expression:" %>

<%= f.input :uk_unit, label: "Uk Unit Label:" %>
<%= f.input :en_unit, label: "Unit Label:" %>
<%= f.input :uk_unit, label: "Uk Unit Label:" %>
<%= f.input :en_unit, label: "Unit Label:" %>

<%= link_to_remove_association "- Remove Formula", f, class: "text-red-500 underline" %>
</div>
<%= link_to_remove_association "- Remove Formula", f, class: "text-red-500 underline" %>
</div>
</fieldset>