Skip to content

Commit

Permalink
Merge pull request #13 from mohit23x/type-enhancemets
Browse files Browse the repository at this point in the history
fix issue #10
  • Loading branch information
mohit23x authored Feb 19, 2021
2 parents f55b129 + a64940b commit 62b0884
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 60 deletions.
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
example/
lib/
assets/
.github/
17 changes: 7 additions & 10 deletions build/Sheet.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import type { ConstantsType, Fn, NamedStyles } from './type';
export default class Sheet<T, S extends NamedStyles<S> | NamedStyles<any>, O = S> {
result: O;
source: Fn<T, S>;
nativeSheet: O;
constructor(sourceFn: Fn<T, S>);
calc(globalVars: T, constants: ConstantsType, activeIndex: number): O;
getResult(): O;
import type { ConstantsType, Fn, NamedStyles, StyleSheetType } from './type';
export default class Sheet<T, P extends NamedStyles<P> | NamedStyles<any>> {
result: StyleSheetType<P>;
source: Fn<T, P>;
constructor(sourceFn: Fn<T, P>);
calc(globalVars: T, constants: ConstantsType, activeIndex: number): StyleSheetType<P>;
getResult(): StyleSheetType<P>;
clearResult(): void;
calcStyles(globalVars: T, constants: ConstantsType, activeIndex: number): void;
calcStyle(key: string, styleProps: any): void;
calcNative(): void;
}
13 changes: 0 additions & 13 deletions build/Sheet.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const react_native_1 = require("react-native");
class Sheet {
constructor(sourceFn) {
this.nativeSheet = {};
this.source = sourceFn;
this.result = {};
}
calc(globalVars, constants, activeIndex) {
this.clearResult();
this.calcStyles(globalVars, constants, activeIndex);
this.calcNative();
return this.getResult();
}
getResult() {
Expand Down Expand Up @@ -45,15 +42,5 @@ class Sheet {
});
}
}
calcStyle(key, styleProps) {
// @ts-ignore
this.nativeSheet[key] = styleProps;
}
calcNative() {
if (Object.keys(this.result).length) {
const rnStyleSheet = react_native_1.StyleSheet.create(this.nativeSheet);
Object.assign(this.result, rnStyleSheet);
}
}
}
exports.default = Sheet;
4 changes: 2 additions & 2 deletions build/Sugar.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StyleSheet } from 'react-native';
import { Fn, buildEventType, NamedStyles, ConstantsType } from './type';
import { Fn, buildEventType, NamedStyles, ConstantsType, StyleSheetType } from './type';
import Sheet from './Sheet';
export default class Sugar<T> {
builded: boolean;
Expand Down Expand Up @@ -99,7 +99,7 @@ export default class Sugar<T> {
_refresh(): void;
build(themeObj: T): void;
configure(newConstants: Partial<ConstantsType>): void;
create<P extends NamedStyles<P> | NamedStyles<any>>(objFn: Fn<T, P>): P;
create<P extends NamedStyles<P> | NamedStyles<any>>(objFn: Fn<T, P>): StyleSheetType<P>;
_calculateActiveIndex(): void;
_calcSheets(): void;
_callListeners(event: buildEventType): void;
Expand Down
6 changes: 4 additions & 2 deletions build/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ export declare type NamedStyles<T> = {
};
export declare type S = NamedStyles<any>;
export declare type Fn<T, P> = (theme: T, constants: ConstantsType) => P extends NamedStyles<P> ? NamedStyles<P> : P;
export declare type StyleSheetType<T> = {
[P in keyof T]: ViewStyle | TextStyle | ImageStyle;
export declare type StyleSheetType<P> = {
[K in keyof P]: {
[J in keyof P[K]]: P[K][J] extends Array<any> ? J extends 'transform' | 'transformMatrix' ? P[K][J] : P[K][J][number] : P[K][J];
};
};
export declare type buildEventType = 'build';
export declare type ThemeProp<T> = {
Expand Down
31 changes: 7 additions & 24 deletions lib/Sheet.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
import { StyleSheet } from 'react-native';
import type { ConstantsType, Fn, NamedStyles, StyleSheetType } from './type';

export default class Sheet<
T,
S extends NamedStyles<S> | NamedStyles<any>,
O = S
> {
public result: O;
public source: Fn<T, S>;
public nativeSheet: O = {} as O;
P extends NamedStyles<P> | NamedStyles<any>> {
public result: StyleSheetType<P>;
public source: Fn<T, P>;

constructor(sourceFn: Fn<T, S>) {
constructor(sourceFn: Fn<T, P>) {
this.source = sourceFn;
this.result = {} as O;
this.result = {} as StyleSheetType<P>;
}

calc(globalVars: T, constants: ConstantsType, activeIndex: number): O {
calc(globalVars: T, constants: ConstantsType, activeIndex: number): StyleSheetType<P> {
this.clearResult();
this.calcStyles(globalVars, constants, activeIndex);
this.calcNative();
return this.getResult();
}

getResult(): O {
getResult(): StyleSheetType<P> {
return this.result;
}

Expand Down Expand Up @@ -59,16 +54,4 @@ export default class Sheet<
});
}
}

calcStyle(key: string, styleProps: any): void {
// @ts-ignore
this.nativeSheet[key] = styleProps;
}

calcNative(): void {
if (Object.keys(this.result).length) {
const rnStyleSheet = StyleSheet.create(this.nativeSheet);
Object.assign(this.result, rnStyleSheet);
}
}
}
8 changes: 5 additions & 3 deletions lib/Sugar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,18 @@ export default class Sugar<T> {
this._refresh();
}

create<P extends NamedStyles<P> | NamedStyles<any>>(objFn: Fn<T, P>): P {
create<P extends NamedStyles<P> | NamedStyles<any>>(
objFn: Fn<T, P>
): StyleSheetType<P> {
if (typeof objFn === 'function') {
const sheet = new Sheet(objFn);
this.sheets.push(sheet);
if (this.builded) {
sheet.calc(this.theme, this.constants, this.activeIndex);
}
return sheet.getResult() as P;
return sheet.getResult() as StyleSheetType<P>;
}
return objFn as P;
return objFn as StyleSheetType<P>;
}

_calculateActiveIndex(): void {
Expand Down
19 changes: 14 additions & 5 deletions lib/type.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Sugar from './Sugar';

export type ConstantsType = typeof constants;


export type SugarViewStyle = {
[P in keyof ViewStyle]: ViewStyle[P] | Array<ViewStyle[P]>;
};
Expand All @@ -22,11 +21,21 @@ export type NamedStyles<T> = {

export type S = NamedStyles<any>;

export type Fn<T, P> = (theme: T, constants: ConstantsType) => P extends NamedStyles<P> ? NamedStyles<P> : P;

export type StyleSheetType<T> = {
[P in keyof T]: ViewStyle | TextStyle | ImageStyle;
export type Fn<T, P> = (
theme: T,
constants: ConstantsType
) => P extends NamedStyles<P> ? NamedStyles<P> : P;

export type StyleSheetType<P> = {
[K in keyof P]: {
[J in keyof P[K]]: P[K][J] extends Array<any>
? J extends 'transform' | 'transformMatrix'
? P[K][J]
: P[K][J][number]
: P[K][J];
};
};

export type buildEventType = 'build';

export type ThemeProp<T> = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-sugar-style",
"version": "0.1.4",
"version": "0.1.5",
"description": "React Native Stylesheet alternative with theme support",
"author": "mohit23x",
"license": "MIT",
Expand Down

0 comments on commit 62b0884

Please sign in to comment.