Skip to content

Commit

Permalink
Fix datepicker cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverguenther committed Oct 11, 2024
1 parent bebb971 commit c78fb7f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 10 additions & 7 deletions frontend/src/stimulus/controllers/dynamic/subform.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ export default class SubformController extends Controller {

declare readonly tableTarget:HTMLTableElement;

private rowTemplate:HTMLElement;
private rowTemplate:string;

connect() {
// We can't use a target as we duplicate the "template" without much cleaning up
const item = this.element.querySelector('[data-row-target]') as HTMLElement;
this.rowTemplate = item;
this.rowTemplate = item.outerHTML;
item.remove();
}

Expand All @@ -54,13 +54,16 @@ export default class SubformController extends Controller {

addRow() {
const index = this.itemCount;
const newRow = this.rowTemplate.cloneNode(true) as HTMLElement;
this.tableTarget.appendChild(newRow);
newRow.style.removeProperty('display');
newRow.outerHTML = newRow.outerHTML.replace(/INDEX/g, index.toString());
const newRow = this.rowTemplate.replace(/INDEX/g, index.toString());
this.tableTarget.insertAdjacentHTML('beforeend', newRow);

const last = this.tableTarget.lastElementChild as HTMLElement;
last.style.removeProperty('display'); // Show the row

// Autofocus
setTimeout(() => newRow.querySelector<HTMLElement>('input')?.focus(), 10);
setTimeout(() => {
last.querySelector<HTMLElement>('input')?.focus();
}, 50);
}

get itemCount():number {
Expand Down
2 changes: 2 additions & 0 deletions modules/costs/spec/features/members_hourly_rates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def add_rate(rate:, date:)
click_link_or_button "Add rate"

datepicker = Components::BasicDatepicker.new
datepicker.expect_visible
datepicker.set_date(date.strftime("%Y-%m-%d"))

within "tr[id^='user_new_rate_attributes_']" do
Expand All @@ -68,6 +69,7 @@ def change_rate_date(from:, to:)
input = find("table.rates .date input[data-value='#{from.strftime('%Y-%m-%d')}']")
input.click
datepicker = Components::BasicDatepicker.new
datepicker.expect_visible
datepicker.set_date(to.strftime("%Y-%m-%d"))
end

Expand Down

0 comments on commit c78fb7f

Please sign in to comment.