@prosperitainova/dumbo-react-native
Version:
Dumbo for React Native Library
146 lines (144 loc) • 3.69 kB
JavaScript
"use strict";
import React from 'react';
import { StyleSheet, View, Pressable } from 'react-native';
import { getColor } from '../../styles/colors';
import { createIcon, pressableFeedbackStyle, styleReferenceBreaker } from '../../helpers';
import ChevronDownIcon from '@carbon/icons/es/chevron--down/20';
import ChevronUpIcon from '@carbon/icons/es/chevron--up/20';
import { Text } from '../Text';
/** Props for Accordion component */
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
* Accordion component for showing the accept legal terms flow
*
* {@link https://github.com/carbon-design-system/carbon-react-native/blob/main/example/src/Views/Accordion.tsx | Example code}
*/
export class Accordion extends React.Component {
state = {
open: false
};
get itemColor() {
const {
disabled
} = this.props;
return disabled ? getColor('textDisabled') : getColor('textPrimary');
}
get styles() {
return StyleSheet.create({
wrapper: {
borderBottomColor: getColor('borderSubtle00'),
borderBottomWidth: 1
},
content: {
padding: 16,
paddingTop: 8,
paddingRight: 64,
paddingBottom: 24
},
action: {
position: 'relative',
minHeight: 48,
padding: 13,
paddingLeft: 16,
paddingRight: 50
},
iconStyle: {
position: 'absolute',
top: 12,
right: 12
},
textItem: {
flex: 1,
color: this.itemColor
}
});
}
getStateStyle = state => {
return state.pressed ? {
backgroundColor: getColor('layerActive01')
} : undefined;
};
get accordionIcon() {
const {
open
} = this.state;
return /*#__PURE__*/_jsx(View, {
style: this.styles.iconStyle,
children: createIcon(open ? ChevronUpIcon : ChevronDownIcon, 22, 22, this.itemColor)
});
}
toggleDropdown = event => {
const {
open
} = this.state;
const {
onPress
} = this.props;
this.setState({
open: !open
}, () => {
if (typeof onPress === 'function') {
onPress(!open, event);
}
});
};
componentDidUpdate(previosuProps) {
const {
open
} = this.props;
if (open !== previosuProps.open && typeof open === 'boolean') {
this.setState({
open: open
});
}
}
componentDidMount() {
const {
open
} = this.props;
if (open) {
this.setState({
open: true
});
}
}
render() {
const {
componentProps,
style,
disabled,
title,
children,
firstAccordion,
textBreakMode
} = this.props;
const {
open
} = this.state;
const finalStyle = styleReferenceBreaker(this.styles.wrapper);
if (firstAccordion) {
finalStyle.borderTopColor = getColor('borderSubtle00');
finalStyle.borderTopWidth = 1;
}
return /*#__PURE__*/_jsxs(View, {
style: styleReferenceBreaker(finalStyle, style),
...(componentProps || {}),
children: [/*#__PURE__*/_jsxs(Pressable, {
style: state => pressableFeedbackStyle(state, this.styles.action, this.getStateStyle),
accessibilityLabel: title,
accessibilityRole: "togglebutton",
onPress: this.toggleDropdown,
disabled: disabled,
children: [/*#__PURE__*/_jsx(Text, {
style: this.styles.textItem,
text: title,
breakMode: textBreakMode
}), this.accordionIcon]
}), open && /*#__PURE__*/_jsx(View, {
style: this.styles.content,
children: children
})]
});
}
}
//# sourceMappingURL=index.js.map