Skip to content

Commit f55b129

Browse files
authored
Merge pull request #12 from mohit23x/responsive-values
v0.1.4 allow undefined value in responsive array values
2 parents a5dc6c4 + 7dd09d5 commit f55b129

7 files changed

Lines changed: 24 additions & 16 deletions

File tree

build/Sheet.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { ConstantsType, Fn, NamedStyles, StyleSheetType } from './type';
2-
export default class Sheet<T, S extends NamedStyles<S> | NamedStyles<any>, O extends StyleSheetType<O> | StyleSheetType<any>> {
1+
import type { ConstantsType, Fn, NamedStyles } from './type';
2+
export default class Sheet<T, S extends NamedStyles<S> | NamedStyles<any>, O = S> {
33
result: O;
44
source: Fn<T, S>;
55
nativeSheet: O;

build/Sheet.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,15 @@ class Sheet {
2929
Object.keys(styles).forEach((styleKey) => {
3030
const styleValue = styles[styleKey];
3131
if (styleValue && Array.isArray(styleValue)) {
32-
const selectedValue = styleValue[activeIndex] || styleValue[styleValue.length - 1];
33-
styles[styleKey] = selectedValue;
32+
// length is checked to allow undefined to set as styleValue
33+
if (activeIndex >= styleValue.length) {
34+
const selectedValue = styleValue[styleValue.length - 1];
35+
styles[styleKey] = selectedValue;
36+
}
37+
else {
38+
const selectedValue = styleValue[activeIndex];
39+
styles[styleKey] = selectedValue;
40+
}
3441
}
3542
});
3643
// @ts-ignore

build/Sugar.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { StyleSheet } from 'react-native';
2-
import { Fn, buildEventType, NamedStyles, ConstantsType, StyleSheetType } from './type';
2+
import { Fn, buildEventType, NamedStyles, ConstantsType } from './type';
33
import Sheet from './Sheet';
44
export default class Sugar<T> {
55
builded: boolean;
@@ -99,7 +99,7 @@ export default class Sugar<T> {
9999
_refresh(): void;
100100
build(themeObj: T): void;
101101
configure(newConstants: Partial<ConstantsType>): void;
102-
create<P extends NamedStyles<P> | NamedStyles<any>, O extends StyleSheetType<P> | StyleSheetType<any>>(objFn: Fn<T, P>): P;
102+
create<P extends NamedStyles<P> | NamedStyles<any>>(objFn: Fn<T, P>): P;
103103
_calculateActiveIndex(): void;
104104
_calcSheets(): void;
105105
_callListeners(event: buildEventType): void;

example/App.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from "react";
22
import { ThemeProvider } from "./style";
33
import Screen from "./screens";
4-
import { StatusBar } from "react-native";
54

65
export default function App() {
76
return (

lib/Sheet.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { ConstantsType, Fn, NamedStyles, StyleSheetType } from './type';
44
export default class Sheet<
55
T,
66
S extends NamedStyles<S> | NamedStyles<any>,
7-
O extends StyleSheetType<O> | StyleSheetType<any>
7+
O = S
88
> {
99
public result: O;
1010
public source: Fn<T, S>;
@@ -44,9 +44,14 @@ export default class Sheet<
4444
Object.keys(styles).forEach((styleKey) => {
4545
const styleValue = styles[styleKey];
4646
if (styleValue && Array.isArray(styleValue)) {
47-
const selectedValue =
48-
styleValue[activeIndex] || styleValue[styleValue.length - 1];
49-
styles[styleKey] = selectedValue;
47+
// length is checked to allow undefined to set as styleValue
48+
if (activeIndex >= styleValue.length) {
49+
const selectedValue = styleValue[styleValue.length - 1];
50+
styles[styleKey] = selectedValue;
51+
} else {
52+
const selectedValue = styleValue[activeIndex];
53+
styles[styleKey] = selectedValue;
54+
}
5055
}
5156
});
5257
// @ts-ignore

lib/Sugar.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,7 @@ export default class Sugar<T> {
145145
this._refresh();
146146
}
147147

148-
create<
149-
P extends NamedStyles<P> | NamedStyles<any>,
150-
O extends StyleSheetType<P> | StyleSheetType<any>
151-
>(objFn: Fn<T, P>): P {
148+
create<P extends NamedStyles<P> | NamedStyles<any>>(objFn: Fn<T, P>): P {
152149
if (typeof objFn === 'function') {
153150
const sheet = new Sheet(objFn);
154151
this.sheets.push(sheet);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-sugar-style",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "React Native Stylesheet alternative with theme support",
55
"author": "mohit23x",
66
"license": "MIT",

0 commit comments

Comments
 (0)