Skip to content

Commit

Permalink
Removed ember-arg-types again, in favor of helpers and custom simple …
Browse files Browse the repository at this point in the history
…decorator
  • Loading branch information
simonihmig committed Mar 28, 2020
1 parent 27a4c6a commit 25286cc
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 165 deletions.
2 changes: 1 addition & 1 deletion addon/components/bs-button.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<button
disabled={{this.__disabled}}
type={{this.buttonType}}
class="btn {{if this.active "active"}} {{if this.block "btn-block"}} {{this.sizeClass}} {{this.typeClass}}"
class="btn {{if @active "active"}} {{if this.block "btn-block"}} {{bs-size-class "btn" @size}} {{bs-type-class "btn" @type default=(if (has-bootstrap-version 3) "default" "secondary") outline=@outline}}"
...attributes
{{on "click" this.handleClick}}
{{did-update this.resetState @reset}}
Expand Down
44 changes: 10 additions & 34 deletions addon/components/bs-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { equal, or } from '@ember/object/computed';
import Component from '@glimmer/component';
import sizeClass from 'ember-bootstrap/utils/cp/size-class';
import typeClass from 'ember-bootstrap/utils/cp/type-class';
import { hasBootstrapVersion } from 'ember-bootstrap/compatibility-helpers';
import { arg } from 'ember-arg-types';
import arg from 'ember-bootstrap/utils/decorators/arg';

/**
Implements a HTML button element, with support for all [Bootstrap button CSS styles](http://getbootstrap.com/css/#buttons)
Expand Down Expand Up @@ -142,14 +139,13 @@ export default class Button extends Component {
* @default null
* @private
*/
@arg _disabled = null;

get __disabled() {
if (this._disabled !== null) {
return this._disabled;
if (this.args._disabled !== undefined) {
return this.args._disabled;
}

return this.isPending && this.preventConcurrency;
return this.isPending && this.args.preventConcurrency !== false;
}

/**
Expand All @@ -171,7 +167,6 @@ export default class Button extends Component {
* @default false
* @public
*/
@arg active = false;

/**
* Property for block level buttons
Expand All @@ -193,7 +188,6 @@ export default class Button extends Component {
* @default false
* @public
*/
@arg bubble = false;

/**
* If button is active and this is set, the icon property will match this property
Expand All @@ -202,7 +196,6 @@ export default class Button extends Component {
* @type String
* @public
*/
@arg iconActive = null;

/**
* If button is inactive and this is set, the icon property will match this property
Expand All @@ -211,7 +204,6 @@ export default class Button extends Component {
* @type String
* @public
*/
@arg iconInactive = null;

/**
* Class(es) (e.g. glyphicons or font awesome) to use as a button icon
Expand All @@ -222,7 +214,7 @@ export default class Button extends Component {
* @public
*/
get icon() {
return this.args.icon || (this.active ? this.iconActive : this.iconInactive);
return this.args.icon || (this.args.active ? this.args.iconActive : this.args.iconInactive);
}

/**
Expand All @@ -233,7 +225,6 @@ export default class Button extends Component {
* @type any
* @public
*/
@arg value = null;

/**
* Controls if `onClick` action is fired concurrently. If `true` clicking button multiple times will not trigger
Expand All @@ -246,7 +237,6 @@ export default class Button extends Component {
* @default true
* @public
*/
@arg preventConcurrency = true;

/**
* State of the button. The button's label (if not used as a block component) will be set to the
Expand Down Expand Up @@ -323,11 +313,6 @@ export default class Button extends Component {
* @type String
* @public
*/
@arg
size = null;

@sizeClass('btn', 'size')
sizeClass;

/**
* Property for type styling
Expand All @@ -339,8 +324,6 @@ export default class Button extends Component {
* @default 'secondary'
* @public
*/
@arg
type = hasBootstrapVersion(4) ? 'secondary' : 'default';

/**
* Property to create outline buttons (BS4+ only)
Expand All @@ -350,12 +333,6 @@ export default class Button extends Component {
* @default false
* @public
*/
@arg
outline = false;


@typeClass('btn', 'type')
typeClass;

/**
* When clicking the button this action is called with the value of the button (that is the value of the "value" property).
Expand All @@ -371,7 +348,6 @@ export default class Button extends Component {
* @param {*} value
* @public
*/
@arg onClick = null;

/**
* This will reset the state property to 'default', and with that the button's label to defaultText
Expand All @@ -394,15 +370,15 @@ export default class Button extends Component {
*/
@action
handleClick(e) {
let onClick = this.onClick;
let preventConcurrency = this.preventConcurrency;
let onClick = this.args.onClick;
let preventConcurrency = this.args.preventConcurrency;

if (onClick === null || onClick === undefined) {
if (!onClick) {
return;
}

if (!preventConcurrency || !this.isPending) {
let promise = (onClick)(this.value);
let promise = (onClick)(this.args.value);
if (promise && typeof promise.then === 'function' && !this.isDestroyed) {
this.state = 'pending';
promise.then(() => {
Expand All @@ -418,7 +394,7 @@ export default class Button extends Component {
}
}

if (!this.bubble) {
if (!this.args.bubble) {
e.stopPropagation();
}
}
Expand Down
9 changes: 9 additions & 0 deletions addon/helpers/bs-size-class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { helper } from '@ember/component/helper';
import { isBlank } from '@ember/utils';

export function sizeClassHelper([prefix, size], { default: defaultValue }) {
size = size ?? defaultValue;
return isBlank(size) ? null : `${prefix}-${size}`;
}

export default helper(sizeClassHelper);
11 changes: 11 additions & 0 deletions addon/helpers/bs-type-class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { helper } from '@ember/component/helper';

export function typeClassHelper([prefix, type], { default: defaultValue, outline = false }) {
type = type ?? defaultValue;
if (outline) {
return `${prefix}-outline-${type}`;
}
return `${prefix}-${type}`;
}

export default helper(typeClassHelper);
10 changes: 10 additions & 0 deletions addon/utils/decorators/arg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default function arg(target, key, descriptor) {
let defaultValue = descriptor.initializer ? descriptor.initializer() : undefined;

return {
get() {
const argValue = this.args[key];
return argValue !== undefined ? argValue : defaultValue;
}
};
}
15 changes: 0 additions & 15 deletions addon/utils/overrideable-decorator.js

This file was deleted.

1 change: 1 addition & 0 deletions app/helpers/bs-size-class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default, eq } from 'ember-bootstrap/helpers/bs-size-class';
1 change: 1 addition & 0 deletions app/helpers/bs-type-class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default, eq } from 'ember-bootstrap/helpers/bs-type-class';
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"chai": "^4.0.0",
"chai-things": "^0.2.0",
"ember-a11y-testing": "^1.1.1",
"ember-arg-types": "^0.2.0",
"ember-cli": "~3.15.1",
"ember-cli-app-version": "^3.0.0",
"ember-cli-blueprint-test-helpers": "^0.19.1",
Expand Down
Loading

0 comments on commit 25286cc

Please sign in to comment.