Skip to content

Commit 78e80ac

Browse files
committed
Fix person form UX issues:
- Fix indexes for unsaved people. Fixes #1295 - Ensure delete button works on newly-added people. Fixes #1304 - Ensure autocomplete works on existing people. Fixes #1305
1 parent 5688a78 commit 78e80ac

2 files changed

Lines changed: 24 additions & 13 deletions

File tree

app/assets/javascripts/people.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,21 @@ const People = {
1313
// Replace the placeholder index with the actual index
1414
newForm = $(newForm.replace(/replace-me/g, index + 1));
1515
newForm.appendTo(list);
16-
const nameInput = newForm.find('.person-name');
17-
const orcidInput = newForm.find('.person-orcid');
16+
17+
People.bind(newForm);
18+
19+
return newForm;
20+
},
21+
22+
delete: function () {
23+
$(this).parents('.person-form').fadeOut('fast', function() {
24+
$(this).remove();
25+
});
26+
},
27+
28+
bind: function (element) {
29+
const nameInput = element.find('.person-name');
30+
const orcidInput = element.find('.person-orcid');
1831
const opts = {
1932
orientation: 'top',
2033
triggerSelectOnValidInput: false,
@@ -32,19 +45,11 @@ const People = {
3245
formatResult: Autocompleters.formatResultWithHint
3346
}
3447

35-
opts.serviceUrl = list.parents('[data-role="people-form"]').data('autocompleteUrl');
48+
opts.serviceUrl = element.parents('[data-role="people-form"]').data('autocompleteUrl');
3649
opts.dataType = 'json';
3750
opts.deferRequestBy = 100;
3851

3952
nameInput.autocomplete(opts);
40-
41-
return newForm;
42-
},
43-
44-
delete: function () {
45-
$(this).parents('.person-form').fadeOut('fast', function() {
46-
$(this).remove();
47-
});
4853
},
4954

5055
init: function () {
@@ -70,8 +75,14 @@ const People = {
7075
nextItem.find('input.form-control:first').focus();
7176
}
7277
});
78+
79+
// Set up autocomplete on any existing fields
80+
form.find('.person-form').each(function (node) {
81+
People.bind($(this));
82+
});
83+
84+
$(form).on('change', '.delete-person-btn input.destroy-attribute', People.delete);
7385
});
7486

75-
$('.delete-person-btn input.destroy-attribute').change(People.delete);
7687
}
7788
};

app/views/common/_people_form.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<div data-role="people-form-list">
2323
<% f.object.send(field_name).each_with_index do |person, index| %>
2424
<%= render partial: 'common/person_form',
25-
locals: { field_prefix: "#{model_name}[#{field_name}]", index: person.id, person: person, role: role } %>
25+
locals: { field_prefix: "#{model_name}[#{field_name}]", index: person.id || (index + 1), person: person, role: role } %>
2626
<% end %>
2727
</div>
2828

0 commit comments

Comments
 (0)