Skip to content

Commit

Permalink
Fix strict typing on abstract select
Browse files Browse the repository at this point in the history
  • Loading branch information
joegaudet committed Jul 27, 2020
1 parent 3cdd421 commit 0fb073b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
16 changes: 13 additions & 3 deletions addon/components/field-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export default class FieldForComponent extends Component {
return (
!this.inlineEditing ||
(this.inlineEditing && this.isEditing) ||
(this.hasControlCallout && this.hasErrors)
(this.inlineEditing && this.hasErrors)
);
}

Expand Down Expand Up @@ -472,7 +472,13 @@ export default class FieldForComponent extends Component {
*/
@arg(func)
formatValue = (value) => {
return this._hasCompositeValue ? JSON.stringify(value) : value;
if (this._hasCompositeValue) {
return JSON.stringify(value);
} else if (isArray(value)) {
return value.map((v) => JSON.stringify(v));
} else {
return value;
}
};

/**
Expand Down Expand Up @@ -575,7 +581,11 @@ export default class FieldForComponent extends Component {
this.isEditing = true;
later(() => {
if (this._showControl) {
document.querySelector(`#${this.controlId}`).focus();
const control = document.querySelector(`#${this.controlId}`);

if (control) {
control.focus();
}
}
}, 100);
}
Expand Down
4 changes: 2 additions & 2 deletions addon/components/form-controls/abstract-select.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Component from '@glimmer/component';
import { arg } from 'ember-arg-types';
import { string, array } from 'prop-types';
import { string } from 'prop-types';
import { typeOf } from '@ember/utils';
import { get } from '@ember/object';
import { action } from '@ember/object';
Expand All @@ -12,7 +12,7 @@ export default class FormControlsAbstractSelectComponent extends Component {
@arg(string)
idKey = 'id';

@arg(array)
@arg
values = [];

get isPrimitive() {
Expand Down
11 changes: 11 additions & 0 deletions addon/components/form-controls/ff-checkbox-select.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,15 @@
{{/let}}
</div>
{{/each}}

{{#if this.showClearAll}}
<button
type="button"
class="ff-control-checkbox-select-clear-all"
data-test-ff-control-checkbox-select-clear-all
{{on "click" this.clearAll}}
>
Clear All
</button>
{{/if}}
</div>
17 changes: 13 additions & 4 deletions addon/components/form-controls/ff-checkbox-select.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import FormControlsAbstractSelectComponent from './abstract-select';
import { action } from '@ember/object';
import { arg } from 'ember-arg-types';
import { string, array } from 'prop-types';
import { string, bool } from 'prop-types';
import { get } from '@ember/object';
import { A } from '@ember/array';

export default class FormControlsFfCheckboxSelectComponent extends FormControlsAbstractSelectComponent {
@arg(string)
for = 'id';

@arg(array)
@arg
value = [];

@arg(bool)
showClearAll = false;

@action
idFor(item) {
return `${this.for}-${this.isPrimitive ? item : get(item, this.idKey)}`;
Expand All @@ -26,13 +30,18 @@ export default class FormControlsFfCheckboxSelectComponent extends FormControlsA
if (this.isSelected(value)) {
this.args.onChange(this.value.filter((_) => !this._compare(_, value)));
} else {
this.args.onChange([value].concat(this.value));
this.args.onChange(A(this.value).toArray().concat(value));
}
}

@action
isSelected(value) {
return this.value.some((_) => this._compare(_, value));
return !!this.value.find((_) => this._compare(_, value));
}

@action
clearAll() {
return this.args.onChange([]);
}

_compare(a, b) {
Expand Down
5 changes: 2 additions & 3 deletions tests/integration/components/form-for-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ module('Integration | Component | form for', function (hooks) {
});

test('it does not hide the control when inline-editing when there are errors', async function (assert) {
this.model = { foo: 'bar', errors: { foo: ['bar error'] } };
this.model = { foo: 'bar', errors: { foo: [{ message: 'bar error' }] } };

await render(hbs`
<FormFor @model={{this.model}} as |f|>
Expand All @@ -235,8 +235,7 @@ module('Integration | Component | form for', function (hooks) {
assert.dom('input').hasValue('bar');
assert.dom('[data-test-form-error]').hasText('bar error');
await click('[data-test-commit-buttons-cancel]');
assert.dom('input').doesNotExist();
assert.dom('.field-for-value-container').hasText('bar');
assert.dom('[data-test-form-error]').hasText('bar error');
});

test('it shows the control and the value when inline-editing and has-control-callout and the value is clicked', async function (assert) {
Expand Down

0 comments on commit 0fb073b

Please sign in to comment.