Skip to content

Commit 2a3eb3e

Browse files
committed
Use radio buttons instead of checkboxes for accurate filter behavior
1 parent c3965ca commit 2a3eb3e

File tree

4 files changed

+40
-19
lines changed

4 files changed

+40
-19
lines changed

js/dist/hierarchy-label-checkboxes.island.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/lib/hierarchy-label-checkboxes/hierarchy-label-checkbox-filters.island.tsx

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const Label = styled.label`
1414
cursor: pointer;
1515
`
1616

17-
const Checkbox = styled.input`
17+
const Radio = styled.input`
1818
outline: 2px;
1919
clip: unset;
2020
position: relative;
@@ -26,22 +26,32 @@ const Checkbox = styled.input`
2626

2727
const FilterIsland = ({originalSelect, selectOptions}) => {
2828

29-
const onChange = (event) => {
29+
const onChange = (parentValue: string, event) => {
30+
let parent = null
3031
for (let i = 0; i < originalSelect.options.length; i++) {
31-
if (event.target.value === originalSelect.options[i].value) {
32-
originalSelect.options[i].selected = event.target.checked
32+
if (!selectOptions.find(opt => opt.value === originalSelect.options[i].value).label.startsWith('-')) {
33+
parent = originalSelect.options[i].value
34+
continue
3335
}
36+
37+
if (parentValue !== parent) continue
38+
originalSelect.options[i].selected = event.target.value === originalSelect.options[i].value
3439
}
3540
originalSelect?.closest('form').querySelector('[data-bef-auto-submit-click]')?.click();
3641
};
3742

38-
const optionSets: Array<{ label: string, options: { label: string, value: string }[] }> = []
43+
44+
const optionSets: Array<{
45+
label: string,
46+
value: string,
47+
options: { label: string, value: string }[]
48+
}> = []
3949
let parentLabel = ''
4050

4151
selectOptions.map(option => {
4252
if (!option.label.startsWith('-')) {
4353
parentLabel = option.label
44-
optionSets.push({label: option.label, options: []})
54+
optionSets.push({...option, options: []})
4555
} else {
4656
optionSets.find(item => item.label === parentLabel)?.options.push({
4757
value: option.value,
@@ -51,26 +61,38 @@ const FilterIsland = ({originalSelect, selectOptions}) => {
5161
})
5262

5363

54-
let defaultValues:Array<string> = [];
64+
let defaultValues: Array<string> = [];
5565
for (let option of originalSelect.children) {
5666
if (option.getAttribute('selected')) defaultValues.push(option.getAttribute('value'))
5767
}
5868

5969
return (
6070
<div className="hierarchy-preact-checkbox">
61-
{optionSets.map((set, i) =>
62-
<Fieldset key={i} className="preact-checkbox-item">
71+
{optionSets.map(set =>
72+
<Fieldset key={set.value} className="preact-checkbox-item">
6373
<legend>
6474
{set.label}
6575
</legend>
6676

77+
<Label>
78+
<Radio
79+
type="radio"
80+
name={set.value}
81+
defaultChecked={!set.options.find(option => defaultValues.includes(option.value))}
82+
onChange={onChange.bind(null, set.value)}
83+
data-bef-auto-submit-exclude
84+
/>
85+
- All -
86+
</Label>
87+
6788
{set.options.map(option =>
6889
<Label key={option.value}>
69-
<Checkbox
70-
type="checkbox"
90+
<Radio
91+
type="radio"
92+
name={set.value}
7193
value={option.value}
7294
defaultChecked={defaultValues.includes(option.value)}
73-
onChange={onChange}
95+
onChange={onChange.bind(null, set.value)}
7496
data-bef-auto-submit-exclude
7597
/>
7698
{option.label}

js/lib/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"emitDecoratorMetadata": true,
1616
"experimentalDecorators": true,
1717
"importHelpers": true,
18-
"target": "es2015",
18+
"target": "es2016",
1919
"module": "esnext",
2020
"types": ["node"],
2121
"lib": ["es2017", "dom", "DOM.Iterable"],

src/Plugin/better_exposed_filters/filter/PreactTaxonomyLabelHierarchyCheckbox.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44

55
namespace Drupal\stanford_fields\Plugin\better_exposed_filters\filter;
66

7-
use Drupal\better_exposed_filters\BetterExposedFiltersHelper;
87
use Drupal\Core\Form\FormStateInterface;
98

109
/**
1110
* Default widget implementation.
1211
*
1312
* @BetterExposedFiltersFilterWidget(
1413
* id = "taxonomy_label_hierarchy_checkbox",
15-
* label = @Translation("Hierarchy taxonomy labels checkboxes"),
14+
* label = @Translation("Hierarchy taxonomy labels Radios"),
1615
* )
1716
*/
1817
class PreactTaxonomyLabelHierarchyCheckbox extends PreactFiltersBase {

0 commit comments

Comments
 (0)