Skip to content

Commit

Permalink
update test to account for different parents
Browse files Browse the repository at this point in the history
  • Loading branch information
amk221 committed Jan 23, 2025
1 parent 8a99c9d commit bca8d41
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 17.1.2

- Change internal option sort function

## 17.1.1

- Fix internal order of options when used with `#in-element`
Expand Down
36 changes: 23 additions & 13 deletions addon/components/select-box/index.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,6 @@ export default class SelectBox extends Component {
return this.options[this.nextOptionIndex];
}

get optionElements() {
return this.optionsElement
? [...this.optionsElement.querySelectorAll('.select-box__option')]
: [];
}

get interactiveElement() {
if (this.hasInput) {
return this.inputElement;
Expand All @@ -190,16 +184,32 @@ export default class SelectBox extends Component {
}

@cached
get options() {
if (!this.element) {
return [];
}

const els = this.optionElements;
get optionElements() {
return [
...new Set(this._options.map((option) => option.element.parentElement))
].reduce((optionElements, parentElement) => {
const els = [...parentElement.querySelectorAll('.select-box__option')];
optionElements.push(...els);
return optionElements;
}, []);
}

@cached
get options() {
return this._options
.filter((option) => !option.isDisabled)
.sort((a, b) => els.indexOf(a.element) - els.indexOf(b.element));
.sort((a, b) => {
// Preferred sort is too slow:
// return a.element.compareDocumentPosition(b.element) ===
// Node.DOCUMENT_POSITION_FOLLOWING
// ? -1
// : 1;

return (
this.optionElements.indexOf(a.element) -
this.optionElements.indexOf(b.element)
);
});
}

@action
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ module('select-box (in-element)', function (hooks) {
</sb.Option>
{{/each}}
</sb.Options>
<sb.Option @value={{null}} as |option|>
{{option.index}}
{{~" "~}}
qux
</sb.Option>
<sb.Option @value={{null}} as |option|>
{{option.index}}
{{~" "~}}
norf
</sb.Option>
</sb.Content>
</sb.Dropdown>
</SelectBox>
Expand Down Expand Up @@ -63,7 +73,7 @@ module('select-box (in-element)', function (hooks) {

assert.deepEqual(
findAll('.select-box__option').map((el) => el.textContent.trim()),
['0 bar', '1 baz'],
['0 bar', '1 baz', '2 qux', '3 norf'],
'computes correct index when options elements are not direct children'
);
});
Expand Down

0 comments on commit bca8d41

Please sign in to comment.