UNPKG

6.76 kBJavaScriptView Raw
1var __rest = (this && this.__rest) || function (s, e) {
2 var t = {};
3 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4 t[p] = s[p];
5 if (s != null && typeof Object.getOwnPropertySymbols === "function")
6 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8 t[p[i]] = s[p[i]];
9 }
10 return t;
11};
12import React from 'react';
13import { View, TouchableNativeFeedback, TouchableOpacity, Platform, StyleSheet, } from 'react-native';
14import { withTheme } from '../config';
15import { normalizeText, color } from '../helpers';
16import Text from '../text/Text';
17const ButtonGroup = (props) => {
18 var _a;
19 const { theme } = props, rest = __rest(props, ["theme"]);
20 const { Component = Platform.select({
21 android: TouchableNativeFeedback,
22 default: TouchableOpacity,
23 }), buttons, onPress = () => null, selectedIndex = null, selectedIndexes = [], selectMultiple = false, containerStyle, innerBorderStyle, buttonStyle, buttonContainerStyle, textStyle, selectedTextStyle, selectedButtonStyle, underlayColor = (_a = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _a === void 0 ? void 0 : _a.primary, activeOpacity, onHideUnderlay, onShowUnderlay, setOpacityTo, disabled = false, disabledStyle, disabledTextStyle, disabledSelectedStyle, disabledSelectedTextStyle, vertical = false } = rest, attributes = __rest(rest, ["Component", "buttons", "onPress", "selectedIndex", "selectedIndexes", "selectMultiple", "containerStyle", "innerBorderStyle", "buttonStyle", "buttonContainerStyle", "textStyle", "selectedTextStyle", "selectedButtonStyle", "underlayColor", "activeOpacity", "onHideUnderlay", "onShowUnderlay", "setOpacityTo", "disabled", "disabledStyle", "disabledTextStyle", "disabledSelectedStyle", "disabledSelectedTextStyle", "vertical"]);
24 let innerBorderWidth = 1;
25 if (innerBorderStyle &&
26 Object.prototype.hasOwnProperty.call(innerBorderStyle, 'width')) {
27 innerBorderWidth = innerBorderStyle.width;
28 }
29 return (<View {...attributes} style={StyleSheet.flatten([
30 styles.container,
31 vertical && styles.verticalContainer,
32 containerStyle && containerStyle,
33 ])}>
34 {buttons === null || buttons === void 0 ? void 0 : buttons.map((button, i) => {
35 var _a, _b, _c, _d, _e, _f;
36 const isSelected = selectedIndex === i || selectedIndexes.includes(i);
37 const isDisabled = disabled === true ||
38 (Array.isArray(disabled) && disabled.includes(i));
39 return (<View key={i} style={StyleSheet.flatten([
40 styles.button,
41 vertical && styles.verticalComponent,
42 i !== buttons.length - 1 &&
43 (vertical
44 ? {
45 borderBottomWidth: innerBorderWidth,
46 borderBottomColor: (innerBorderStyle && innerBorderStyle.color) || ((_a = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _a === void 0 ? void 0 : _a.grey4),
47 }
48 : {
49 borderRightWidth: innerBorderWidth,
50 borderRightColor: (innerBorderStyle && innerBorderStyle.color) || ((_b = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _b === void 0 ? void 0 : _b.grey4),
51 }),
52 buttonContainerStyle,
53 ])}>
54 <Component testID="buttonGroupItem" activeOpacity={activeOpacity} setOpacityTo={setOpacityTo} onHideUnderlay={onHideUnderlay} onShowUnderlay={onShowUnderlay} underlayColor={underlayColor} disabled={isDisabled} onPress={() => {
55 if (selectMultiple) {
56 if (selectedIndexes.includes(i)) {
57 onPress(selectedIndexes.filter((index) => index !== i));
58 }
59 else {
60 onPress([...selectedIndexes, i]);
61 }
62 }
63 else {
64 onPress(i);
65 }
66 }} style={styles.button}>
67 <View style={StyleSheet.flatten([
68 styles.textContainer,
69 buttonStyle && buttonStyle,
70 isSelected && {
71 backgroundColor: (_c = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _c === void 0 ? void 0 : _c.primary,
72 },
73 isSelected && selectedButtonStyle && selectedButtonStyle,
74 isDisabled && styles.disabled,
75 isDisabled && disabledStyle,
76 isDisabled &&
77 isSelected && {
78 backgroundColor: (_d = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _d === void 0 ? void 0 : _d.disabled,
79 },
80 isDisabled && isSelected && disabledSelectedStyle,
81 ])}>
82 {button.element ? (<button.element />) : (<Text testID="buttonGroupItemText" style={StyleSheet.flatten([
83 Object.assign({ fontSize: normalizeText(13), color: (_e = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _e === void 0 ? void 0 : _e.grey2 }, Platform.select({
84 android: {},
85 default: {
86 fontWeight: '500',
87 },
88 })),
89 textStyle && textStyle,
90 isSelected && { color: '#fff' },
91 isSelected && selectedTextStyle,
92 isDisabled && {
93 color: color((_f = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _f === void 0 ? void 0 : _f.disabled)
94 .darken(0.3)
95 .toString(),
96 },
97 isDisabled && disabledTextStyle,
98 isDisabled && isSelected && disabledSelectedTextStyle,
99 ])}>
100 {button}
101 </Text>)}
102 </View>
103 </Component>
104 </View>);
105 })}
106 </View>);
107};
108const styles = StyleSheet.create({
109 button: {
110 flex: 1,
111 },
112 textContainer: {
113 flex: 1,
114 justifyContent: 'center',
115 alignItems: 'center',
116 },
117 container: {
118 marginHorizontal: 10,
119 marginVertical: 5,
120 borderColor: '#e3e3e3',
121 borderWidth: 1,
122 flexDirection: 'row',
123 borderRadius: 3,
124 overflow: 'hidden',
125 backgroundColor: '#fff',
126 height: 40,
127 },
128 verticalContainer: {
129 flexDirection: 'column',
130 height: null,
131 },
132 verticalComponent: {
133 height: 40,
134 },
135 disabled: {
136 backgroundColor: 'transparent',
137 },
138});
139export { ButtonGroup };
140export default withTheme(ButtonGroup, 'ButtonGroup');