@render-props/choices
Version:
A state container which provides an interface for making selections from a group of choices. The `Choices` component itself is a context provider which can be used with the `Choice` and `ChoicesConsumer` components for deep-tree selections. It does not ha
44 lines (35 loc) • 1.16 kB
JavaScript
import _extends from '@babel/runtime/helpers/esm/extends'
import React from 'react'
import PropTypes from 'prop-types'
import ChoicesContext from './ChoicesContext'
class Choice extends React.Component {
constructor(props) {
super(props)
this.select = () => this.props.select(this.props.value)
this.deselect = () => this.props.deselect(this.props.value)
this.toggle = () => this.props.toggle(this.props.value)
this.delete = () => this.props.deleteChoice(this.props.value)
this.choiceContext = {
select: this.select,
deselect: this.deselect,
toggle: this.toggle,
delete: this.delete,
isSelected: this.props.isSelected(props.value),
}
}
render() {
this.choiceContext.isSelected = this.props.isSelected(this.props.value)
return this.props.children(this.choiceContext)
}
}
Choice.propTypes = {
children: PropTypes.func.isRequired,
value: PropTypes.any.isRequired,
}
export default function(props) {
return React.createElement(ChoicesContext.Consumer, null, function(
choicesContext
) {
return React.createElement(Choice, _extends({}, choicesContext, props))
})
}