UNPKG

@gechiui/components

Version:
99 lines (85 loc) 2.16 kB
/** * External dependencies */ import { View } from 'react-native'; /** * GeChiUI dependencies */ import { __ } from '@gechiui/i18n'; import { Component, Fragment } from '@gechiui/element'; import { usePreferredColorSchemeStyle } from '@gechiui/compose'; import { PanelBody, TextControl } from '@gechiui/components'; /** * Internal dependencies */ import BottomSheet from '../bottom-sheet'; import styles from './styles.scss'; function Separator() { const separatorStyle = usePreferredColorSchemeStyle( styles.separator, styles.separatorDark ); return <View style={ separatorStyle } />; } export default class Picker extends Component { constructor() { super( ...arguments ); this.onClose = this.onClose.bind( this ); this.onCellPress = this.onCellPress.bind( this ); this.state = { isVisible: false, }; } presentPicker() { this.setState( { isVisible: true } ); } onClose() { this.setState( { isVisible: false } ); } onCellPress( value ) { const { onChange } = this.props; onChange( value ); this.onClose(); } getOptions() { const { options, leftAlign } = this.props; return options.map( ( option ) => ( <Fragment key={ `${ option.label }-${ option.value }` }> { options.length > 1 && option.separated && <Separator /> } <BottomSheet.Cell icon={ option.icon } leftAlign={ leftAlign } label={ option.label } separatorType={ 'none' } onPress={ () => this.onCellPress( option.value ) } disabled={ option.disabled } style={ option.disabled && styles.disabled } /> </Fragment> ) ); } render() { const { hideCancelButton, title, testID } = this.props; const { isVisible } = this.state; return ( <BottomSheet isVisible={ isVisible } onClose={ this.onClose } style={ { paddingBottom: 20 } } hideHeader testID={ testID } > <PanelBody title={ title } style={ styles.panelBody }> { this.getOptions() } { ! hideCancelButton && ( <TextControl label={ __( '取消' ) } onPress={ this.onClose } separatorType={ 'none' } /> ) } </PanelBody> </BottomSheet> ); } }