|
| 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 | +}); |
0 commit comments