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

Update SelectMultiple.js #40

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
57 changes: 16 additions & 41 deletions src/SelectMultiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { View, FlatList, Text, TouchableWithoutFeedback, Image } from 'react-nat
import styles from './SelectMultiple.styles'
import checkbox from '../images/icon-checkbox.png'
import checkboxChecked from '../images/icon-checkbox-checked.png'
import { mergeStyles } from './style'
import { mergeStyles } from './style';

const itemType = PropTypes.oneOfType([
PropTypes.string,
Expand All @@ -24,19 +24,17 @@ export default class SelectMultiple extends Component {
static propTypes = {
items: PropTypes.arrayOf(itemType).isRequired,
selectedItems: PropTypes.arrayOf(itemType),
maxSelect: PropTypes.number,

onSelectionsChange: PropTypes.func.isRequired,
keyExtractor: PropTypes.func,

checkboxSource: sourceType,
selectedCheckboxSource: sourceType,
renderLabel: PropTypes.func,
flatListProps: PropTypes.any,
style: styleType,
rowStyle: styleType,
checkboxStyle: styleType,
labelStyle: styleType,

selectedRowStyle: styleType,
selectedCheckboxStyle: styleType,
selectedLabelStyle: styleType
Expand All @@ -49,27 +47,11 @@ export default class SelectMultiple extends Component {
checkboxStyle: {},
checkboxCheckedStyle: {},
labelStyle: {},
maxSelect: null,
checkboxSource: checkbox,
selectedCheckboxSource: checkboxChecked,
renderLabel: null
}

constructor (props) {
super(props)
this.state = { dataSource: [] }
}

componentDidMount () {
const rows = this.getRowData(this.props)
this.setState({ dataSource: rows })
}

componentWillReceiveProps (nextProps) {
const rows = this.getRowData(nextProps)
this.setState({ dataSource: rows })
}

getRowData ({ items, selectedItems }) {
items = items.map(this.toLabelValueObject)
selectedItems = (selectedItems || []).map(this.toLabelValueObject)
Expand All @@ -83,7 +65,7 @@ export default class SelectMultiple extends Component {

onRowPress (row) {
const { label, value } = row
let { selectedItems, maxSelect } = this.props
let { selectedItems } = this.props

selectedItems = (selectedItems || []).map(this.toLabelValueObject)

Expand All @@ -92,11 +74,7 @@ export default class SelectMultiple extends Component {
if (index > -1) {
selectedItems = selectedItems.filter((selectedItem) => selectedItem.value !== value)
} else {
if (maxSelect != null && selectedItems.length >= maxSelect) {
return
} else {
selectedItems = selectedItems.concat({ label, value })
}
selectedItems = selectedItems.concat({ label, value })
}

this.props.onSelectionsChange(selectedItems, { label, value })
Expand All @@ -110,21 +88,7 @@ export default class SelectMultiple extends Component {
}
}

keyExtractor = (item, index) => index.toString()

render () {
const { dataSource } = this.state
const { style, flatListProps, keyExtractor } = this.props
return (
<FlatList
style={style}
keyExtractor={keyExtractor || this.keyExtractor}
data={dataSource}
renderItem={this.renderItemRow}
{...flatListProps}
/>
)
}
keyExtractor = (item, index) => index

renderLabel = (label, style, selected) => {
if (this.props.renderLabel) {
Expand Down Expand Up @@ -170,4 +134,15 @@ export default class SelectMultiple extends Component {
</TouchableWithoutFeedback>
)
}

render () {
const { style, keyExtractor } = this.props
const dataSource = this.getRowData(this.props)
return <FlatList
style={style}
keyExtractor={keyExtractor || this.keyExtractor}
data={dataSource}
renderItem={this.renderItemRow}
/>
}
}