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

[FIX] Custom decorator #80

Merged
merged 2 commits into from
Dec 12, 2019
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
43 changes: 29 additions & 14 deletions packages/button/addon/components/nucleus-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import {
tagName,
layout as templateLayout,
} from '@ember-decorators/component';
import defaultProp from '@freshworks/core/utils/default-decorator';

import { or, equal } from '@ember/object/computed';
import { run } from '@ember/runloop';
import Component from '@ember/component';
import { set, getWithDefault, computed } from '@ember/object';
import { set, computed } from '@ember/object';
import layout from "../templates/components/nucleus-button";
import { BUTTON_STATE } from "../constants/nucleus-button";

Expand Down Expand Up @@ -45,6 +46,7 @@ class NucleusButton extends Component {
* @default null
* @public
*/
@defaultProp
label = null;

/**
Expand All @@ -55,6 +57,7 @@ class NucleusButton extends Component {
* @default null
* @public
*/
@defaultProp
size = null;

/**
Expand All @@ -65,6 +68,7 @@ class NucleusButton extends Component {
* @default 'primary'
* @public
*/
@defaultProp
type = 'primary';

/**
Expand All @@ -84,6 +88,7 @@ class NucleusButton extends Component {
* @default false
* @public
*/
@defaultProp
active = false;

/**
Expand All @@ -94,6 +99,7 @@ class NucleusButton extends Component {
* @default false
* @public
*/
@defaultProp
autofocus = false;

/**
Expand All @@ -104,6 +110,7 @@ class NucleusButton extends Component {
* @default false
* @public
*/
@defaultProp
block = false;

/**
Expand All @@ -113,6 +120,7 @@ class NucleusButton extends Component {
* @type string|null
* @public
*/
@defaultProp
icon = null;

/**
Expand All @@ -122,6 +130,7 @@ class NucleusButton extends Component {
* @type string
* @public
*/
@defaultProp
customClass = null;

/**
Expand All @@ -132,6 +141,7 @@ class NucleusButton extends Component {
* @default false
* @public
*/
@defaultProp
disabled = false;

/**
Expand All @@ -142,8 +152,8 @@ class NucleusButton extends Component {
*/
@computed('disabled', '_isPending')
get _disabled() {
let isDisabled = this.disabled;
return isDisabled ? isDisabled : this._isPending;
let isDisabled = this.get('disabled');
return isDisabled ? isDisabled : this.get('_isPending');
}

/**
Expand All @@ -153,6 +163,7 @@ class NucleusButton extends Component {
* @type string|number|object
* @public
*/
@defaultProp
value = null;

/**
Expand Down Expand Up @@ -233,7 +244,7 @@ class NucleusButton extends Component {
*/
@computed('_isLoading', 'pendingLabel', 'fulfilledLabel', 'rejectedLabel')
get _isShowLoading() {
return !(this.pendingLabel || this.fulfilledLabel || this.rejectedLabel);
return !(this.get('pendingLabel') || this.get('fulfilledLabel') || this.get('rejectedLabel'));
}

/**
Expand All @@ -244,7 +255,8 @@ class NucleusButton extends Component {
* @default undefined
* @public
*/
pendingLabel = undefined;
@defaultProp
pendingLabel = '';

/**
* Label to be displayed during Promise fulfilled state, a.k.a success label
Expand All @@ -254,7 +266,8 @@ class NucleusButton extends Component {
* @default undefined
* @public
*/
fulfilledLabel = undefined;
@defaultProp
fulfilledLabel = '';

/**
* Label to be displayed during Promise rejected state, a.k.a failure label
Expand All @@ -264,7 +277,8 @@ class NucleusButton extends Component {
* @default undefined
* @public
*/
rejectedLabel = undefined;
@defaultProp
rejectedLabel = '';

/**
* Optional aria-label attribute
Expand All @@ -274,6 +288,7 @@ class NucleusButton extends Component {
* @default null
* @public
*/
@defaultProp
ariaLabel = null;

/**
Expand All @@ -284,7 +299,7 @@ class NucleusButton extends Component {
*/
@computed('size')
get _sizeClass() {
let size = this.size;
let size = this.get('size');
return size ? `nucleus-button--${size}` : null;
}

Expand All @@ -296,8 +311,8 @@ class NucleusButton extends Component {
*/
@computed('type')
get _typeClass() {
let type = this.type;
return type ? `nucleus-button--${this.type}` : 'nucleus-button--primary';
let type = this.get('type');
return type ? `nucleus-button--${this.get('type')}` : 'nucleus-button--primary';
}

/**
Expand All @@ -319,8 +334,8 @@ class NucleusButton extends Component {
get _buttonText() {
let state = this._buttonState;
return state === 'default'
? this.label
: getWithDefault(this, `${state}Label`, this.label);
? this.get('label')
: this.get(`${state}Label`);
}

/**
Expand All @@ -332,7 +347,7 @@ class NucleusButton extends Component {
*/
@computed('_buttonText', 'ariaLabel', 'icon')
get _label() {
return this.ariaLabel || this._buttonText || this.icon;
return this.get('ariaLabel') || this.get('_buttonText') || this.get('icon');
}

/**
Expand All @@ -350,7 +365,7 @@ class NucleusButton extends Component {
}

if (!this._isPending) {
let promise = action(this.value);
let promise = action(this.get('value'));

if (promise && typeof promise.then === 'function' && !this.isDestroyed) {
set(this, '_buttonState', BUTTON_STATE.PENDING);
Expand Down
2 changes: 1 addition & 1 deletion packages/button/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"ember-cli-sass": "^10.0.0",
"ember-css-transitions": "^0.1.16",
"ember-decorators": "^6.1.1",
"ember-decorators-polyfill": "^1.1.1",
"ember-decorators-polyfill": "shibulijack-fd/ember-decorators-polyfill#master",
"ember-svg-jar": "^2.1.0",
"ember-template-lint": "^1.6.0",
"ember-truth-helpers": "^2.1.0",
Expand Down
14 changes: 14 additions & 0 deletions packages/core/addon/utils/default-decorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { computed } from '@ember/object';

export default function defaultProp(target, key, descriptor) {
let { initializer, value } = descriptor;

return computed({
get() {
return initializer ? initializer.call(this) : value;
},
set(_, v) {
return v;
}
})(target, key, { ...descriptor, value: undefined, initializer: undefined });
}
1 change: 1 addition & 0 deletions packages/core/app/utils/default-decorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@freshworks/core/utils/default-decorator';
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6337,6 +6337,14 @@ ember-decorators-polyfill@^1.1.1:
ember-cli-version-checker "^3.1.3"
ember-compatibility-helpers "^1.2.0"

ember-decorators-polyfill@shibulijack-fd/ember-decorators-polyfill#master:
version "1.1.1"
resolved "https://codeload.github.com/shibulijack-fd/ember-decorators-polyfill/tar.gz/de8ce9c8070d63c316ac9b28c444d71e6fc5cf6d"
dependencies:
ember-cli-babel "^7.1.2"
ember-cli-version-checker "^3.1.3"
ember-compatibility-helpers "^1.2.0"

ember-decorators@^6.1.1:
version "6.1.1"
resolved "https://registry.yarnpkg.com/ember-decorators/-/ember-decorators-6.1.1.tgz#6d770f8999cf5a413a1ee459afd520838c0fc470"
Expand Down