Files
customgroupbox/src/CustomGroupbox.tsx

89 lines
2.7 KiB
TypeScript
Raw Normal View History

2020-10-28 23:30:50 +01:00
import { Component, ReactNode, createElement } from "react";
import { TextStyle, ViewStyle, View, TouchableOpacity } from "react-native";
import { DynamicValue, NativeIcon, ValueStatus } from "mendix";
import { Icon } from 'mendix/components/native/Icon';
import { Style, mergeNativeStyles } from '@mendix/pluggable-widgets-tools';
import { CustomGroupboxProps } from '../typings/CustomGroupboxProps';
export interface CustomStyle extends Style {
2020-12-21 09:12:08 +01:00
headerContainer: ViewStyle;
2020-10-28 23:30:50 +01:00
label: TextStyle;
innerContainer: ViewStyle;
selectedContainer: ViewStyle;
divider: ViewStyle;
icon: ViewStyle;
2020-12-21 09:12:08 +01:00
groupboxParent: ViewStyle
2020-10-28 23:30:50 +01:00
}
const defaultStyle: CustomStyle = {
2020-12-21 09:12:08 +01:00
headerContainer: {},
2020-10-28 23:30:50 +01:00
label: {},
innerContainer: {},
selectedContainer: {},
divider: {
backgroundColor: '#A2A2A2',
height: 1,
},
icon: {
alignSelf: 'flex-end'
2020-12-21 09:12:08 +01:00
},
groupboxParent: {}
2020-10-28 23:30:50 +01:00
};
interface State {
showContent: boolean;
}
const defaultCollapseIconGlyph = "glyphicon-minus";
const defaultExpandIconGlyph = "glyphicon-plus";
export class CustomGroupbox extends Component<CustomGroupboxProps<CustomStyle>, State> {
constructor(props: CustomGroupboxProps<CustomStyle>){
super(props)
this.toggleContent = this.toggleContent.bind(this)
this.state = { showContent: (this.props.startCollapsed ? false : true ), }
2020-10-28 23:30:50 +01:00
}
private readonly styles = mergeNativeStyles(defaultStyle, this.props.style);
2020-10-28 23:30:50 +01:00
render(): ReactNode {
const icons = {
collapseIcon: this.renderIcon(defaultCollapseIconGlyph, this.props.collapseIcon),
expandIcon: this.renderIcon(defaultExpandIconGlyph, this.props.expandIcon),
};
2020-10-28 23:30:50 +01:00
return (
2020-12-21 09:12:08 +01:00
<View style={this.styles.groupboxParent}>
<TouchableOpacity onPress={this.toggleContent}>
<View style={this.styles.headerContainer}>
<View>{this.props.header}</View>
<View style={this.styles.icon}>{this.state.showContent ? icons.collapseIcon : icons.expandIcon}</View>
</View>
</TouchableOpacity>
2020-10-28 23:30:50 +01:00
2020-12-21 09:12:08 +01:00
<View style={this.state.showContent ? this.styles.selectedContainer : null}>
2020-10-28 23:30:50 +01:00
{this.state.showContent && <View style={this.styles.innerContainer}>{this.props.content}</View>}
{this.props.showDivider && <View style={this.styles.divider}></View>}
</View>
2020-12-21 09:12:08 +01:00
</View>
2020-10-28 23:30:50 +01:00
);
}
toggleContent() {
2020-12-21 09:12:08 +01:00
this.setState({ showContent: !this.state.showContent })
2020-10-28 23:30:50 +01:00
}
private renderIcon = (glyph: string, toBeRenderedIcon?: DynamicValue<NativeIcon>) => {
const nativeIcon: NativeIcon =
toBeRenderedIcon && toBeRenderedIcon.status === ValueStatus.Available
? toBeRenderedIcon.value
: { type: "glyph", iconClass: glyph };
return <Icon icon={nativeIcon} />;
};
}