|
| 1 | +import React, { useCallback } from 'react'; |
| 2 | +import { FlatList } from 'react-native'; |
| 3 | +import _ from 'lodash'; |
| 4 | +import PropTypes from 'prop-types'; |
| 5 | +import { connectStyle } from '@shoutem/theme'; |
| 6 | +import { View } from '../View'; |
| 7 | +import Category from './Category'; |
| 8 | +import { categoryShape } from './shapes'; |
| 9 | + |
| 10 | +export function CategoryPicker({ |
| 11 | + categories, |
| 12 | + onCategorySelected, |
| 13 | + style, |
| 14 | + selectedCategory, |
| 15 | +}) { |
| 16 | + const renderItem = useCallback( |
| 17 | + ({ item: category }) => ( |
| 18 | + <Category |
| 19 | + category={category} |
| 20 | + key={category.id} |
| 21 | + onPress={onCategorySelected} |
| 22 | + isSelected={selectedCategory.id === category.id} |
| 23 | + /> |
| 24 | + ), |
| 25 | + [selectedCategory.id, onCategorySelected], |
| 26 | + ); |
| 27 | + |
| 28 | + if (_.size(categories) < 2) { |
| 29 | + return null; |
| 30 | + } |
| 31 | + |
| 32 | + return ( |
| 33 | + <View style={style.container}> |
| 34 | + <FlatList |
| 35 | + horizontal |
| 36 | + scrollToOverflowEnabled |
| 37 | + contentContainerStyle={style.listContainer} |
| 38 | + showsHorizontalScrollIndicator={false} |
| 39 | + data={categories} |
| 40 | + renderItem={renderItem} |
| 41 | + /> |
| 42 | + </View> |
| 43 | + ); |
| 44 | +} |
| 45 | + |
| 46 | +CategoryPicker.propTypes = { |
| 47 | + categories: PropTypes.arrayOf(categoryShape), |
| 48 | + selectedCategory: categoryShape, |
| 49 | + style: PropTypes.object, |
| 50 | + onCategorySelected: PropTypes.func, |
| 51 | +}; |
| 52 | + |
| 53 | +CategoryPicker.defaultProps = { |
| 54 | + categories: [], |
| 55 | + style: {}, |
| 56 | + selectedCategory: undefined, |
| 57 | + onCategorySelected: undefined, |
| 58 | +}; |
| 59 | + |
| 60 | +export default React.memo( |
| 61 | + connectStyle('shoutem.ui.CategoryPicker')(CategoryPicker), |
| 62 | +); |
0 commit comments