Skip to content

Commit 346b65d

Browse files
authored
Merge pull request #2754 from StoDevX/hawken-filter-popovers
Refactoring of Filter Popover
2 parents e8b1d44 + e3a13de commit 346b65d

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

source/views/components/filter/filter-popover.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//@flow
22
import * as React from 'react'
3-
import {StyleSheet, View, TouchableOpacity} from 'react-native'
43
import Popover from 'react-native-popover-view'
54
import {FilterSection} from './section'
65
import type {FilterType} from './types'
6+
import {type TouchableUnion} from '../touchable'
77
import * as c from '../colors'
88

99
type Props = {
10-
anchor: React.Ref<typeof TouchableOpacity>,
10+
anchor: ?React.Ref<TouchableUnion>,
1111
filter: FilterType,
1212
onClosePopover: (filter: FilterType) => any,
1313
visible: boolean,
@@ -43,20 +43,17 @@ export class FilterPopover extends React.PureComponent<Props, State> {
4343
isVisible={visible}
4444
onClose={() => onClosePopover(filter)}
4545
placement="bottom"
46+
popoverStyle={popoverContainer}
4647
>
47-
<View style={styles.popoverContainer}>
48-
<FilterSection filter={filter} onChange={this.onFilterChanged} />
49-
</View>
48+
<FilterSection filter={filter} onChange={this.onFilterChanged} />
5049
</Popover>
5150
)
5251
}
5352
}
5453

55-
const styles = StyleSheet.create({
56-
popoverContainer: {
57-
minWidth: 200,
58-
maxWidth: 300,
59-
},
60-
})
54+
const popoverContainer = {
55+
minWidth: 200,
56+
maxWidth: 300,
57+
}
6158

6259
const arrowStyle = {backgroundColor: c.iosLightBackground}

source/views/components/filter/filter-toolbar-button.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// @flow
22
import * as React from 'react'
3-
import {StyleSheet, Text, Platform, TouchableOpacity} from 'react-native'
3+
import {StyleSheet, Text, Platform} from 'react-native'
44
import Icon from 'react-native-vector-icons/Ionicons'
55
import type {FilterType} from './types'
66
import {FilterPopover} from './filter-popover'
77
import * as c from '../colors'
8+
import {Touchable, type TouchableUnion} from '../touchable'
89

910
const buttonStyles = StyleSheet.create({
1011
button: {
@@ -51,12 +52,12 @@ type State = {
5152
}
5253

5354
export class FilterToolbarButton extends React.PureComponent<Props, State> {
54-
touchable: TouchableOpacity
55-
5655
state = {
5756
popoverVisible: false,
5857
}
5958

59+
touchable: ?TouchableUnion = null
60+
6061
openPopover = () => {
6162
this.setState(() => ({popoverVisible: true}))
6263
}
@@ -101,14 +102,15 @@ export class FilterToolbarButton extends React.PureComponent<Props, State> {
101102

102103
return (
103104
<React.Fragment>
104-
<TouchableOpacity
105-
ref={ref => (this.touchable = ref)}
105+
<Touchable
106+
getRef={ref => (this.touchable = ref)}
107+
highlight={false}
106108
onPress={this.openPopover}
107109
style={[buttonStyles.button, activeButtonStyle, style]}
108110
>
109111
<Text style={buttonTextStyle}>{title}</Text>
110112
<Icon name={icon} size={18} style={activeContentStyle} />
111-
</TouchableOpacity>
113+
</Touchable>
112114
<FilterPopover
113115
anchor={this.touchable}
114116
filter={filter}

source/views/components/filter/filter-toolbar.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,33 @@ export function FilterToolbar({filters, onPopoverDismiss}: Props) {
4141
onPopoverDismiss(newFilter)
4242
}
4343

44-
const filterToggles = filters.map((filter, index) => (
44+
const filterToggles = filters.map(filter => (
4545
<FilterToolbarButton
4646
key={filter.spec.title}
4747
filter={filter}
4848
isActive={filter.enabled}
4949
onPopoverDismiss={onPopoverDismiss}
50-
style={index === 0 ? styles.first : null}
5150
title={filter.spec.title}
5251
/>
5352
))
5453

55-
const allButtons = filters.filter(f => f.enabled).map((filter, i) => {
54+
const allButtons = filters.filter(f => f.enabled).map(filter => {
5655
if (filter.type === 'toggle') {
5756
return (
5857
<ActiveFilterButton
5958
key={filter.spec.title}
6059
filter={filter}
6160
label={filter.spec.label}
6261
onRemove={filter => updateFilter(filter)}
63-
style={i === 0 ? styles.first : null}
6462
/>
6563
)
6664
} else if (filter.type === 'list') {
67-
return filter.spec.selected.map((selected, j) => (
65+
return filter.spec.selected.map(selected => (
6866
<ActiveFilterButton
6967
key={selected.title}
7068
filter={filter}
7169
label={selected.label || selected.title.toString()}
7270
onRemove={filter => updateFilter(filter, selected)}
73-
style={i === 0 && j === 0 ? styles.first : null}
7471
/>
7572
))
7673
}
@@ -81,11 +78,19 @@ export function FilterToolbar({filters, onPopoverDismiss}: Props) {
8178

8279
return (
8380
<React.Fragment>
84-
<ScrollView horizontal={true} showsHorizontalScrollIndicator={false}>
81+
<ScrollView
82+
contentContainerStyle={styles.scroller}
83+
horizontal={true}
84+
showsHorizontalScrollIndicator={false}
85+
>
8586
<Toolbar>{filterToggles}</Toolbar>
8687
</ScrollView>
8788
{anyFiltersEnabled && (
88-
<ScrollView horizontal={true} showsHorizontalScrollIndicator={false}>
89+
<ScrollView
90+
contentContainerStyle={styles.scroller}
91+
horizontal={true}
92+
showsHorizontalScrollIndicator={false}
93+
>
8994
{activeFilterButtons}
9095
</ScrollView>
9196
)}
@@ -94,7 +99,7 @@ export function FilterToolbar({filters, onPopoverDismiss}: Props) {
9499
}
95100

96101
const styles = StyleSheet.create({
97-
first: {
98-
marginLeft: 10,
102+
scroller: {
103+
paddingLeft: 10,
99104
},
100105
})

source/views/components/touchable.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import {
99
View,
1010
} from 'react-native'
1111

12+
export type TouchableUnion =
13+
| typeof TouchableHighlight
14+
| typeof TouchableOpacity
15+
| typeof TouchableNativeFeedback
16+
1217
type Props = {|
1318
accessibilityComponentType?: string,
1419
accessibilityLabel?: string,
@@ -17,6 +22,7 @@ type Props = {|
1722
borderless?: boolean,
1823
children?: React.Node,
1924
containerStyle?: any,
25+
getRef?: () => TouchableUnion,
2026
highlight?: boolean,
2127
onPress?: () => any,
2228
style?: any,
@@ -27,6 +33,7 @@ export const Touchable = ({
2733
borderless = false,
2834
children,
2935
containerStyle,
36+
getRef,
3037
highlight = true,
3138
onPress = () => {},
3239
style,
@@ -45,6 +52,7 @@ export const Touchable = ({
4552

4653
return (
4754
<Component
55+
ref={getRef}
4856
onPress={onPress}
4957
{...innerProps}
5058
style={containerStyle}
@@ -64,6 +72,7 @@ export const Touchable = ({
6472

6573
return (
6674
<TouchableNativeFeedback
75+
ref={getRef}
6776
background={background}
6877
onPress={onPress}
6978
style={containerStyle}

0 commit comments

Comments
 (0)