Skip to content

Feat/add label support to checkbox #959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Oct 26, 2020
Merged
18 changes: 14 additions & 4 deletions demo/src/screens/componentScreens/CheckboxScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React, {Component} from 'react';
import {Checkbox, Assets, Text, View, Colors} from 'react-native-ui-lib'; //eslint-disable-line

import {Checkbox, Assets, Text, View, Colors, Spacings} from 'react-native-ui-lib'; //eslint-disable-line

class CheckboxScreen extends Component {
state = {
value1: false,
value2: false,
value3: true,
value4: true,
value5: false
value5: false,
value6: false
};

render() {
Expand All @@ -21,6 +21,14 @@ class CheckboxScreen extends Component {
onValueChange={value1 => this.setState({value1})}
style={{marginBottom: 20}}
/>
<Checkbox
value={this.state.value6}
label={'with label'}
labelStyle={{marginLeft: Spacings.s3}}
color={Colors.green20}
onValueChange={value6 => this.setState({value6})}
style={{marginBottom: 20, marginLeft: 75}}
/>
<Checkbox
value={this.state.value2}
onValueChange={value2 => this.setState({value2})}
Expand All @@ -40,7 +48,9 @@ class CheckboxScreen extends Component {
style={{marginBottom: 20}}
/>
<View row marginB-20>
<Text text70 centerV>Disabled: </Text>
<Text text70 centerV>
Disabled:{' '}
</Text>
<Checkbox
disabled
value={this.state.value5}
Expand Down
10 changes: 9 additions & 1 deletion generatedTypes/components/checkbox/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { StyleProp, TouchableOpacityProps, ViewStyle } from 'react-native';
import { StyleProp, TouchableOpacityProps, ViewStyle, TextStyle } from 'react-native';
export interface CheckboxPropTypes extends TouchableOpacityProps {
/**
* The value of the Checkbox. If true the switch will be turned on. Default value is false.
Expand Down Expand Up @@ -33,6 +33,14 @@ export interface CheckboxPropTypes extends TouchableOpacityProps {
* The selected icon color
*/
iconColor?: string;
/**
* The label of the checkbox
*/
label?: string;
/**
* The style of the label
*/
labelStyle?: StyleProp<TextStyle>;
/**
* Additional styling
*/
Expand Down
76 changes: 49 additions & 27 deletions src/components/checkbox/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import _ from 'lodash';
import React, {Component} from 'react';
import {Animated, Easing, StyleSheet, StyleProp, TouchableOpacityProps, ViewStyle} from 'react-native';
import {
Text,
Animated,
Easing,
StyleSheet,
StyleProp,
TouchableOpacityProps,
ViewStyle,
TextStyle
} from 'react-native';
import {Colors} from '../../style';
//@ts-ignore
import Assets from '../../assets';
Expand Down Expand Up @@ -45,15 +54,23 @@ export interface CheckboxPropTypes extends TouchableOpacityProps {
* The selected icon color
*/
iconColor?: string;
/**
* The label of the checkbox
*/
label?: string;
/**
* The style of the label
*/
labelStyle?: StyleProp<TextStyle>;
/**
* Additional styling
*/
style?: StyleProp<ViewStyle>
style?: StyleProp<ViewStyle>;
}

interface CheckboxState {
isChecked: Animated.Value;
};
}

/**
* @description: Checkbox component for toggling boolean value related to some context
Expand Down Expand Up @@ -155,33 +172,38 @@ class Checkbox extends Component<CheckboxPropTypes, CheckboxState> {
}

render() {
const {selectedIcon, color, iconColor, disabled, testID, style, ...others} = this.props;
const {selectedIcon, color, iconColor, disabled, testID, label, labelStyle, style, ...others} = this.props;
return (
// @ts-ignore
<TouchableOpacity
{...this.getAccessibilityProps()}
activeOpacity={1}
testID={testID}
{...others}
style={[this.getBorderStyle(), style]}
onPress={this.onPress}
>
{
<Animated.View
style={[this.styles.container, {backgroundColor: this.getColor()}, {opacity: this.animationStyle.opacity}]}
>
<Animated.Image
<TouchableOpacity row onPress={this.onPress}>
<Animated.View
{...this.getAccessibilityProps()}
activeOpacity={1}
testID={testID}
{...others}
style={[this.getBorderStyle(), style]}
>
{
<Animated.View
style={[
this.styles.selectedIcon,
color && {tintColor: iconColor},
{transform: this.animationStyle.transform},
disabled && {tintColor: DEFAULT_ICON_COLOR}
this.styles.container,
{backgroundColor: this.getColor()},
{opacity: this.animationStyle.opacity}
]}
source={selectedIcon || Assets.icons.checkSmall}
testID={`${testID}.selected`}
/>
</Animated.View>
}
>
<Animated.Image
style={[
this.styles.selectedIcon,
color && {tintColor: iconColor},
{transform: this.animationStyle.transform},
disabled && {tintColor: DEFAULT_ICON_COLOR}
]}
source={selectedIcon || Assets.icons.checkSmall}
testID={`${testID}.selected`}
/>
</Animated.View>
}
</Animated.View>
{label && <Text style={labelStyle}>{label}</Text>}
</TouchableOpacity>
);
}
Expand Down