1 | var __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 | };
|
12 | import React from 'react';
|
13 | import { View, TouchableNativeFeedback, TouchableOpacity, Platform, StyleSheet, } from 'react-native';
|
14 | import { withTheme } from '../config';
|
15 | import { normalizeText, color } from '../helpers';
|
16 | import Text from '../text/Text';
|
17 | const 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) ||
|
47 | ((_a = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _a === void 0 ? void 0 : _a.grey4),
|
48 | }
|
49 | : {
|
50 | borderRightWidth: innerBorderWidth,
|
51 | borderRightColor: (innerBorderStyle && innerBorderStyle.color) ||
|
52 | ((_b = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _b === void 0 ? void 0 : _b.grey4),
|
53 | }),
|
54 | buttonContainerStyle,
|
55 | ])}>
|
56 | <Component testID="buttonGroupItem" activeOpacity={activeOpacity} setOpacityTo={setOpacityTo} onHideUnderlay={onHideUnderlay} onShowUnderlay={onShowUnderlay} underlayColor={underlayColor} disabled={isDisabled} onPress={() => {
|
57 | if (selectMultiple) {
|
58 | if (selectedIndexes.includes(i)) {
|
59 | onPress(selectedIndexes.filter((index) => index !== i));
|
60 | }
|
61 | else {
|
62 | onPress([...selectedIndexes, i]);
|
63 | }
|
64 | }
|
65 | else {
|
66 | onPress(i);
|
67 | }
|
68 | }} style={styles.button}>
|
69 | <View style={StyleSheet.flatten([
|
70 | styles.textContainer,
|
71 | buttonStyle && buttonStyle,
|
72 | isSelected && {
|
73 | backgroundColor: (_c = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _c === void 0 ? void 0 : _c.primary,
|
74 | },
|
75 | isSelected && selectedButtonStyle && selectedButtonStyle,
|
76 | isDisabled && styles.disabled,
|
77 | isDisabled && disabledStyle,
|
78 | isDisabled &&
|
79 | isSelected && {
|
80 | backgroundColor: (_d = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _d === void 0 ? void 0 : _d.disabled,
|
81 | },
|
82 | isDisabled && isSelected && disabledSelectedStyle,
|
83 | ])}>
|
84 | {button.element ? (<button.element />) : (<Text testID="buttonGroupItemText" style={StyleSheet.flatten([
|
85 | 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({
|
86 | android: {},
|
87 | default: {
|
88 | fontWeight: '500',
|
89 | },
|
90 | })),
|
91 | textStyle && textStyle,
|
92 | isSelected && { color: '#fff' },
|
93 | isSelected && selectedTextStyle,
|
94 | isDisabled && {
|
95 | color: color((_f = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _f === void 0 ? void 0 : _f.disabled)
|
96 | .darken(0.3)
|
97 | .toString(),
|
98 | },
|
99 | isDisabled && disabledTextStyle,
|
100 | isDisabled && isSelected && disabledSelectedTextStyle,
|
101 | ])}>
|
102 | {button}
|
103 | </Text>)}
|
104 | </View>
|
105 | </Component>
|
106 | </View>);
|
107 | })}
|
108 | </View>);
|
109 | };
|
110 | const styles = StyleSheet.create({
|
111 | button: {
|
112 | flex: 1,
|
113 | },
|
114 | textContainer: {
|
115 | flex: 1,
|
116 | justifyContent: 'center',
|
117 | alignItems: 'center',
|
118 | },
|
119 | container: {
|
120 | marginHorizontal: 10,
|
121 | marginVertical: 5,
|
122 | borderColor: '#e3e3e3',
|
123 | borderWidth: 1,
|
124 | flexDirection: 'row',
|
125 | borderRadius: 3,
|
126 | overflow: 'hidden',
|
127 | backgroundColor: '#fff',
|
128 | height: 40,
|
129 | },
|
130 | verticalContainer: {
|
131 | flexDirection: 'column',
|
132 | height: null,
|
133 | },
|
134 | verticalComponent: {
|
135 | height: 40,
|
136 | },
|
137 | disabled: {
|
138 | backgroundColor: 'transparent',
|
139 | },
|
140 | });
|
141 | export { ButtonGroup };
|
142 | export default withTheme(ButtonGroup, 'ButtonGroup');
|