Skip to content

Commit

Permalink
feat: enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
artyorsh authored Apr 2, 2019
1 parent 47d9c15 commit 3b807ac
Show file tree
Hide file tree
Showing 16 changed files with 254 additions and 75 deletions.
6 changes: 5 additions & 1 deletion src/framework/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export {
withStyles,
} from './component';

export { ModalService } from './service';
export {
all,
allWithRest,
ModalService,
} from './service';

export * from './type';
1 change: 1 addition & 0 deletions src/framework/theme/service/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './theme';
export * from './style';
export * from './modal';
export * from './props';
4 changes: 4 additions & 0 deletions src/framework/theme/service/props/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export {
all,
allWithRest,
} from './props.service';
48 changes: 48 additions & 0 deletions src/framework/theme/service/props/props.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export interface Props {
[key: string]: any;
}

export interface RestProps {
rest?: Partial<Props>;
}

export type AllOfProps = Partial<Props>;

export type AllWithRestProps = Partial<Props> & RestProps;

/**
* Retrieves all props included in `from` array
*
* @param source (Props) - source object
* @param from (string[]) - array of keys needed to retrieve from `source`
*
* @return (Partial<Props>) - object with keys contained in `from` array
*/
export function all(source: Props | undefined, from: string[]): AllOfProps {
if (!source) {
return {};
}

return from.reduce((acc: Partial<AllOfProps>, prop: string): Partial<AllOfProps> => {
return { ...acc, [prop]: source[prop] };
}, {});
}

/**
* Retrieves all props included in `from` array, rest props includes in under the `rest` key
*/
export function allWithRest(source: Props | undefined, from: string[]): AllWithRestProps {
if (!source) {
return { rest: {} };
}

return Object.keys(source).reduce((acc: Partial<AllWithRestProps>, prop: string): Partial<AllWithRestProps> => {
const { rest, ...allOf } = acc;

if (from.includes(prop)) {
return { ...allOf, [prop]: source[prop], rest };
}

return { ...allOf, rest: { ...rest, [prop]: source[prop] } };
}, {});
}
70 changes: 70 additions & 0 deletions src/framework/theme/service/props/props.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {
all,
AllOfProps,
allWithRest,
AllWithRestProps,
Props,
} from './props.service';

describe('@props: service checks', () => {

const json = (value: any): string => JSON.stringify(value);

const props: Props = {
backgroundColor: 'black',
color: 'white',
fontSize: 32,
};

const textProps: string[] = [
'color',
'fontSize',
];

describe('* all', () => {

it('* non-null', () => {
const retrievedProps: AllOfProps = all(props, textProps);

expect(json(retrievedProps)).toEqual(json({
color: 'white',
fontSize: 32,
}));
});

it('* nullable', () => {
const retrievedProps: AllOfProps = all(undefined, textProps);

expect(json(retrievedProps)).toEqual(json({}));
});

});

describe('* allWithRest', () => {

it('* non-null', () => {
const retrievedProps: AllWithRestProps = allWithRest(props, textProps);

const { rest, ...allOf } = retrievedProps;

expect(json(allOf)).toEqual(json({
color: 'white',
fontSize: 32,
}));

expect(json(rest)).toEqual(json({
backgroundColor: 'black',
}));
});

it('* nullable', () => {
const retrievedProps: AllWithRestProps = allWithRest(undefined, textProps);

expect(json(retrievedProps)).toEqual(json({
rest: {},
}));
});

});

});
2 changes: 1 addition & 1 deletion src/framework/ui/avatar/avatar.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Avatar extends React.Component<Props> {
// @ts-ignore: rhs operator is restricted to be number
const borderRadius: number = roundCoefficient * baseStyle.height;

return { ...baseStyle, borderRadius };
return { borderRadius, ...baseStyle };
};

