-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(relationship-select): add component to select a model for a rela…
…tionship via power-select
- Loading branch information
Showing
7 changed files
with
422 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<input | ||
class="hidden-input" | ||
value={{or (or @selected.id @selected.length) ""}} | ||
...attributes | ||
{{did-insert (perform this.fetchModels)}} | ||
/> | ||
{{#with | ||
(component | ||
(if @multiple "power-select-multiple" "power-select") | ||
) as |RelationPowerSelect| | ||
}} | ||
<RelationPowerSelect | ||
@searchEnabled={{true}} | ||
@search={{perform this.fetchModels}} | ||
@options={{this.models}} | ||
@selected={{@selected}} | ||
@placeholder={{ | ||
if this.fetchModels.isRunning (t "emeis.loading") @placeholder | ||
}} | ||
@noMatchesMessage={{t "emeis.empty"}} | ||
@onChange={{@onChange}} as |model| | ||
> | ||
{{yield model}} | ||
</RelationPowerSelect> | ||
{{/with}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { inject as service } from "@ember/service"; | ||
import Component from "@glimmer/component"; | ||
import { timeout } from "ember-concurrency"; | ||
import { restartableTask, lastValue } from "ember-concurrency-decorators"; | ||
|
||
import handleModelErrors from "ember-emeis/decorators/handle-model-errors"; | ||
|
||
export default class RelationshipSelectComponent extends Component { | ||
@service notification; | ||
@service intl; | ||
@service store; | ||
|
||
@lastValue("fetchModels") models; | ||
|
||
@restartableTask | ||
@handleModelErrors | ||
*fetchModels(search) { | ||
yield timeout(200); | ||
return yield typeof search === "string" | ||
? this.store.query(this.args.modelName, { filter: { search } }) | ||
: this.store.findAll(this.args.modelName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "ember-emeis/components/relationship-select"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { render, waitUntil } from "@ember/test-helpers"; | ||
import { hbs } from "ember-cli-htmlbars"; | ||
import { setupMirage } from "ember-cli-mirage/test-support"; | ||
import { setupIntl } from "ember-intl/test-support"; | ||
import { | ||
typeInSearch, | ||
clickTrigger, | ||
selectChoose, | ||
} from "ember-power-select/test-support/helpers"; | ||
import { setupRenderingTest } from "ember-qunit"; | ||
import { module, test } from "qunit"; | ||
|
||
import setupRequestAssertions from "./../../helpers/assert-request"; | ||
|
||
module("Integration | Component | relationship-select", function (hooks) { | ||
setupRenderingTest(hooks); | ||
setupMirage(hooks); | ||
setupRequestAssertions(hooks); | ||
setupIntl(hooks, ["en"]); | ||
|
||
test("single select", async function (assert) { | ||
this.set("modelName", "role"); | ||
const role = this.server.create("role"); | ||
|
||
await render(hbs` | ||
<RelationshipSelect | ||
@modelName={{this.modelName}} | ||
@onChange={{set this.selected}} | ||
@selected={{this.selected}} | ||
@placeholder="test" | ||
as |model| | ||
> | ||
<span data-test-name>{{model.name}}</span> | ||
</RelationshipSelect> | ||
`); | ||
|
||
this.assertRequest("GET", "/api/v1/roles", (request) => { | ||
assert.ok(request); | ||
}); | ||
await clickTrigger(); | ||
|
||
// For some reason the await click is not actually waiting for the fetch task to finish. | ||
// Probably some runloop issue. | ||
await waitUntil(() => this.element.querySelector("[data-test-name]")); | ||
assert.dom("[data-test-name]").hasText(role.name.en); | ||
|
||
this.assertRequest("GET", "/api/v1/roles", (request) => { | ||
assert.equal(request.queryParams.search, "test"); | ||
}); | ||
await typeInSearch("test"); | ||
|
||
await selectChoose("", role.name.en); | ||
assert.equal(this.selected.name, role.name.en); | ||
}); | ||
|
||
test("multiple select", async function (assert) { | ||
this.set("modelName", "role"); | ||
const role = this.server.create("role"); | ||
|
||
await render(hbs` | ||
<RelationshipSelect | ||
@modelName={{this.modelName}} | ||
@onChange={{set this.selected}} | ||
@selected={{this.selected}} | ||
@placeholder="test" | ||
@multiple="true" | ||
as |model| | ||
> | ||
<span data-test-name>{{model.name}}</span> | ||
</RelationshipSelect> | ||
`); | ||
|
||
this.assertRequest("GET", "/api/v1/roles", (request) => { | ||
assert.ok(request); | ||
}); | ||
await clickTrigger(); | ||
|
||
// For some reason the await click is not actually waiting for the fetch task to finish. | ||
// Probably some runloop issue. | ||
await waitUntil(() => this.element.querySelector("[data-test-name]")); | ||
assert.dom("[data-test-name]").hasText(role.name.en); | ||
|
||
this.assertRequest("GET", "/api/v1/roles", (request) => { | ||
assert.equal(request.queryParams.search, "test"); | ||
}); | ||
await typeInSearch("test"); | ||
|
||
await selectChoose("", role.name.en); | ||
assert.equal(this.selected[0].name, role.name.en); | ||
}); | ||
}); |
Oops, something went wrong.