Skip to content

Commit 2900b04

Browse files
StheffanyHadlichTrancever
authored andcommittedApr 24, 2020
feat: create Checkbox.Item component
1 parent bc95ae1 commit 2900b04

File tree

7 files changed

+139
-14
lines changed

7 files changed

+139
-14
lines changed
 

‎src/components/Checkbox.tsx renamed to ‎src/components/Checkbox/Checkbox.tsx

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import * as React from 'react';
22
import { Platform } from 'react-native';
3+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
4+
import CheckboxIOS, { CheckboxIOS as _CheckboxIOS } from './CheckboxIOS';
35
import CheckboxAndroid, {
46
// eslint-disable-next-line @typescript-eslint/no-unused-vars
57
CheckboxAndroid as _CheckboxAndroid,
68
} from './CheckboxAndroid';
7-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
8-
import CheckboxIOS, { CheckboxIOS as _CheckboxIOS } from './CheckboxIOS';
9-
import { withTheme } from '../core/theming';
10-
import { Theme } from '../types';
9+
import CheckboxItem from './CheckboxItem';
10+
import { withTheme } from '../../core/theming';
11+
import { Theme } from '../../types';
1112

1213
type Props = {
1314
/**
@@ -81,6 +82,9 @@ type Props = {
8182
* ```
8283
*/
8384
class Checkbox extends React.Component<Props> {
85+
// @component ./CheckboxItem.tsx
86+
static Item = CheckboxItem;
87+
8488
// @component ./CheckboxAndroid.tsx
8589
static Android = CheckboxAndroid;
8690

‎src/components/CheckboxAndroid.tsx renamed to ‎src/components/Checkbox/CheckboxAndroid.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as React from 'react';
22
import { Animated, View, StyleSheet, I18nManager } from 'react-native';
33
import color from 'color';
4-
import MaterialCommunityIcon from './MaterialCommunityIcon';
5-
import TouchableRipple from './TouchableRipple';
6-
import { withTheme } from '../core/theming';
7-
import { Theme, $RemoveChildren } from '../types';
4+
import MaterialCommunityIcon from '../MaterialCommunityIcon';
5+
import TouchableRipple from '../TouchableRipple';
6+
import { withTheme } from '../../core/theming';
7+
import { Theme, $RemoveChildren } from '../../types';
88

99
type Props = $RemoveChildren<typeof TouchableRipple> & {
1010
/**

‎src/components/CheckboxIOS.tsx renamed to ‎src/components/Checkbox/CheckboxIOS.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as React from 'react';
22
import { StyleSheet, View, I18nManager } from 'react-native';
33
import color from 'color';
4-
import MaterialCommunityIcon from './MaterialCommunityIcon';
5-
import TouchableRipple from './TouchableRipple';
6-
import { withTheme } from '../core/theming';
7-
import { Theme, $RemoveChildren } from '../types';
4+
import MaterialCommunityIcon from '../MaterialCommunityIcon';
5+
import TouchableRipple from '../TouchableRipple';
6+
import { withTheme } from '../../core/theming';
7+
import { Theme, $RemoveChildren } from '../../types';
88

99
type Props = $RemoveChildren<typeof TouchableRipple> & {
1010
/**
+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import * as React from 'react';
2+
3+
import {
4+
StyleProp,
5+
StyleSheet,
6+
TextStyle,
7+
View,
8+
ViewStyle,
9+
} from 'react-native';
10+
11+
import CheckBox from './Checkbox';
12+
import Text from '../Typography/Text';
13+
import { Theme } from '../../types';
14+
import TouchableRipple from '../TouchableRipple';
15+
import { withTheme } from '../../core/theming';
16+
17+
type Props = {
18+
/**
19+
* Status of checkbox.
20+
*/
21+
status: 'checked' | 'unchecked' | 'indeterminate';
22+
/**
23+
* Whether checkbox is disabled.
24+
*/
25+
disabled?: boolean;
26+
/**
27+
* Label to be displayed on the item.
28+
*/
29+
label: string;
30+
/**
31+
* Function to execute on press.
32+
*/
33+
onPress?: () => void;
34+
/**
35+
* Custom color for unchecked checkbox.
36+
*/
37+
uncheckedColor?: string;
38+
/**
39+
* Custom color for checkbox.
40+
*/
41+
color?: string;
42+
/**
43+
* Additional styles for container View.
44+
*/
45+
style?: StyleProp<ViewStyle>;
46+
/**
47+
* Style that is passed to Label element.
48+
*/
49+
labelStyle?: StyleProp<TextStyle>;
50+
/**
51+
* @optional
52+
*/
53+
theme: Theme;
54+
};
55+
56+
/**
57+
* Checkbox.Item allows you to press the whole row (item) instead of only the Checkbox.
58+
*
59+
* ## Usage
60+
* ```js
61+
* import * as React from 'react';
62+
* import { View } from 'react-native';
63+
* import { Checkbox, Text } from 'react-native-paper';
64+
*
65+
* export default class MyComponent extends React.Component {
66+
*
67+
* render() {
68+
* return(
69+
* <View>
70+
* <Checkbox.Item label="Item" status="checked" />
71+
* </View>
72+
* )
73+
* }
74+
* }
75+
*```
76+
*/
77+
78+
class CheckboxItem extends React.Component<Props> {
79+
static displayName = 'RadioButton.Item';
80+
81+
render() {
82+
const {
83+
style,
84+
status,
85+
label,
86+
onPress,
87+
labelStyle,
88+
theme: { colors },
89+
...props
90+
} = this.props;
91+
92+
return (
93+
<TouchableRipple onPress={onPress}>
94+
<View style={[styles.container, style]} pointerEvents="none">
95+
<Text style={[styles.label, labelStyle, { color: colors.primary }]}>
96+
{label}
97+
</Text>
98+
<CheckBox status={status} {...props}></CheckBox>
99+
</View>
100+
</TouchableRipple>
101+
);
102+
}
103+
}
104+
105+
export default withTheme(CheckboxItem);
106+
107+
// @component-docs ignore-next-line
108+
export { CheckboxItem };
109+
110+
const styles = StyleSheet.create({
111+
container: {
112+
flexDirection: 'row',
113+
alignItems: 'center',
114+
justifyContent: 'space-between',
115+
paddingVertical: 8,
116+
paddingHorizontal: 16,
117+
},
118+
label: {
119+
fontSize: 16,
120+
},
121+
});

‎src/components/__tests__/Checkbox.test.js renamed to ‎src/components/__tests__/Checkbox/Checkbox.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import renderer from 'react-test-renderer';
3-
import Checkbox from '../Checkbox.tsx';
3+
import Checkbox from '../../Checkbox/Checkbox';
44

55
it('renders checked Checkbox with onPress', () => {
66
const tree = renderer

‎src/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export { default as Banner } from './components/Banner';
2626
export { default as BottomNavigation } from './components/BottomNavigation';
2727
export { default as Button } from './components/Button';
2828
export { default as Card } from './components/Card/Card';
29-
export { default as Checkbox } from './components/Checkbox';
29+
export { default as Checkbox } from './components/Checkbox/Checkbox';
3030
export { default as Chip } from './components/Chip';
3131
export { default as DataTable } from './components/DataTable/DataTable';
3232
export { default as Dialog } from './components/Dialog/Dialog';

0 commit comments

Comments
 (0)