From 9b543107de994aa10eb1bd5dedd7820cf31078c5 Mon Sep 17 00:00:00 2001 From: Yauhen Date: Mon, 29 Apr 2019 09:57:37 +0300 Subject: [PATCH] refactor(ui): button-group layout refactor --- .../ui/buttonGroup/buttonGroup.component.tsx | 56 ++++++------ .../ui/buttonGroup/buttonGroup.spec.tsx | 2 +- src/framework/ui/buttonGroup/type.ts | 51 ----------- src/framework/ui/common/mapping.json | 26 +++++- .../src/ui/screen/buttonGroup.component.tsx | 90 +++++++++++++++---- 5 files changed, 124 insertions(+), 101 deletions(-) delete mode 100644 src/framework/ui/buttonGroup/type.ts diff --git a/src/framework/ui/buttonGroup/buttonGroup.component.tsx b/src/framework/ui/buttonGroup/buttonGroup.component.tsx index 2c7ddf063..fa2f11879 100644 --- a/src/framework/ui/buttonGroup/buttonGroup.component.tsx +++ b/src/framework/ui/buttonGroup/buttonGroup.component.tsx @@ -3,16 +3,13 @@ import { StyleSheet, View, ViewProps, + ViewStyle, } from 'react-native'; import { StyledComponentProps, StyleType, } from '@kitten/theme'; import { Props as ButtonProps } from '../button/button.component'; -import { - ButtonStyleProvider, - ButtonStyleProviders, -} from './type'; type ButtonElement = React.ReactElement; @@ -27,54 +24,46 @@ export class ButtonGroup extends React.Component { static styledComponentName: string = 'ButtonGroup'; - private styleProvider: ButtonStyleProvider = ButtonStyleProviders.DEFAULT; - private getComponentStyle = (style: StyleType): StyleType => { const { - buttonBorderRadius, + buttonBorderRightColor, + buttonBorderRightWidth, ...containerParameters } = style; return { - container: containerParameters, + container: { + ...containerParameters, + ...styles.container, + }, button: { - borderRadius: buttonBorderRadius, + borderRightColor: buttonBorderRightColor, + borderRightWidth: buttonBorderRightWidth, + ...styles.button, }, }; }; - private getChildComponentStyle = (index: number, source: StyleType): StyleType => { + private isLastElement = (index: number): boolean => { const { children } = this.props; - switch (index) { - case 0: - return this.styleProvider.start(source); - case React.Children.count(children) - 1: - return this.styleProvider.end(source); - default: - return this.styleProvider.center(source); - } + return index === React.Children.count(children) - 1; }; private renderComponentChild = (element: ButtonElement, index: number, style: StyleType): ButtonElement => { - const { appearance, size, children } = this.props; - const { style: elementStyle, ...derivedProps } = element.props; + const { appearance, size } = this.props; - const isSingle: boolean = React.Children.count(children) === 1; - const positionedStyle: StyleType = isSingle ? style : this.getChildComponentStyle(index, style); + const additionalStyle: ViewStyle = this.isLastElement(index) ? styles.lastButton : style; return React.cloneElement(element, { - ...derivedProps, - style: [elementStyle, positionedStyle], key: index, + style: [element.props.style, additionalStyle], appearance: appearance, size: size, }); }; - private renderComponentChildren = (source: ButtonElement | ButtonElement[], - style: StyleType): ButtonElement[] => { - + private renderComponentChildren = (source: ButtonElement | ButtonElement[], style: StyleType): ButtonElement[] => { return React.Children.map(source, (element: ButtonElement, index: number): ButtonElement => { return this.renderComponentChild(element, index, style); }); @@ -89,7 +78,7 @@ export class ButtonGroup extends React.Component { return ( + style={[container, style]}> {componentChildren} ); @@ -99,5 +88,16 @@ export class ButtonGroup extends React.Component { const styles = StyleSheet.create({ container: { flexDirection: 'row', + overflow: 'hidden', + }, + button: { + borderRadius: 0, + borderLeftWidth: 0, + borderTopWidth: 0, + borderBottomWidth: 0, + }, + lastButton: { + borderWidth: 0, + borderRadius: 0, }, }); diff --git a/src/framework/ui/buttonGroup/buttonGroup.spec.tsx b/src/framework/ui/buttonGroup/buttonGroup.spec.tsx index 7e1968056..d35f4fde0 100644 --- a/src/framework/ui/buttonGroup/buttonGroup.spec.tsx +++ b/src/framework/ui/buttonGroup/buttonGroup.spec.tsx @@ -42,7 +42,7 @@ describe('@button-group: component checks', () => { @@ -95,7 +101,7 @@ class ButtonGroupScreen extends React.Component { @@ -103,7 +109,7 @@ class ButtonGroupScreen extends React.Component { @@ -111,7 +117,7 @@ class ButtonGroupScreen extends React.Component { @@ -119,7 +125,7 @@ class ButtonGroupScreen extends React.Component { @@ -128,13 +134,61 @@ class ButtonGroupScreen extends React.Component { - + + Outline Icon + Text + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ); } } export default withStyles(ButtonGroupScreen, (theme: ThemeType) => ({ container: { + flex: 1, + }, + content: { paddingVertical: 8, paddingHorizontal: 16, },