Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: display more level subItem #279

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
229 changes: 180 additions & 49 deletions lib/components/RowSubItem.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import React, { Component } from 'react'
import isEqual from 'lodash.isequal'
import { View, TouchableOpacity, Text } from 'react-native'
import {
View,
TouchableOpacity,
Text,
FlatList,
LayoutAnimation
} from 'react-native'
import ItemIcon from './ItemIcon'
import { callIfFunction } from '../helpers'

class RowSubItem extends Component {
shouldComponentUpdate(nextProps) {
constructor(props) {
super(props)

this.state = {
showSubCategories: false,
subToggled: null
}
}

shouldComponentUpdate(nextProps, nextState) {
if (nextProps.selectedItems !== this.props.selectedItems) {
if (
this.props.selectedItems.includes(
Expand All @@ -27,6 +42,24 @@ class RowSubItem extends Component {
) {
return true
}

if (
this.props.subItem[this.props.subKey] &&
this.props.subItem[this.props.subKey].findIndex((el) =>
nextProps.selectedItems.includes(el[this.props.uniqueKey])
) !== -1
) {
return true
}
if (
this.props.subItem[this.props.subKey] &&
this.props.subItem[this.props.subKey].findIndex((el) =>
this.props.selectedItems.includes(el[this.props.uniqueKey])
) !== -1
) {
return true
}

if (this.props.highlightChildren || this.props.selectChildren) {
if (
this.props.highlightedChildren.includes(
Expand All @@ -50,6 +83,9 @@ class RowSubItem extends Component {
if (!isEqual(this.props.mergedColors, nextProps.mergedColors)) {
return true
}
if (this.state.showSubCategories !== nextState.showSubCategories) {
return true
}
return false
}

Expand Down Expand Up @@ -99,6 +135,35 @@ class RowSubItem extends Component {
) : null
}

_showSubCategoryDropDown = () => {
const { showDropDowns, searchTerm } = this.props
if (searchTerm.length) {
return true
}
if (showDropDowns) {
return this.state.showSubCategories
}

return true
}

_renderSubItemFlatList = ({ item }) => (
<RowSubItem
toggleSubItem={this._toggleSubItem}
highlightedChildren={this.props.highlightedChildren}
{...this.props}
subItem={item}
/>
)

_toggleDropDown = () => {
const { customLayoutAnimation, animateDropDowns } = this.props
const animation =
customLayoutAnimation || LayoutAnimation.Presets.easeInEaseOut
animateDropDowns && LayoutAnimation.configureNext(animation)
this.setState({ showSubCategories: !this.state.showSubCategories })
}

render() {
const {
mergedStyles,
Expand All @@ -112,67 +177,133 @@ class RowSubItem extends Component {
itemNumberOfLines,
displayKey,
iconKey,
iconRenderer: Icon
iconRenderer: Icon,
subKey,
subItemsFlatListProps,
selectedItems,
showDropDowns,
dropDownToggleIconDownComponent,
dropDownToggleIconUpComponent,
icons
} = this.props
const hasDropDown =
subItem[subKey] && subItem[subKey].length > 0 && showDropDowns

const highlightChild =
!selectChildren && highlightedChildren.includes(subItem[uniqueKey])
const itemSelected = this._itemSelected()

return (
<View
key={subItem[uniqueKey]}
style={{
flexDirection: 'row',
flex: 1,
backgroundColor: mergedColors.subItemBackground
}}
>
<TouchableOpacity
disabled={highlightChild || subItem.disabled}
onPress={this._toggleItem}
style={[
{
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'flex-start',
paddingVertical: 6
},
mergedStyles.subItem,
(itemSelected || highlightChild) && mergedStyles.selectedItem,
(itemSelected || highlightChild) && mergedStyles.selectedSubItem
]}
<View>
<View
key={subItem[uniqueKey]}
style={{
flexDirection: 'row',
flex: 1,
backgroundColor: mergedColors.subItemBackground
}}
>
{selectedIconOnLeft && this._renderSelectedIcon()}

{iconKey && subItem[iconKey] && (
<ItemIcon
iconRenderer={Icon}
iconKey={iconKey}
icon={subItem[iconKey]}
style={mergedStyles.itemIconStyle}
/>
)}
<Text
numberOfLines={itemNumberOfLines}
<TouchableOpacity
disabled={highlightChild || subItem.disabled}
onPress={this._toggleItem}
style={[
{
flex: 1,
color: subItem.disabled
? mergedColors.disabled
: mergedColors.subText
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'flex-start',
paddingVertical: 6
},
subItemFontFamily,
mergedStyles.subItemText,
(itemSelected || highlightChild) &&
mergedStyles.selectedSubItemText
mergedStyles.subItem,
(itemSelected || highlightChild) && mergedStyles.selectedItem,
(itemSelected || highlightChild) && mergedStyles.selectedSubItem
]}
>
{subItem[displayKey]}
</Text>
{!selectedIconOnLeft && this._renderSelectedIcon()}
</TouchableOpacity>
{selectedIconOnLeft && this._renderSelectedIcon()}

{iconKey && subItem[iconKey] && (
<ItemIcon
iconRenderer={Icon}
iconKey={iconKey}
icon={subItem[iconKey]}
style={mergedStyles.itemIconStyle}
/>
)}
<Text
numberOfLines={itemNumberOfLines}
style={[
{
flex: 1,
color: subItem.disabled
? mergedColors.disabled
: mergedColors.subText
},
subItemFontFamily,
mergedStyles.subItemText,
(itemSelected || highlightChild) &&
mergedStyles.selectedSubItemText
]}
>
{subItem[displayKey]}
</Text>
{!selectedIconOnLeft && this._renderSelectedIcon()}
</TouchableOpacity>
{hasDropDown && (
<TouchableOpacity
style={[
{
alignItems: 'flex-end',
justifyContent: 'center',
paddingHorizontal: 10,
backgroundColor: 'transparent'
},
mergedStyles.toggleIcon
]}
onPress={this._toggleDropDown}
>
{this._showSubCategoryDropDown() ? (
<View>
{callIfFunction(dropDownToggleIconUpComponent) || (
<Icon
style={{
color: mergedColors.primary
}}
{...icons.arrowUp}
/>
)}
</View>
) : (
<View>
{callIfFunction(dropDownToggleIconDownComponent) || (
<Icon
style={{
color: mergedColors.primary
}}
{...icons.arrowDown}
/>
)}
</View>
)}
</TouchableOpacity>
)}
</View>
{subItem[subKey] && this._showSubCategoryDropDown() && (
<View
style={{
paddingLeft: 8
}}
>
<FlatList
keyExtractor={(i) => `${i[uniqueKey]}`}
data={subItem[subKey]}
extraData={selectedItems}
ItemSeparatorComponent={this._renderSubSeparator}
renderItem={this._renderSubItemFlatList}
initialNumToRender={20}
{...subItemsFlatListProps}
/>
</View>
)}
</View>
)
}
Expand Down