-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 %> |
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]"]'); | ||
this.endInput = this.element.querySelector('input[name="range[date_range][end]"]'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use a stimulus target here rather than doing a querySelector? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't we just set both fields to required statically? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', ''); | ||
} | ||
} | ||
} |
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?