Skip to content

Commit

Permalink
ui: Adds a sort-control component for asc/desc sorting of columns e…
Browse files Browse the repository at this point in the history
…tc (#6034)

This adds the component but doesn't yet use it anywhere. No tests
are added here as there isn't an awful lot to test.
  • Loading branch information
johncowen authored and John Cowen committed Aug 29, 2019
1 parent 413ec9d commit 489cb36
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ui-v2/app/components/sort-control.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Component from '@ember/component';
import { get, set } from '@ember/object';
export default Component.extend({
classNames: ['sort-control'],
direction: 'asc',
onchange: function() {},
actions: {
change: function(e) {
if (e.target.type === 'checkbox') {
set(this, 'direction', e.target.checked ? 'desc' : 'asc');
}
this.onchange({ target: { value: `${get(this, 'value')}:${get(this, 'direction')}` } });
},
},
});
7 changes: 7 additions & 0 deletions ui-v2/app/helpers/starts-with.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { helper } from '@ember/component/helper';

export function startsWith([needle, haystack = ''] /*, hash*/) {
return haystack.startsWith(needle);
}

export default helper(startsWith);
1 change: 1 addition & 0 deletions ui-v2/app/styles/base/components/sort-control/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import './skin';
17 changes: 17 additions & 0 deletions ui-v2/app/styles/base/components/sort-control/skin.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
%sort-control input {
display: none;
}
%sort-control label {
@extend %user-select-none;
cursor: pointer;
}
%sort-control input[type='checkbox'] + label::after {
@extend %as-pseudo;
opacity: 0.7;
}
%sort-control input[type='checkbox'] + label::after {
@extend %with-arrow-down-icon;
}
%sort-control input[type='checkbox']:checked + label::after {
@extend %with-arrow-up-icon;
}
1 change: 1 addition & 0 deletions ui-v2/app/styles/components/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@
@import './modal-dialog';
@import './notice';
@import './tooltip';
@import './sort-control';
4 changes: 4 additions & 0 deletions ui-v2/app/styles/components/sort-control.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@import '../base/components/sort-control/index';
.sort-control {
@extend %sort-control;
}
7 changes: 7 additions & 0 deletions ui-v2/app/templates/components/sort-control.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<input id={{concat 'sort-control-value-' elementId}} type="radio" value="Name" name={{concat 'sort-control-' name}} onchange={{action 'change'}} />
{{#if checked}}
<input id={{concat 'sort-control-direction-' elementId}} type="checkbox" onchange={{action 'change'}} />
{{/if}}
<label for={{if checked (concat 'sort-control-direction-' elementId) (concat 'sort-control-value-' elementId)}}>
{{yield}}
</label>
50 changes: 50 additions & 0 deletions ui-v2/tests/integration/components/sort-control-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';

moduleForComponent('sort-control', 'Integration | Component | sort control', {
integration: true,
});

test('it renders', function(assert) {
// Set any properties with this.set('myProperty', 'value');
// Handle any actions with this.on('myAction', function(val) { ... });

this.render(hbs`{{sort-control}}`);

assert.equal(
this.$()
.text()
.trim(),
''
);

// Template block usage:
this.render(hbs`
{{#sort-control}}
template block text
{{/sort-control}}
`);

assert.equal(
this.$()
.text()
.trim(),
'template block text'
);
});
test('it changes direction and calls onchange when clicked/activated', function(assert) {
assert.expect(2);
let count = 0;
this.on('change', e => {
if (count === 0) {
assert.equal(e.target.value, 'sort:desc');
} else {
assert.equal(e.target.value, 'sort:asc');
}
count++;
});
this.render(hbs`{{sort-control checked=true value='sort' onchange=(action 'change')}}`);
const $label = this.$('label');
$label.trigger('click');
$label.trigger('click');
});
20 changes: 20 additions & 0 deletions ui-v2/tests/integration/helpers/starts-with-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';

moduleForComponent('starts-with', 'helper:starts-with', {
integration: true,
});

// Replace this with your real tests.
test('it renders', function(assert) {
this.set('inputValue', '1234');

this.render(hbs`{{starts-with inputValue}}`);

assert.equal(
this.$()
.text()
.trim(),
'false'
);
});

0 comments on commit 489cb36

Please sign in to comment.