|
| 1 | +// @flow |
| 2 | +import * as React from 'react' |
| 3 | +import {StyleSheet, Text, Platform} from 'react-native' |
| 4 | +import Icon from 'react-native-vector-icons/Ionicons' |
| 5 | +import type {FilterType} from './types' |
| 6 | +import {FilterPopover} from './filter-popover' |
| 7 | +import * as c from '../colors' |
| 8 | +import {Touchable, type TouchableUnion} from '../touchable' |
| 9 | + |
| 10 | +const buttonStyles = StyleSheet.create({ |
| 11 | + button: { |
| 12 | + flexDirection: 'row', |
| 13 | + alignItems: 'center', |
| 14 | + marginRight: 10, |
| 15 | + paddingHorizontal: 8, |
| 16 | + paddingVertical: 5, |
| 17 | + marginVertical: 8, |
| 18 | + borderWidth: 1, |
| 19 | + borderRadius: 2, |
| 20 | + }, |
| 21 | + activeButton: { |
| 22 | + backgroundColor: c.toolbarButtonBackground, |
| 23 | + borderColor: c.toolbarButtonBackground, |
| 24 | + }, |
| 25 | + inactiveButton: { |
| 26 | + borderColor: c.iosDisabledText, |
| 27 | + }, |
| 28 | + activeText: { |
| 29 | + color: c.toolbarButtonForeground, |
| 30 | + }, |
| 31 | + inactiveText: { |
| 32 | + color: c.iosDisabledText, |
| 33 | + }, |
| 34 | + text: { |
| 35 | + fontSize: 16, |
| 36 | + }, |
| 37 | + textWithIcon: { |
| 38 | + paddingRight: 8, |
| 39 | + }, |
| 40 | +}) |
| 41 | + |
| 42 | +type Props = { |
| 43 | + filter: FilterType, |
| 44 | + isActive: boolean, |
| 45 | + onPopoverDismiss: (filter: FilterType) => any, |
| 46 | + style?: any, |
| 47 | + title: string, |
| 48 | +} |
| 49 | + |
| 50 | +type State = { |
| 51 | + popoverVisible: boolean, |
| 52 | +} |
| 53 | + |
| 54 | +export class FilterToolbarButton extends React.PureComponent<Props, State> { |
| 55 | + state = { |
| 56 | + popoverVisible: false, |
| 57 | + } |
| 58 | + |
| 59 | + touchable: ?TouchableUnion = null |
| 60 | + |
| 61 | + openPopover = () => { |
| 62 | + this.setState(() => ({popoverVisible: true})) |
| 63 | + } |
| 64 | + |
| 65 | + onClosePopover = (filter: FilterType) => { |
| 66 | + this.props.onPopoverDismiss(filter) |
| 67 | + this.setState(() => ({popoverVisible: false})) |
| 68 | + } |
| 69 | + |
| 70 | + render() { |
| 71 | + const {filter, isActive, style, title} = this.props |
| 72 | + const {popoverVisible} = this.state |
| 73 | + const icon = Platform.select({ |
| 74 | + ios: 'ios-arrow-down', |
| 75 | + android: 'md-arrow-dropdown', |
| 76 | + }) |
| 77 | + |
| 78 | + let activeButtonStyle = isActive |
| 79 | + ? buttonStyles.activeButton |
| 80 | + : buttonStyles.inactiveButton |
| 81 | + let activeContentStyle = isActive |
| 82 | + ? buttonStyles.activeText |
| 83 | + : buttonStyles.inactiveText |
| 84 | + |
| 85 | + let textWithIconStyle = icon ? buttonStyles.textWithIcon : null |
| 86 | + let activeTextStyle = { |
| 87 | + fontWeight: isActive && Platform.OS === 'android' ? 'bold' : 'normal', |
| 88 | + } |
| 89 | + |
| 90 | + if (filter.type === 'list') { |
| 91 | + if (!filter.spec.options.length) { |
| 92 | + return null |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + const buttonTextStyle = [ |
| 97 | + activeContentStyle, |
| 98 | + textWithIconStyle, |
| 99 | + activeTextStyle, |
| 100 | + buttonStyles.text, |
| 101 | + ] |
| 102 | + |
| 103 | + return ( |
| 104 | + <React.Fragment> |
| 105 | + <Touchable |
| 106 | + getRef={ref => (this.touchable = ref)} |
| 107 | + highlight={false} |
| 108 | + onPress={this.openPopover} |
| 109 | + style={[buttonStyles.button, activeButtonStyle, style]} |
| 110 | + > |
| 111 | + <Text style={buttonTextStyle}>{title}</Text> |
| 112 | + <Icon name={icon} size={18} style={activeContentStyle} /> |
| 113 | + </Touchable> |
| 114 | + <FilterPopover |
| 115 | + anchor={this.touchable} |
| 116 | + filter={filter} |
| 117 | + onClosePopover={this.onClosePopover} |
| 118 | + visible={popoverVisible} |
| 119 | + /> |
| 120 | + </React.Fragment> |
| 121 | + ) |
| 122 | + } |
| 123 | +} |
0 commit comments