Skip to content

Commit fcdafdd

Browse files
committed
keep old format if group_by is not set
1 parent e1736dc commit fcdafdd

File tree

6 files changed

+57
-51
lines changed

6 files changed

+57
-51
lines changed

src/Autocomplete/CHANGELOG.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22

33
## 2.8.0
44

5-
- Added support for using `Option Group`: the JSON format with option groupe takes two entries
6-
`options` and `optgroups`
5+
- Added support for using `Option Group`: the JSON format with option groupe takes two entries
6+
`options` and `optgroups`
77

88
```json
99
{
10-
"results": {
11-
"options": [
12-
{"value": "1", "text": "Pizza", "group_by": ["food"]},
13-
{"value": "2", "text": "Banana", "group_by": ["food"]}
14-
],
15-
"optgroups":[
16-
{"value":"food","label":"food"}
17-
]
18-
}
10+
"results": {
11+
"options": [
12+
{ "value": "1", "text": "Pizza", "group_by": ["food"] },
13+
{ "value": "2", "text": "Banana", "group_by": ["food"] }
14+
],
15+
"optgroups": [{ "value": "food", "label": "food" }]
16+
}
1917
}
2018
```
2119

src/Autocomplete/assets/test/controller.test.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -108,32 +108,28 @@ describe('AutocompleteController', () => {
108108
fetchMock.mock(
109109
'/path/to/autocomplete?query=',
110110
JSON.stringify({
111-
results: {
112-
options: [
113-
{
114-
value: 3,
115-
text: 'salad'
116-
},
117-
]
118-
},
111+
results: [
112+
{
113+
value: 3,
114+
text: 'salad'
115+
},
116+
]
119117
}),
120118
);
121119

122120
fetchMock.mock(
123121
'/path/to/autocomplete?query=foo',
124122
JSON.stringify({
125-
results: {
126-
options: [
127-
{
128-
value: 1,
129-
text: 'pizza'
130-
},
131-
{
132-
value: 2,
133-
text: 'popcorn'
134-
}
135-
]
136-
},
123+
results: [
124+
{
125+
value: 1,
126+
text: 'pizza'
127+
},
128+
{
129+
value: 2,
130+
text: 'popcorn'
131+
}
132+
]
137133
}),
138134
);
139135

@@ -309,7 +305,7 @@ describe('AutocompleteController', () => {
309305
});
310306
});
311307

