Skip to content

Commit

Permalink
make "Other" question option more explicit as an affordance
Browse files Browse the repository at this point in the history
  • Loading branch information
aktfrikshun authored Apr 4, 2022
1 parent 1947889 commit 7937afc
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 2 deletions.
18 changes: 17 additions & 1 deletion app/controllers/admin/question_options_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Admin::QuestionOptionsController < AdminController
before_action :set_question, only: [:new, :create, :show, :edit, :update, :destroy]
before_action :set_question, only: [:new, :create, :create_other, :show, :edit, :update, :destroy]
before_action :set_question_option, only: [:show, :edit, :update, :destroy]

def index
Expand Down Expand Up @@ -41,6 +41,10 @@ def create

if text_array.length > 0
text_array.each do | txt |
if (txt.upcase == 'OTHER' || txt.upcase == 'OTRO')
@errors << "Use add #{txt} button"
next
end
question_option = QuestionOption.where(question_id: params[:question_id], text: txt).first
if question_option
@errors << "Question option already exists for text #{txt}"
Expand Down Expand Up @@ -71,6 +75,18 @@ def create
render :create, format: :js
end

def create_other
@errors = []
question_option = QuestionOption.new()
question_option.position = @question.question_options.size + 1
question_option.question_id = @question.id
question_option.text = "Other"
question_option.value = "OTHER"
question_option.save!
@question_options = [ question_option ]
render :create, format: :js
end

def update
@question_option.update(question_option_params)
render :update, format: :js
Expand Down
5 changes: 5 additions & 0 deletions app/views/components/forms/edit/_builder.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ $(function() {
});
});

$('.form-builder').on("click", '.form-add-other-question-option', function(event) {
event.preventDefault();
$(this).closest('div.new-question-options-div').find('.form-add-other-question-option').hide();
});

$('.question').on("click", ".edit.button", function(event) {
event.preventDefault();
var contentDiv = $(this).closest('.question-option');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@
<i class="fa fa-plus"></i>
Add Checkbox Option
<% end %>
<% if !question.question_options.detect{ | option | option.value.upcase == 'OTHER' || option.value.upcase == 'OTRO'} %>
<%= link_to create_other_admin_form_question_question_options_path(question.form, question),method: :post, remote: true, class: "usa-button usa-button--outline form-add-other-question-option" do %>
<i class="fa fa-plus"></i>
Add Other Option
<% end %>
<% end %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<%= render 'components/forms/edit/question_types/update_value_text_fields', { question: question, option: option } %>
<% end %>
<% if ["OTHER", "OTRO"].include?(option.text.upcase) %>
<%= label_tag(nil, for: "#{question.answer_field}_other", class: "usa-input__label") do %><%= t 'form.enter_other_text' %><% end %>
<input type="text" name="<%= question.answer_field %>_other" id="<%= question.answer_field %>_other" data-option-id="<%= option.id %>" placeholder="<%= t 'form.enter_other_text' %>" class="usa-input other-option" />
<br/>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<%= render 'components/forms/edit/question_types/update_value_text_fields', { question: question, option: option } %>
<% end %>
<% if ["OTHER", "OTRO"].include?(option.text.upcase) %>
<%= label_tag(nil, for: "#{question.answer_field}_other", class: "usa-input__label") do %><%= t 'form.enter_other_text' %><% end %>
<input type="text" name="<%= question.answer_field %>_other" id="<%= question.answer_field %>_other" data-option-id="<%= option.id %>" placeholder="<%= t 'form.enter_other_text' %>" class="usa-input other-option" />
<br/>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@
<i class="fa fa-plus"></i>
Add Radio Button Option
<% end %>
<% if !question.question_options.detect{ | option | option.value.upcase == 'OTHER' || option.value.upcase == 'OTRO'} %>
<%= link_to create_other_admin_form_question_question_options_path(question.form, question),method: :post, remote: true, class: "usa-button usa-button--outline form-add-other-question-option" do %>
<i class="fa fa-plus"></i>
Add Other Option
<% end %>
<% end %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<%= check_box_tag(@option_id, option.value, false, { name: question.answer_field, multiple: true, class: "usa-checkbox__input usa-checkbox__input--tile", required: question.is_required }) %>
<%= label_tag(nil, for: @option_id, class: "usa-checkbox__label") do %><%= option.text %><% end %>
<% if ["OTHER", "OTRO"].include?(option.text.upcase) %>
<%= label_tag(nil, for: "#{question.answer_field}_other", class: "usa-input__label") do %><%= t 'form.enter_other_text' %><% end %>
<input type="text" name="<%= question.answer_field %>_other" id="<%= question.answer_field %>_other" data-option-id="<%= @option_id %>" placeholder="<%= t 'form.enter_other_text' %>" class="usa-input other-option" />
<br/>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<%= radio_button_tag(@option_id, option.value, nil, { id: @option_id, name: question.answer_field, class: "usa-radio__input usa-radio__input--tile", required: question.is_required }) %>
<%= label_tag(@option_id, nil, class: "usa-radio__label") do %><%= option.text %><% end %>
<% if ["OTHER", "OTRO"].include?(option.text.upcase) %>
<%= label_tag(nil, for: "#{question.answer_field}_other", class: "usa-input__label") do %><%= t 'form.enter_other_text' %><% end %>
<input type="text" name="<%= question.answer_field %>_other" id="<%= question.answer_field %>_other" data-option-id="<%= @option_id %>" placeholder="<%= t 'form.enter_other_text' %>" class="usa-input other-option" />
<br/>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions app/views/components/widget/_fba.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ function FBAform(d, N) {
var params = {
<%# Dynamically write the Form parameters for Custom Forms %>
<% form.questions.each do |question| %>
<% next unless question_type_javascript_params(question).present? %>
<%= question.answer_field %>:
<%= raw question_type_javascript_params(question) %>,
<% end %>
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@
end
resources :question_options, except: [:index, :show] do
patch "update_title", to: "question_options#update_title", as: :inline_update
collection do
post "create_other", to: "question_options#create_other", as: :create_other
end
end
collection do
patch "sort", to: "questions#sort", as: :sort_questions
Expand Down
2 changes: 1 addition & 1 deletion spec/features/embedded_touchpoints_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
click_on "Next"

expect(page).to have_content("Option elements")
find("#question_option_4 label").click
find("#question_option_4 .usa-radio__label").click
fill_in("answer_04_other", with: "otro 2")

all('.usa-checkbox__label').each do |z|; z.click; end
Expand Down

0 comments on commit 7937afc

Please sign in to comment.