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

refactor(module:core): refactor input convert decorators #2535

Merged
merged 1 commit into from
Nov 26, 2018
Merged
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
61 changes: 35 additions & 26 deletions components/core/util/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,36 @@ export function valueFunctionProp<T>(prop: FunctionProp<T>, ...args: any[]): T {
return typeof prop === 'function' ? prop(...args) : prop;
}

// tslint:disable-next-line: no-any
function propDecoratorFactory<T, D>(name: string, fallback: (v: T) => D): (target: any, propName: string) => void {

// tslint:disable-next-line: no-any
function propDecorator(target: any, propName: string): void {
const privatePropName = `$$__${propName}`;

if (Object.prototype.hasOwnProperty.call(target, privatePropName)) {
console.warn(`The prop "${privatePropName}" is already exist, it will be overrided by ${name} decorator.`);
}

Object.defineProperty(target, privatePropName, {
configurable: true,
writable : true
});

Object.defineProperty(target, propName, {
get(): string {
return this[ privatePropName ]; // tslint:disable-line:no-invalid-this
},
set(value: T): void {
this[ privatePropName ] = fallback(value); // tslint:disable-line:no-invalid-this
}
});
}

return propDecorator;

}

/**
* Input decorator that handle a prop to do get/set automatically with toBoolean
*
Expand All @@ -34,31 +64,10 @@ export function valueFunctionProp<T>(prop: FunctionProp<T>, ...args: any[]): T {
* // __visible = false;
* ```
*/
export function InputBoolean(): any { // tslint:disable-line:no-any
return function InputBooleanPropDecorator (target: object, name: string): void {
// Add our own private prop
const privatePropName = `$$__${name}`;

if (Object.prototype.hasOwnProperty.call(target, privatePropName)) {
console.warn(`The prop "${privatePropName}" is already exist, it will be overrided by InputBoolean decorator.`);
}

Object.defineProperty(target, privatePropName, {
configurable: true,
writable: true
});

Object.defineProperty(target, name, {
get(): boolean {
return this[ privatePropName ]; // tslint:disable-line:no-invalid-this
},
set(value: boolean | string): void {
this[ privatePropName ] = toBoolean(value); // tslint:disable-line:no-invalid-this
}
});
export function InputBoolean(): any { // tslint:disable-line: no-any
return propDecoratorFactory('InputBoolean', toBoolean);
}

// // Do rest things for input decorator
// const inputDecorator = Input();
// inputDecorator(target, name);
};
export function InputCssPixel(): any { // tslint:disable-line: no-any
return propDecoratorFactory('InputCssPixel', toCssPixel);
}