312-
it('connect with ajax URL and group_by option', async () => {
308+
it('group related options', async () => {
313309
const container = mountDOM(`
314310
<label for="the-select">Items</label>
315311
<select

src/Autocomplete/src/AutocompleteResults.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,16 @@ public function __construct(
2323
public bool $hasNextPage,
2424
) {
2525
}
26+
27+
/**
28+
* @return array{options: array{text: string, value: mixed, group_by?: string}, optgroups: array{label: string, value: string}}
29+
*/
30+
public function getResults(): array
31+
{
32+
if (!empty($this->optgroups)) {
33+
return ['options' => $this->options, 'optgroups' => $this->optgroups];
34+
}
35+
36+
return $this->options;
37+
}
2638
}

src/Autocomplete/src/Controller/EntityAutocompleteController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function __invoke(string $alias, Request $request): Response
5050
}
5151

5252
return new JsonResponse([
53-
'results' => ['options' => $data->options, 'optgroups' => $data->optgroups],
53+
'results' => $data->getResults(),
5454
'next_page' => $nextPage,
5555
]);
5656
}

src/Autocomplete/tests/Functional/CustomAutocompleterTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public function testItReturnsBasicResults(): void
3535
->throwExceptions()
3636
->get('/test/autocomplete/custom_product')
3737
->assertSuccessful()
38-
->assertJsonMatches('length(results.options)', 3)
39-
->assertJsonMatches('results.options[0].value', $product->getId())
40-
->assertJsonMatches('results.options[0].text', 'foo')
38+
->assertJsonMatches('length(results)', 3)
39+
->assertJsonMatches('results[0].value', $product->getId())
40+
->assertJsonMatches('results[0].text', 'foo')
4141
->get('/test/autocomplete/custom_product?query=bar')
42-
->assertJsonMatches('length(results.options)', 2)
42+
->assertJsonMatches('length(results)', 2)
4343
;
4444
}
4545

@@ -54,8 +54,8 @@ public function testItUsesTheCustomQuery(): void
5454
->throwExceptions()
5555
->get('/test/autocomplete/custom_product?query=foo')
5656
->assertSuccessful()
57-
->assertJsonMatches('length(results.options)', 1)
58-
->assertJsonMatches('results.options[0].text', 'foo')
57+
->assertJsonMatches('length(results)', 1)
58+
->assertJsonMatches('results[0].text', 'foo')
5959
;
6060
}
6161

@@ -69,11 +69,11 @@ public function testItOnlySearchedOnSearchableFields(): void
6969
// search on name or description
7070
->get('/test/autocomplete/custom_product?query=foo')
7171
->assertSuccessful()
72-
->assertJsonMatches('length(results.options)', 2)
72+
->assertJsonMatches('length(results)', 2)
7373
->get('/test/autocomplete/custom_product?query=50')
7474
// price should not be searched
75-
->assertJsonMatches('length(results.options)', 1)
76-
->assertJsonMatches('results.options[0].text', 'bar')
75+
->assertJsonMatches('length(results)', 1)
76+
->assertJsonMatches('results[0].text', 'bar')
7777
;
7878
}
7979

@@ -93,7 +93,7 @@ public function testItEnforcesSecurity(): void
9393
],
9494
])
9595
->assertSuccessful()
96-
->assertJsonMatches('length(results.options)', 3)
96+
->assertJsonMatches('length(results)', 3)
9797
;
9898
}
9999

src/Autocomplete/tests/Functional/FieldAutocompleterTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ public function testItReturnsBasicResults(): void
3333
->throwExceptions()
3434
->get('/test/autocomplete/category_autocomplete_type')
3535
->assertSuccessful()
36-
->assertJsonMatches('length(results.options)', 2)
37-
->assertJsonMatches('results.options[0].value', (string) $category->getId())
38-
->assertJsonMatches('results.options[0].text', '<strong>foo and baz</strong>')
36+
->assertJsonMatches('length(results)', 2)
37+
->assertJsonMatches('results[0].value', (string) $category->getId())
38+
->assertJsonMatches('results[0].text', '<strong>foo and baz</strong>')
3939
->get('/test/autocomplete/category_autocomplete_type?query=bar')
40-
->assertJsonMatches('length(results.options)', 1)
40+
->assertJsonMatches('length(results)', 1)
4141
;
4242
}
4343

@@ -51,8 +51,8 @@ public function testItUsesTheCustomQuery(): void
5151
// query already ONLY returns items matching "foo"
5252
->get('/test/autocomplete/category_autocomplete_type?query=bar')
5353
->assertSuccessful()
54-
->assertJsonMatches('length(results.options)', 1)
55-
->assertJsonMatches('results.options[0].text', '<strong>foo and bar</strong>')
54+
->assertJsonMatches('length(results)', 1)
55+
->assertJsonMatches('results[0].text', '<strong>foo and bar</strong>')
5656
;
5757
}
5858

@@ -74,7 +74,7 @@ public function testItEnforcesSecurity(): void
7474
],
7575
])
7676
->assertSuccessful()
77-
->assertJsonMatches('length(results.options)', 3)
77+
->assertJsonMatches('length(results)', 3)
7878
;
7979
}
8080

@@ -98,7 +98,7 @@ public function testItWorksWithoutAChoiceLabel(): void
9898
->throwExceptions()
9999
->get('/test/autocomplete/category_no_choice_label_autocomplete_type?query=foo')
100100
->assertSuccessful()
101-
->assertJsonMatches('length(results.options)', 5)
101+
->assertJsonMatches('length(results)', 5)
102102
;
103103
}
104104
}

0 commit comments

Comments
 (0)