Skip to content

Commit 836ba88

Browse files
psaizzzacharo
authored andcommitted
selector: Allow the same value in multiple children aggregations
1 parent 425334f commit 836ba88

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

src/lib/components/BucketAggregation/BucketAggregationValues.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,14 @@ const ValueElement = ({
134134
overridableId,
135135
keyField,
136136
}) => {
137-
const { buildUID } = useContext(AppContext);
137+
const { buildUID, nextComponentIndex } = useContext(AppContext);
138138
const label = bucket.label
139139
? bucket.label
140140
: `${keyField} (${bucket.doc_count.toLocaleString("en-US")})`;
141141
const childAggCmps = getChildAggCmps(bucket);
142-
142+
/* Note that the checkbox does not have any `id`. It used to have one, based on the value, and the issue
143+
was that, if the same value appeared more than once, react didn't like having two items with the same id,
144+
and clicking on the second would select the first one */
143145
return (
144146
<Overridable
145147
id={buildUID("BucketAggregationValues.element", overridableId)}
@@ -152,7 +154,7 @@ const ValueElement = ({
152154
<List.Item key={bucket.key}>
153155
<Checkbox
154156
label={label}
155-
id={`${bucket.key}-agg-value-checkbox`}
157+
id={nextComponentIndex()}
156158
value={bucket.key}
157159
onClick={() => onFilterClicked(bucket.key)}
158160
checked={isSelected}

src/lib/components/ReactSearchKit/ReactSearchKit.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,18 @@ export class ReactSearchKit extends Component {
3535
this.store = createStoreWithConfig(appConfig);
3636
this.appName = props.appName;
3737
this.eventListenerEnabled = props.eventListenerEnabled;
38+
this.componentIndex = 0;
3839
}
39-
4040
render() {
4141
const { appName, children, eventListenerEnabled, overridableId, searchOnInit } =
4242
this.props;
4343

4444
const context = {
4545
appName,
4646
buildUID: (element, overrideId) => buildUID(element, overrideId, appName),
47+
/* This is an internal function that will be used to create id for the checkboxes and labels, ensuring that
48+
they are unique */
49+
nextComponentIndex: () => `${this.appName}_${this.componentIndex++}`,
4750
};
4851

4952
const _overridableId = buildUID("ReactSearchKit.children", overridableId, appName);

src/lib/state/selectors/query.test.js

+36
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ describe("queries with second level filters.", () => {
154154
["type", "Publication"],
155155
]);
156156
});
157+
157158
test("query with terms that start with the same text and with children", () => {
158159
const state = [
159160
["file_type", "pdf", ["sub_type", "image"]],
@@ -165,6 +166,41 @@ describe("queries with second level filters.", () => {
165166

166167
expect(newState).toEqual([["file_type", "pdf2"]]);
167168
});
169+
170+
test("query with `subtype: Validation` should add it to the correct type", () => {
171+
const state = [
172+
["file_type", "pdf"],
173+
["type", "Documentation", ["subtype", "Validation"]],
174+
];
175+
176+
const query = ["type", "Publication", ["subtype", "Validation"]];
177+
178+
const newState = updateQueryFilters(query, state);
179+
180+
expect(newState).toEqual([
181+
["file_type", "pdf"],
182+
["type", "Documentation", ["subtype", "Validation"]],
183+
["type", "Publication", ["subtype", "Validation"]],
184+
]);
185+
});
186+
187+
test("query with `subtype: Validation` should remove it from the correct type", () => {
188+
const state = [
189+
["file_type", "pdf"],
190+
["type", "Documentation", ["subtype", "Validation"]],
191+
["type", "Publication", ["subtype", "Validation"]],
192+
];
193+
194+
const query = ["type", "Publication", ["subtype", "Validation"]];
195+
196+
const newState = updateQueryFilters(query, state);
197+
198+
expect(newState).toEqual([
199+
["file_type", "pdf"],
200+
["type", "Documentation", ["subtype", "Validation"]],
201+
["type", "Publication"],
202+
]);
203+
});
168204
});
169205

170206
describe("queries with third level filters.", () => {

0 commit comments

Comments
 (0)