This repository has been archived by the owner on Jan 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement multiple fields using Stimulus and Jest
Re-implements the UI for managing multiple fields using the simulus.js package and Jest for testing. Accomplishes the same feature set with less code and without any reliance on additional css classes. The only classes used are from Bootstrap. JS linting and testing is also done during the Travis build.
- Loading branch information
Showing
24 changed files
with
2,012 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
{ | ||
"env": { | ||
"browser": true, | ||
"es6": true | ||
"es6": true, | ||
"jest": true | ||
}, | ||
"extends": "standard", | ||
"globals": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Implements multiple fields in our forms by adding DOM elements to add and remove cloned fields | ||
// in the form. Relies heavily on Bootstrap's form elements. Including the proper data attributes | ||
// will trigger the insertion of the buttons to manage the additional fields. Certain html elements | ||
// are required, such as a label and the input-group css class. | ||
// @example | ||
// <div class="form-group" data-controller="fields"> | ||
// <!-- A label is required in order for the buttons to display correctly --> | ||
// <label> | ||
// Identifier | ||
// </label> | ||
// <!-- The input-goup and form=control classes are required in order for new fields to be added --> | ||
// <div class="input-group"> | ||
// <input class="form-control" /> | ||
// </div> | ||
|
||
import { Controller } from 'stimulus' | ||
|
||
export default class extends Controller { | ||
connect () { | ||
let label = this.element.getElementsByTagName('label').item(0).innerText | ||
for (let count = 1; count < this.inputGroups.length; count++) { | ||
this.inputGroups.item(count).appendChild(this.removeButton) | ||
} | ||
this.element.appendChild(this.addButton(label)) | ||
} | ||
|
||
// By default, Stimulus listens to click events on buttons and will execute this method. | ||
add (event) { | ||
event.preventDefault() | ||
this.element.insertBefore(this.newField, this.element.lastElementChild) | ||
} | ||
|
||
// By default, Stimulus listens to click events on buttons and will execute this method. | ||
remove (event) { | ||
event.preventDefault() | ||
event.target.parentElement.remove() | ||
} | ||
|
||
// @return [HTMLElement] cloned field taken from the first input group. | ||
get newField () { | ||
let clone = this.inputGroups.item(0).cloneNode(true) | ||
Array.from(clone.getElementsByClassName('form-control')).forEach((input) => { | ||
input.value = '' | ||
}) | ||
clone.appendChild(this.removeButton) | ||
return clone | ||
} | ||
|
||
// @return [HTMLElement] | ||
addButton (label) { | ||
let node = document.createElement('button') | ||
node.setAttribute('data-action', 'fields#add') | ||
node.classList.add('btn', 'btn-outline-success', 'btn-sm') | ||
let content = document.createTextNode('Add another ' + label) | ||
node.appendChild(content) | ||
return node | ||
} | ||
|
||
// @return [HTMLElement] | ||
get removeButton () { | ||
let node = document.createElement('button') | ||
node.setAttribute('data-action', 'fields#remove') | ||
node.classList.add('btn', 'btn-outline-danger', 'btn-sm') | ||
let content = document.createTextNode('Remove') | ||
node.appendChild(content) | ||
return node | ||
} | ||
|
||
// @return [HTMLCollection] | ||
get inputGroups () { | ||
return this.element.getElementsByClassName('input-group') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Load all the controllers within this directory and all subdirectories. | ||
// Controller files must be named *_controller.js. | ||
|
||
import { Application } from 'stimulus' | ||
import { definitionsFromContext } from 'stimulus/webpack-helpers' | ||
|
||
const application = Application.start() | ||
const context = require.context('controllers', true, /_controller\.js$/) | ||
application.load(definitionsFromContext(context)) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import 'controllers' |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,26 @@ | ||
<%- field.values.each do |value| %> | ||
|
||
<div class="container ff-group"> | ||
<div class="row"> | ||
<div class="col-md-12 col-lg-3"> | ||
<%= label_tag :role %> | ||
<%= text_field_tag "#{form.object_name}[creator][][role]", value.fetch(:role, nil), | ||
field.options.merge!(list: "#{field.label}_role") %> | ||
<datalist id="<%= "#{field.label}_role" %>"> | ||
<% field.datalist(component: :roles).each do |item| %> | ||
<option value="<%= item %>"><%= item.label %></option> | ||
<% end %> | ||
</datalist> | ||
</div> | ||
|
||
<div class="col-md-12 col-lg-3"> | ||
<%= label_tag :agent %> | ||
<%= text_field_tag "#{form.object_name}[creator][][agent]", value.fetch(:agent, nil), | ||
field.options.merge!(list: "#{field.label}_agent") %> | ||
<datalist id="<%= "#{field.label}_agent" %>"> | ||
<% field.datalist(component: :agents).each do |item| %> | ||
<option value="<%= item.id %>"><%= item %></option> | ||
<% end %> | ||
</datalist> | ||
</div> | ||
<div class="input-group"> | ||
<div class="input-group-prepend"> | ||
<span class="input-group-text"><%= t('cho.field_label.name_and_role') %></span> | ||
</div> | ||
|
||
<%= text_field_tag "#{form.object_name}[creator][][agent]", value.fetch(:agent, nil), | ||
field.options.merge!(list: "#{field.label}_agent") %> | ||
<datalist id="<%= "#{field.label}_agent" %>"> | ||
<% field.datalist(component: :agents).each do |item| %> | ||
<option value="<%= item.id %>"><%= item %></option> | ||
<% end %> | ||
</datalist> | ||
|
||
<%= text_field_tag "#{form.object_name}[creator][][role]", value.fetch(:role, nil), | ||
field.options.merge!(list: "#{field.label}_role") %> | ||
<datalist id="<%= "#{field.label}_role" %>"> | ||
<% field.datalist(component: :roles).each do |item| %> | ||
<option value="<%= item %>"><%= item.label %></option> | ||
<% end %> | ||
</datalist> | ||
|
||
</div> | ||
|
||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<% field.values.each do |value| %> | ||
<%= content_tag :div, class: 'input-group ff-group' do %> | ||
<%= content_tag :div, class: 'input-group' do %> | ||
<%= form.text_field field.label, field.options.merge(multiple: field.multiple?, value: value) %> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<% field.values.each do |value| %> | ||
<%= content_tag :div, class: 'input-group ff-group' do %> | ||
<%= content_tag :div, class: 'input-group' do %> | ||
<%= form.text_area field.label, field.options.merge(multiple: field.multiple?, value: value) %> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.