diff --git a/components/core/util/convert.ts b/components/core/util/convert.ts index 612768571a9..dfd1603e6b8 100644 --- a/components/core/util/convert.ts +++ b/components/core/util/convert.ts @@ -18,6 +18,36 @@ export function valueFunctionProp(prop: FunctionProp, ...args: any[]): T { return typeof prop === 'function' ? prop(...args) : prop; } +// tslint:disable-next-line: no-any +function propDecoratorFactory(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 * @@ -34,31 +64,10 @@ export function valueFunctionProp(prop: FunctionProp, ...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); }