Skip to content
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

fix: support numbers selection for PowerSelect component #1674

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ember-power-select/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@
"./components/power-select/search-message.js": "./dist/_app_/components/power-select/search-message.js",
"./components/power-select/trigger.js": "./dist/_app_/components/power-select/trigger.js",
"./helpers/ember-power-select-is-equal.js": "./dist/_app_/helpers/ember-power-select-is-equal.js",
"./helpers/ember-power-select-is-group.js": "./dist/_app_/helpers/ember-power-select-is-group.js"
"./helpers/ember-power-select-is-group.js": "./dist/_app_/helpers/ember-power-select-is-group.js",
"./helpers/ember-power-select-is-selected-present.js": "./dist/_app_/helpers/ember-power-select-is-selected-present.js"
}
}
}
10 changes: 5 additions & 5 deletions ember-power-select/src/components/power-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { tracked } from '@glimmer/tracking';
import { action, get } from '@ember/object';
import { addObserver, removeObserver } from '@ember/object/observers';
import { scheduleOnce } from '@ember/runloop';
import { isEqual } from '@ember/utils';
import { isEqual, isNone } from '@ember/utils';
import { assert } from '@ember/debug';
import {
indexOfOption,
Expand Down Expand Up @@ -299,7 +299,7 @@ export default class PowerSelectComponent extends Component<PowerSelectSignature
if (this._resolvedSelected) {
return toPlainArray(this._resolvedSelected);
} else if (
this.args.selected &&
!isNone(this.args.selected) &&
typeof this.args.selected.then !== 'function'
) {
return toPlainArray(this.args.selected);
Expand Down Expand Up @@ -450,7 +450,7 @@ export default class PowerSelectComponent extends Component<PowerSelectSignature

@action
_updateSelected(): void {
if (!this.args.selected) return;
if (isNone(this.args.selected)) return;
if (typeof this.args.selected.then === 'function') {
if (this._lastSelectedPromise === this.args.selected) return; // promise is still the same
if (
Expand Down Expand Up @@ -502,7 +502,7 @@ export default class PowerSelectComponent extends Component<PowerSelectSignature

@action
_highlight(opt: any): void {
if (opt && opt.disabled) {
if (!isNone(opt) && opt.disabled) {
return;
}
this.highlighted = opt;
Expand Down Expand Up @@ -765,7 +765,7 @@ export default class PowerSelectComponent extends Component<PowerSelectSignature
this.storedAPI.options,
this.storedAPI.highlighted,
);
} else if (!this.storedAPI.isOpen && this.selected) {
} else if (!this.storedAPI.isOpen && !isNone(this.selected)) {
searchStartOffset += indexOfOption(this.storedAPI.options, this.selected);
} else {
searchStartOffset = 0;
Expand Down
2 changes: 1 addition & 1 deletion ember-power-select/src/components/power-select/trigger.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{#if @select.selected}}
{{#if (ember-power-select-is-selected-present @select.selected)}}
{{#if @selectedItemComponent}}
{{#let (component (ensure-safe-component @selectedItemComponent)) as |SelectedItemComponent|}}
<SelectedItemComponent
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { helper } from '@ember/component/helper';
import { isNone } from '@ember/utils';

export function emberPowerSelectIsSelectedPresent([value]: [any]): boolean {
return !isNone(value);
}

export default helper(emberPowerSelectIsSelectedPresent);
2 changes: 2 additions & 0 deletions test-app/tests/integration/components/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export const numerals = [
'853',
];

export const digits = [0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 853];

export const names = [
'María',
'Søren Larsen',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import RSVP from 'rsvp';
import { tracked } from '@glimmer/tracking';
import { A } from '@ember/array';
import { run, later } from '@ember/runloop';
import { numbers, names, countries } from '../constants';
import { numbers, names, countries, digits } from '../constants';
import PromiseProxyMixin from '@ember/object/promise-proxy-mixin';
import ArrayProxy from '@ember/array/proxy';
import ObjectProxy from '@ember/object/proxy';
Expand Down Expand Up @@ -1621,5 +1621,36 @@ module(

//TODO: also try starting from non-null value and maybe also going back to null?
});

test('works with numbers', async function (assert) {
assert.expect(3);

this.selected = digits[0];
this.digits = digits;
await render(hbs`
<PowerSelect @options={{this.digits}} @selected={{this.selected}} @onChange={{fn (mut this.selected)}} as |option|>
{{option}}
</PowerSelect>
`);

assert
.dom('.ember-power-select-trigger')
.hasText(`${digits[0]}`, 'number "0" is shown in the trigger');

await clickTrigger();

assert
.dom('.ember-power-select-option[aria-current=true]')
.hasText(
`${digits[0]}`,
'The option containing "0" has been highlighted',
);

await click('.ember-power-select-option:nth-child(4)');

assert
.dom('.ember-power-select-trigger')
.hasText(`${digits[3]}`, 'number "5" is shown in the trigger');
});
},
);
Loading