@prosperitainova/dumbo-react-native
Version:
Dumbo for React Native Library
153 lines (150 loc) • 4.36 kB
JavaScript
;
import React from 'react';
import { StyleSheet, View, Pressable } from 'react-native';
import { getColor } from '../../styles/colors';
import { pressableFeedbackStyle, styleReferenceBreaker } from '../../helpers';
import { Text } from '../Text';
/** An item to pass to content switcher */
/** Props for ContentSwitcher component */
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
* ContentSwitcher component for rendering the content switcher
*
* {@link https://github.com/carbon-design-system/carbon-react-native/blob/main/example/src/Views/ContentSwitcher.tsx | Example code}
*/
export class ContentSwitcher extends React.Component {
state = {
currentIndex: 0
};
get styles() {
const basicStyle = {
padding: 16,
paddingTop: 14,
paddingBottom: 14,
flex: 1,
borderTopWidth: 1,
borderBottomWidth: 1,
position: 'relative'
};
return StyleSheet.create({
wrapper: {
flexDirection: 'row'
},
item: {
...basicStyle,
backgroundColor: 'transparent',
borderColor: getColor('borderInverse')
},
selectedItem: {
...basicStyle,
backgroundColor: getColor('layerSelectedInverse'),
borderColor: getColor('layerSelectedInverse')
},
divider: {
backgroundColor: getColor('borderSubtle01'),
width: 1,
height: 16,
position: 'absolute',
top: 16,
right: 0
}
});
}
changeItem = (item, index) => {
const {
onChange
} = this.props;
this.setState({
currentIndex: index
}, () => {
if (typeof onChange === 'function') {
onChange(index, item);
}
});
};
getSwitcher(item, index) {
const {
currentIndex
} = this.state;
const {
items
} = this.props;
const selected = index === currentIndex;
const finalItem = items.length === index + 1;
const finalStyle = styleReferenceBreaker(selected ? this.styles.selectedItem : this.styles.item);
const textStyle = {
color: selected ? getColor('textInverse') : getColor('textSecondary')
};
if (item.disabled) {
if (selected) {
finalStyle.backgroundColor = getColor('buttonDisabled');
}
finalStyle.borderColor = getColor('buttonDisabled');
textStyle.color = getColor('textDisabled');
}
if (index === 0) {
finalStyle.borderLeftWidth = 1;
finalStyle.borderTopLeftRadius = 4;
finalStyle.borderBottomLeftRadius = 4;
}
if (index === (items || []).length - 1) {
finalStyle.borderRightWidth = 1;
finalStyle.borderTopRightRadius = 4;
finalStyle.borderBottomRightRadius = 4;
}
const getStateStyle = state => {
return state.pressed ? {
backgroundColor: selected ? getColor('backgroundInverse') : getColor('backgroundActive')
} : undefined;
};
return /*#__PURE__*/_jsxs(Pressable, {
disabled: item.disabled,
onPress: () => this.changeItem(item, index),
style: state => pressableFeedbackStyle(state, finalStyle, getStateStyle),
accessibilityLabel: item.text,
accessibilityRole: "menuitem",
children: [!finalItem && !selected && /*#__PURE__*/_jsx(View, {
style: this.styles.divider
}), /*#__PURE__*/_jsx(Text, {
type: item.textType || 'body-compact-01',
style: textStyle,
breakMode: "tail",
text: item.text
})]
}, index);
}
componentDidUpdate(previousProps) {
const {
selectedIndex
} = this.props;
if (typeof selectedIndex === 'number' && previousProps.selectedIndex !== selectedIndex) {
this.setState({
currentIndex: selectedIndex
});
}
}
componentDidMount() {
const {
selectedIndex
} = this.props;
if (typeof selectedIndex === 'number') {
this.setState({
currentIndex: selectedIndex
});
}
}
render() {
const {
items,
componentProps,
style
} = this.props;
return /*#__PURE__*/_jsx(View, {
style: styleReferenceBreaker(this.styles.wrapper, style),
accessibilityRole: "menu",
...(componentProps || {}),
children: (items || []).map((item, index) => this.getSwitcher(item, index))
});
}
}
//# sourceMappingURL=index.js.map