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

align all hooks with power-select #35

Merged
merged 2 commits into from
Dec 19, 2018
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ An example page can be found [here](https://weddingshoppe.github.io/ember-model-
modelName='user'
labelProperty='name'
selectedModel=selectedModel
onChange=(action (mut selectedModel))
onchange=(action (mut selectedModel))
}}
```

Expand All @@ -38,7 +38,7 @@ There is also a withCreate option which can be enabled by passing `withCreate=tr
onChange=(action (mut selectedModel))

withCreate=true
onCreate(action 'createModel')
oncreate(action 'createModel')
}}
```

Expand Down
24 changes: 17 additions & 7 deletions addon/components/model-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,21 @@ export default Component.extend({
/**
* Hook called when a model is selected.
*
* @argument onChange
* @argument onchange
* @type Function
*/
onChange: fallbackIfUndefined(function(){}),
onchange: fallbackIfUndefined(function(){}),

/**
* Hook called when a model is created.
*
* @argument onCreate
* @argument oncreate
* @type Function
*/
onCreate: fallbackIfUndefined(function(){}),
oncreate: fallbackIfUndefined(function(){}),

onopen: fallbackIfUndefined(function(){}),
onclose: fallbackIfUndefined(function(){}),

// NOTE: apart from the arguments above, ember-model-select supports the full
// ember-power-select API which can be found: https://ember-power-select.com/docs/api-reference
Expand Down Expand Up @@ -243,20 +246,27 @@ export default Component.extend({

actions: {
loadDefaultOptions(){
if(this.get('loadDefaultOptions')){
if(this.get('loadDefaultOptions')) {
this.get('searchModels').perform(null, null, true);
}
},
onOpen(){
this.send('loadDefaultOptions');

this.get('onopen')(...arguments);
},
onInput(term){
if(isEmpty(term)){
this.send('loadDefaultOptions');
}

this.get('oninput', ...arguments);
},
change(model){
if(model.__isSuggestion__){
this.get('onCreate')(model.__value__);
this.get('oncreate')(model.__value__);
} else {
this.get('onChange')(model);
this.get('onchange')(model);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion addon/templates/components/model-select.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
onfocus=onfocus
oninput=(action "onInput")
onkeydown=onkeydown
onopen=(action "loadDefaultOptions")
onopen=(action "onOpen")
options=_options
optionsComponent=(component optionsComponent infiniteScroll=infiniteScroll infiniteModel=model withCreate=withCreate)
placeholder=placeholder
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/components/model-select-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module('Integration | Component | model-select', function(hooks) {
let handleClick = this.spy();
this.actions = { handleClick };

await render(hbs`{{model-select modelName='user' labelProperty='name' onChange=(action 'handleClick')}}`);
await render(hbs`{{model-select modelName='user' labelProperty='name' onchange=(action 'handleClick')}}`);
await selectChoose('.ember-model-select', '.ember-power-select-option', 1);

assert.ok(handleClick.calledOnce, 'onChange hook has been called');
Expand Down Expand Up @@ -103,13 +103,13 @@ module('Integration | Component | model-select', function(hooks) {
assert.dom('.ember-power-select-option').hasText(`Add "test"...`);
});

test('it fires the onCreate hook when the create option is selected', async function(assert) {
test('it fires the oncreate hook when the create option is selected', async function(assert) {
assert.expect(2);

let handleCreate = this.spy();
this.actions = { handleCreate };

await render(hbs`{{model-select modelName='user' labelProperty='name' searchProperty="filter" withCreate=true onCreate=(action 'handleCreate')}}`);
await render(hbs`{{model-select modelName='user' labelProperty='name' searchProperty="filter" withCreate=true oncreate=(action 'handleCreate')}}`);
await selectSearch('.ember-model-select', 'test');
await selectChoose('.ember-model-select', '.ember-power-select-option', 1);

Expand Down