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

Manage required attribute on date range inputs #781

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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,12 @@
<%= form_tag search_action_path, method: :get, class: [@classes[:form], "range_#{@facet_field.key} d-flex justify-content-center"].join(' '), data: { controller: "range-limit" } do %>
<%= render hidden_search_state %>

<div class="input-group input-group-sm mb-3 flex-nowrap range-limit-input-group">
<%= render_range_input(:begin, begin_label) %>
<%= render_range_input(:end, end_label) %>
<div class="input-group-append visually-hidden">
<%= submit_tag t('blacklight.range_limit.submit_limit'), class: @classes[:submit], name: nil %>
</div>
<%= submit_tag t('blacklight.range_limit.submit_limit'), class: @classes[:submit] + " sr-only", name: nil, data: { range_limit_target: "submitButton" } %>
</div>
<% end %>
25 changes: 25 additions & 0 deletions app/javascript/controllers/range_limit_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
connect() {
this.beginInput = this.element.querySelector('input[name="range[date_range][begin]"]');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use a stimulus target here rather than doing a querySelector?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found this preferable to monkey-patching or subclassing RangeFormComponent to get at render_range_input to add the data target. Unless there's an easier way I'm missing?

this.endInput = this.element.querySelector('input[name="range[date_range][end]"]');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use a stimulus target here rather than doing a querySelector?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found this preferable to monkey-patching or subclassing RangeFormComponent to get at render_range_input to add the data target. Unless there's an easier way I'm missing?

this.beginInput.addEventListener('input', event => this.updateInputRequiredState(event));
this.endInput.addEventListener('input', event => this.updateInputRequiredState(event));
}

updateInputRequiredState(event) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we just set both fields to required statically?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to maintain the user's current ability to reset the date range search by blanking out both sides of the range. You're correct in that my logic could be consolidated though, thanks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hadn't considered that. Can you add a comment about that use case here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I added a comment and a note to check the template because it's only overridden to set the data target. I think it's likely this code gets removed as a result of the production WC.

const bothEmpty = this.beginInput.value === '' && this.endInput.value === ''

// Allow the user to submit an empty range to clear the search, otherwise require both bounds.
// If blacklight_range_limit is updated or our config changes, review if this and the override
// to range_form_component.html.erb are still necessary.
if (bothEmpty) {
this.beginInput.removeAttribute('required');
this.endInput.removeAttribute('required');
} else {
this.beginInput.setAttribute('required', '');
this.endInput.setAttribute('required', '');
}
}
}
35 changes: 35 additions & 0 deletions spec/features/range_limit_facet_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Range Limit Facet', :js do
it 'does not accept an incomplete range' do
visit search_catalog_path

click_on 'Show facets' if page.has_button?('Show facets')

expect(page).to have_button('Date range')
click_on 'Date range'

# Both empty
expect(page).to have_no_css('input#range_date_range_begin[required]')
expect(page).to have_no_css('input#range_date_range_end[required]')

# Only begin has a value
fill_in 'range_date_range_begin', with: '1986'
expect(page).to have_css('input#range_date_range_begin[required]')
expect(page).to have_css('input#range_date_range_end[required]')

# Only end has a value
fill_in 'range_date_range_begin', with: ''
fill_in 'range_date_range_end', with: '1991'
expect(page).to have_css('input#range_date_range_begin[required]')
expect(page).to have_css('input#range_date_range_end[required]')

# Both have values
fill_in 'range_date_range_begin', with: '1986'
fill_in 'range_date_range_end', with: '1991'
expect(page).to have_css('input#range_date_range_begin[required]')
expect(page).to have_css('input#range_date_range_end[required]')
end
end