public render(): React.ReactElement<TouchableOpacityProps> {
Expand Down
17 changes: 14 additions & 3 deletions src/framework/ui/button/button.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import {
GestureResponderEvent,
StyleSheet,
ImageProps,
StyleProp,
TextStyle,
} from 'react-native';
import {
StyledComponentProps,
StyleType,
Interaction,
allWithRest,
styled,
} from '@kitten/theme';
import {
Expand All @@ -20,8 +23,10 @@ import {
ButtonAlignment,
ButtonAlignments,
} from './type';
import { TextStyleProps } from '../common/props';

interface ButtonProps {
style?: StyleProp<TextStyle>;
icon?: (style: StyleType) => React.ReactElement<ImageProps>;
status?: string;
size?: string;
Expand Down Expand Up @@ -64,6 +69,9 @@ export class Button extends React.Component<Props> {
};

private getComponentStyle = (style: StyleType): StyleType => {
const derivedStyle: TextStyle = StyleSheet.flatten(this.props.style);
const { rest: derivedContainerStyle, ...derivedTextStyle } = allWithRest(derivedStyle, TextStyleProps);

const {
textColor,
textFontSize,
Expand All @@ -81,13 +89,16 @@ export class Button extends React.Component<Props> {
return {
container: {
...containerParameters,
...derivedContainerStyle,
...styles.container,
flexDirection: alignment.flex(),
},
text: {
color: textColor,
fontSize: textFontSize,
fontWeight: textFontWeight,
marginHorizontal: textMarginHorizontal,
...derivedTextStyle,
},
icon: {
width: iconWidth,
Expand Down Expand Up @@ -128,15 +139,15 @@ export class Button extends React.Component<Props> {
};

public render(): React.ReactElement<TouchableOpacityProps> {
const { style, themedStyle, ...derivedProps } = this.props;
const { themedStyle, ...derivedProps } = this.props;
const { container, ...componentStyles } = this.getComponentStyle(themedStyle);
const componentChildren: React.ReactNode = this.renderComponentChildren(componentStyles);

return (
<TouchableOpacity
{...derivedProps}
style={[container, style, styles.container]}
activeOpacity={1.0}
{...derivedProps}
style={container}
onPress={this.onPress}
onPressIn={this.onPressIn}
onPressOut={this.onPressOut}>
Expand Down
42 changes: 31 additions & 11 deletions src/framework/ui/checkbox/checkbox.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@ import {
StyleSheet,
TouchableOpacityProps,
GestureResponderEvent,
StyleProp,
TextStyle,
} from 'react-native';
import {
StyledComponentProps,
StyleType,
Interaction,
styled,
allWithRest,
} from '@kitten/theme';
import {
Text as TextComponent,
Props as TextProps,
} from '../text/text.component';
import { CheckMark } from '../drawable/checkmark/checkmark.component';
import { TextStyleProps } from '../common/props';

interface CheckBoxProps {
style?: StyleProp<TextStyle>;
text?: string;
checked?: boolean;
status?: string;
Expand Down Expand Up @@ -55,6 +60,9 @@ export class CheckBox extends React.Component<Props> {
};

private getComponentStyle = (style: StyleType): StyleType => {
const derivedStyle: TextStyle = StyleSheet.flatten(this.props.style);
const { rest: derivedContainerStyle, ...derivedTextStyle } = allWithRest(derivedStyle, TextStyleProps);

const {
textColor,
textFontSize,
Expand All @@ -71,23 +79,35 @@ export class CheckBox extends React.Component<Props> {
} = style;

return {
selectContainer: containerParameters,
container: {
...derivedContainerStyle,
...styles.container,
},
highlightContainer: styles.highlightContainer,
selectContainer: {
...containerParameters,
...styles.selectContainer,
},
text: {
color: textColor,
fontSize: textFontSize,
fontWeight: textFontWeight,
marginLeft: textMarginLeft,
...derivedTextStyle,
...styles.text,
},
select: {
width: selectWidth,
height: selectHeight,
backgroundColor: selectBackgroundColor,
...styles.select,
},
highlight: {
width: highlightWidth,
height: highlightHeight,
borderRadius: highlightBorderRadius,
backgroundColor: highlightBackgroundColor,
...styles.highlight,
},
};
};
Expand All @@ -96,35 +116,35 @@ export class CheckBox extends React.Component<Props> {
const { text } = this.props;

return (
<Text style={[style, styles.text]} key={0}>{text}</Text>
<Text style={style} key={0}>{text}</Text>
);
};

private renderComponentChildren = (style: StyleType): React.ReactNode => {
const { text } = this.props;

return [
text ? this.renderTextElement(style.text) : undefined,
text ? this.renderTextElement(style) : undefined,
];
};

public render(): React.ReactElement<TouchableOpacityProps> {
const { style, themedStyle, ...derivedProps } = this.props;
const { selectContainer, select, highlight, ...componentStyles } = this.getComponentStyle(themedStyle);
const componentChildren: React.ReactNode = this.renderComponentChildren(componentStyles);
const { themedStyle, ...derivedProps } = this.props;
const componentStyle: StyleType = this.getComponentStyle(themedStyle);
const componentChildren: React.ReactNode = this.renderComponentChildren(componentStyle.text);

return (
<View style={[style, styles.container]}>
<View style={styles.highlightContainer}>
<View style={[highlight, styles.highlight]}/>
<View style={componentStyle.container}>
<View style={componentStyle.highlightContainer}>
<View style={componentStyle.highlight}/>
<TouchableOpacity
{...derivedProps}
style={[selectContainer, styles.selectContainer]}
style={componentStyle.selectContainer}
activeOpacity={1.0}
onPress={this.onPress}
onPressIn={this.onPressIn}
onPressOut={this.onPressOut}>
<CheckMark style={[select, styles.select]}/>
<CheckMark style={componentStyle.select}/>
</TouchableOpacity>
</View>
{componentChildren}
Expand Down
31 changes: 31 additions & 0 deletions src/framework/ui/common/props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const TextStyleProps: string[] = [

// TextStyle

'color',
'fontFamily',
'fontSize',
'fontStyle',
'fontWeight',
'letterSpacing',
'lineHeight',
'textAlign',
'textAlign',
'textDecorationLine',
'textDecorationStyle',
'textDecorationColor',
'textShadowColor',
'textShadowColor',
'textShadowOffset',
'textShadowRadius',

// TextStyleIOS

'textTransform',
'writingDirection',

// TextStyleAndroid

'textAlignVertical',
'includeFontPadding',
];
8 changes: 4 additions & 4 deletions src/framework/ui/common/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

// @ts-ignore: props override
export interface TouchableOpacityIndexedProps extends TouchableOpacityProps {
onPress?: (event: GestureResponderEvent, index: number) => void;
onPressIn?: (event: GestureResponderEvent, index: number) => void;
onPressOut?: (event: GestureResponderEvent, index: number) => void;
onLongPress?: (event: GestureResponderEvent, index: number) => void;
onPress?: (index: number, event: GestureResponderEvent) => void;
onPressIn?: (index: number, event: GestureResponderEvent) => void;
onPressOut?: (index: number, event: GestureResponderEvent) => void;
onLongPress?: (index: number, event: GestureResponderEvent) => void;
}
1 change: 1 addition & 0 deletions src/framework/ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export {
TabProps,
TabBarProps,
TabViewProps,
TextProps,
TooltipProps,
ToggleProps,
TopNavigationBarProps,
Expand Down
Loading

0 comments on commit 3b807ac

Please sign in to comment.