[add] CheckBox component

Implements the CheckBox component and adds a web-only 'color' prop to
allow the color of the checkbox to be customized.
This commit is contained in:
Nicolas Gallagher
2017-12-20 14:51:44 +00:00
parent 495defd69b
commit 6de892c92b
14 changed files with 582 additions and 8 deletions
@@ -0,0 +1,93 @@
/* eslint-disable react/jsx-sort-props */
/**
* @flow
*/
import CustomSize from './examples/CustomSize';
import PropColor from './examples/PropColor';
import PropDisabled from './examples/PropDisabled';
import PropOnValueChange from './examples/PropOnValueChange';
import PropValue from './examples/PropValue';
import React from 'react';
import UIExplorer, {
AppText,
Code,
Description,
DocItem,
Section,
storiesOf
} from '../../ui-explorer';
const CheckBoxScreen = () => (
<UIExplorer title="CheckBox" url="components/CheckBox">
<Description>
<AppText>
This is a controlled component that requires an <Code>onValueChange</Code> callback that
updates the value prop in order for the component to reflect user actions. If the{' '}
<Code>value</Code> prop is not updated, the component will continue to render the supplied{' '}
<Code>value</Code> prop instead of the expected result of any user actions.
</AppText>
</Description>
<Section title="Props">
<DocItem name="...View props" />
<DocItem
description="Customize the color of the checkbox."
example={{
render: () => <PropColor />
}}
label="web"
name="color"
typeInfo="?color"
/>
<DocItem
description="If true, the user won't be able to interact with the checkbox."
example={{
render: () => <PropDisabled />
}}
name="disabled"
typeInfo="?boolean = false"
/>
<DocItem
description="Invoked with the event when the value changes."
name="onChange"
typeInfo="?function"
/>
<DocItem
description="Invoked with the new value when the value changes."
example={{
render: () => <PropOnValueChange />
}}
name="onValueChange"
typeInfo="?function"
/>
<DocItem
description="The value of the checkbox. If `true` the checkbox will be checked."
example={{
render: () => <PropValue />
}}
name="value"
typeInfo="?boolean = false"
/>
</Section>
<Section title="More examples">
<DocItem
description="The checkbox size can be controlled by the 'height' and 'width' style properties"
example={{
code: '<CheckBox style={{ height: 32, width: 32 }} />',
render: () => <CustomSize />
}}
name="Custom size"
/>
</Section>
</UIExplorer>
);
storiesOf('Components', module).add('CheckBox', CheckBoxScreen);
@@ -0,0 +1,20 @@
/**
* @flow
*/
import React from 'react';
import styles from './styles';
import { CheckBox, View } from 'react-native';
const CustomSizeExample = () => (
<View style={styles.row}>
<View style={styles.marginRight}>
<CheckBox style={{ height: 20, width: 20 }} value />
</View>
<View style={styles.marginRight}>
<CheckBox style={{ height: 32, width: 32 }} value />
</View>
</View>
);
export default CustomSizeExample;
@@ -0,0 +1,20 @@
/**
* @flow
*/
import React from 'react';
import styles from './styles';
import { CheckBox, View } from 'react-native';
const CheckBoxColorExample = () => (
<View style={styles.row}>
<View style={styles.marginRight}>
<CheckBox color="#1DA1F2" value />
</View>
<View style={styles.marginRight}>
<CheckBox color="#F45D22" value />
</View>
</View>
);
export default CheckBoxColorExample;
@@ -0,0 +1,20 @@
/**
* @flow
*/
import React from 'react';
import styles from './styles';
import { CheckBox, View } from 'react-native';
const CheckBoxDisabledExample = () => (
<View style={styles.row}>
<View style={styles.marginRight}>
<CheckBox disabled value={false} />
</View>
<View style={styles.marginRight}>
<CheckBox disabled value />
</View>
</View>
);
export default CheckBoxDisabledExample;
@@ -0,0 +1,59 @@
/**
* @flow
*/
import styles from './styles';
import React, { PureComponent } from 'react';
import { CheckBox, Text, View } from 'react-native';
class CheckBoxOnValueChangeExample extends PureComponent {
state = {
eventSwitchIsOn: false,
eventSwitchRegressionIsOn: true
};
render() {
const { eventSwitchIsOn, eventSwitchRegressionIsOn } = this.state;
return (
<View style={styles.row}>
<View style={[styles.alignCenter, styles.marginRight]}>
<CheckBox
onValueChange={this._handleEventSwitch}
style={styles.marginBottom}
value={eventSwitchIsOn}
/>
<CheckBox
onValueChange={this._handleEventSwitch}
style={styles.marginBottom}
value={eventSwitchIsOn}
/>
<Text>{eventSwitchIsOn ? 'On' : 'Off'}</Text>
</View>
<View style={styles.alignCenter}>
<CheckBox
onValueChange={this._handleEventSwitchRegression}
style={styles.marginBottom}
value={eventSwitchRegressionIsOn}
/>
<CheckBox
onValueChange={this._handleEventSwitchRegression}
style={styles.marginBottom}
value={eventSwitchRegressionIsOn}
/>
<Text>{eventSwitchRegressionIsOn ? 'On' : 'Off'}</Text>
</View>
</View>
);
}
_handleEventSwitch = value => {
this.setState({ eventSwitchIsOn: value });
};
_handleEventSwitchRegression = value => {
this.setState({ eventSwitchRegressionIsOn: value });
};
}
export default CheckBoxOnValueChangeExample;
+20
View File
@@ -0,0 +1,20 @@
/**
* @flow
*/
import React from 'react';
import styles from './styles';
import { CheckBox, View } from 'react-native';
const CheckBoxValueExample = () => (
<View style={styles.row}>
<View style={styles.marginRight}>
<CheckBox value={false} />
</View>
<View style={styles.marginRight}>
<CheckBox value />
</View>
</View>
);
export default CheckBoxValueExample;
+23
View File
@@ -0,0 +1,23 @@
/**
* @flow
*/
import { StyleSheet } from 'react-native';
const styles = StyleSheet.create({
row: {
flexDirection: 'row',
flexWrap: 'wrap'
},
marginRight: {
marginRight: 10
},
marginBottom: {
marginBottom: 10
},
alignCenter: {
alignItems: 'center'
}
});
export default styles;
@@ -0,0 +1,36 @@
/**
* @flow
*/
import React from 'react';
import { StyleSheet, View } from 'react-native';
const DividerHorizontal = () => <View style={styles.horizontalDivider} />;
const DividerVertical = () => <View style={styles.verticalDivider} />;
export const styles = StyleSheet.create({
horizontalDivider: {
width: '0.6rem'
},
verticalDivider: {
height: '1.3125rem'
},
row: {
flexDirection: 'row',
flexWrap: 'wrap'
},
marginRight: {
marginRight: 10
},
marginBottom: {
marginBottom: 10
},
marginVertical: {
marginVertical: 5
},
alignCenter: {
alignItems: 'center'
}
});
export { DividerHorizontal, DividerVertical };