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

feat: new power select component for parameters #1065

Merged
merged 2 commits into from
Jun 17, 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
41 changes: 41 additions & 0 deletions app/components/pipeline/parameters/selectable/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';

export default class PipelineParametersSelectableComponent extends Component {
@tracked value;

inputValue;

constructor() {
super(...arguments);

this.value = this.args.parameter.value;
}

@action
selectOption(value) {
this.value = value;
this.args.onSelectValue(this.args.parameter, value);
}

@action
handleInput(value) {
this.inputValue = value;
}

@action
handleKeydown(_dropdown, e) {
switch (e.keyCode) {
case 13:
this.selectOption(this.inputValue);
break;
case 27:
// Escape key pressed - prevent further propagation of event
e.stopImmediatePropagation();
break;
default:
break;
}
}
}
16 changes: 16 additions & 0 deletions app/components/pipeline/parameters/selectable/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<PowerSelect
class="parameter-selector"
@options={{@parameter.defaultValues}}
@renderInPlace={{true}}
@searchEnabled={{true}}
@selected={{this.value}}
@searchPlaceholder="Type to filter"
@noMatchesMessage="Press enter to override"
@onOpen={{fn @onOpen}}
@onChange={{this.selectOption}}
@onInput={{this.handleInput}}
@onKeydown={{this.handleKeydown}}
as |selectedValue|
>
{{selectedValue}}
</PowerSelect>
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'screwdriver-ui/tests/helpers';
import { click, render, triggerKeyEvent } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import { selectChoose, selectSearch } from 'ember-power-select/test-support';
import sinon from 'sinon';

module(
'Integration | Component | pipeline/parameters/selectable',
function (hooks) {
setupRenderingTest(hooks);

test('it renders', async function (assert) {
this.setProperties({
parameter: {
defaultValues: ['foo', 'bar'],
value: 'foo'
},
onOpen: () => {},
onSelectValue: () => {}
});

await render(
hbs`<Pipeline::Parameters::Selectable
@parameter={{this.parameter}}
@onOpen={{this.onOpen}}
@onSelectValue={{this.onSelectValue}}
/>`
);

assert.dom(this.element).hasText('foo');
});

test('it calls onOpen callback', async function (assert) {
const onOpen = sinon.spy();

this.setProperties({
parameter: {
defaultValues: ['foo', 'bar'],
value: 'foo'
},
onOpen,
onSelectValue: () => {}
});

await render(
hbs`<Pipeline::Parameters::Selectable
@parameter={{this.parameter}}
@onOpen={{this.onOpen}}
@onSelectValue={{this.onSelectValue}}
/>`
);
await click('.ember-power-select-trigger');

assert.equal(onOpen.callCount, 1);
});

test('it calls onSelectValue callback when selection is made', async function (assert) {
const parameter = {
defaultValues: ['foo', 'bar'],
value: 'foo'
};
const onSelectValue = sinon.spy();

this.setProperties({
parameter,
onOpen: () => {},
onSelectValue
});

await render(
hbs`<Pipeline::Parameters::Selectable
@parameter={{this.parameter}}
@onOpen={{this.onOpen}}
@onSelectValue={{this.onSelectValue}}
/>`
);

await selectChoose('.parameter-selector', parameter.defaultValues[1]);

assert.equal(onSelectValue.callCount, 1);
assert.equal(
onSelectValue.calledWith(parameter, parameter.defaultValues[1]),
true
);
});

test('it overrides default values when enter key is pressed', async function (assert) {
const parameter = { defaultValues: ['foo', 'bar'], value: 'foo' };
const newValue = 'abc123';
const onSelectValue = sinon.spy();

this.setProperties({
parameter,
onOpen: () => {},
onSelectValue
});

await render(
hbs`<Pipeline::Parameters::Selectable
@parameter={{this.parameter}}
@onOpen={{this.onOpen}}
@onSelectValue={{this.onSelectValue}}
/>`
);

await selectSearch('.parameter-selector', newValue);
await triggerKeyEvent('input', 'keydown', 13);

await assert.equal(onSelectValue.callCount, 1);
assert.equal(onSelectValue.calledWith(parameter, newValue), true);
});
}
);