0.1.19 • Published 7 years ago
@render-props/choices v0.1.19
Choices
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 have to be used with these components, however.
Installation
yarn add @render-props/choices or npm i @render-props/choices
Contents
Choices- This main component which must be used
Choice- This is an optional convenience component for deep-tree selections. It
is a context consumer of
Choices. It provides the following props to its child component:{select, deselect, toggle, isSelected}
- This is an optional convenience component for deep-tree selections. It
is a context consumer of
ChoicesConsumer- This is the Consumer for the
ChoicesProvider which provides an object as its value with this shape:{selections, choices, select, deselect, toggle, setSelections, clearSelections, isSelected, addChoice, deleteChoice, setChoices, clearChoices, isChoice}
- This is the Consumer for the
Usage
import Choices, {Choice} from '@render-props/choices'
const FavoritePets = props => (
<Choices
initialChoices={new Set(['cat', 'dog', 'turtle'])}
initialSelections={new Set(['cat'])}
minChoices={1}
minSelections={1}
maxSelections={2}
onBoundMaxSelections={
function ({selections, select, deselect}) {
selections = Array.from(selections)
deselect(selections.shift())
select(selections.pop())
}
}
>
{PetsControl}
</Choices>
)
const PetsControl = ({
addChoice,
deleteChoice,
setChoices,
clearChoices,
isChoice,
select,
deselect,
setSelections,
clearSelections,
isSelected,
selections,
choices
}) => (
<div style={{borderWidth: 1}}>
<span>
Number of favorites: {selections.size}
</span>
{Array.from(choices).map(pet => (
<Choice key={pet} value={pet}>
{function ({select, deselect, toggle, isSelected}) {
return (
<button
onClick={toggle}
style={
isSelected
? {backgroundColor: 'green'}
: {backgroundColor: 'grey'}
}
>
{pet}
</button>
)
}}
</Choice>
))}
</div>
)Choices
Props
initialChoices {array|set}- the initial choices that selections may be made from
initialSelections {array|set}- the initial selections. These must also be members of
@initialChoices
- the initial selections. These must also be members of
minChoices {integer}- the minimum number of
choicesthat should remain in the state
- the minimum number of
maxChoices {integer}- the maximum number of
choicesthat can be added to the state
- the maximum number of
onBoundMinChoices {function ({choices <array|set>, addChoice <function>, deleteChoice <function>})}- called when the minimum bound of
choiceshas been reached. Callback should include one argument for object:{choices, addChoice, deleteChoice}wherechoicesis the set of items which triggered the bounding error
- called when the minimum bound of
onBoundMaxChoices {function ({choices <array|set>, addChoice <function>, deleteChoice <function>})}- called when the maximum bound of
choiceshas been reached. Callback should include one argument for object:{choices, addChoice, deleteChoice}wherechoicesis the set of items which triggered the bounding error
- called when the maximum bound of
onChoicesChange {function (items <array|set>)}- called whenever an item is added or removed from the set of
choices. Receives one argument forchoiceswhich reflects the latest state
- called whenever an item is added or removed from the set of
onAddChoice {function (items <array|set>)}- called whenever an item is added to the
choicesset. Receives one argument forchoiceswhich reflects the latest state.
- called whenever an item is added to the
onDeleteChoice {function (items <array|set>)}- called whenever an item is removed from the
choicesset. Receives one argument forchoiceswhich reflects the latest state
- called whenever an item is removed from the
minSelections {integer}- the minimum number of
selectionsthat should remain in the state
- the minimum number of
maxSelections {integer}- the maximum number of
selectionsthat can be added to the state
- the maximum number of
onBoundMinSelections {function ({selections <array|set>, select <function>, deselect <function>})}- called when the minimum bound of
selectionshas been reached. Callback should include one argument for object:{selections, select, deselect}whereselectionsis the set of items which triggered the bounding error
- called when the minimum bound of
onBoundMaxSelections {function ({selections <array|set>, select <function>, deselect <function>})}- called when the maximum bound of
selectionshas been reached. Callback should include one argument for object:{selections, select, deselect}whereselectionsis the set of items which triggered the bounding error
- called when the maximum bound of
onChange {function (items <array|set>)}- called whenever an item is added or removed from the set of
selections. Receives one argument forselectionswhich reflects the latest state
- called whenever an item is added or removed from the set of
onSelect {function (items <array|set>)}- called whenever an item is added to the
selectionsset. Receives one argument forselectionswhich reflects the latest state.
- called whenever an item is added to the
onDeselect {function (items <array|set>)}- called whenever an item is removed from the
selectionsset. Receives one argument forselectionswhich reflects the latest state
- called whenever an item is removed from the
Render Props
Methods
select(...items <any choice>)- adds one or several selections if they are available in
choices
- adds one or several selections if they are available in
deselect(...items <any selection>)- removes one or several
selections
- removes one or several
toggle(item <any choice>)- removes an item if it is in selections, otherwise adds it to selections
setSelections(items <array|set>)- sets
selectionsto whatever the value of@itemsis
- sets
clearSelections()- clears all selections
isSelected(item <any>)- returns
trueif@itemis inselections
- returns
addChoice(...items <any choice>)- adds one or several
choices
- adds one or several
deleteChoice(...items <any selection>)- removes one or several
choices
- removes one or several
setChoices(items <array|set>)- sets
choicesto whatever the value of@itemsis
- sets
clearChoices()- clears all choices
isChoice(item <any>)- returns
trueif@itemis inchoices
- returns
State
selections {array|set}- the selections currently in the state of the component. Is an
ArrayifinitialSelectionswas anArrayand aSetifinitialSelectionswas aSet
- the selections currently in the state of the component. Is an
choices {array|set}- the choices currently in the state of the component. Is an
ArrayifinitialChoiceswas anArrayand aSetifinitialChoiceswas aSet
- the choices currently in the state of the component. Is an
Choice
Props
value- the value of the choice from the
Set/Arrayof choices in theChoicescomponent
- the value of the choice from the
Render Props
Methods
select()- selects this choice from
choices
- selects this choice from
deselect()- deselects this choice from
selections
- deselects this choice from
toggle()- selects if
isSelected, otherwise deselects
- selects if
delete()- deletes this choice from
choicesandselections
- deletes this choice from
State
isSelected {bool>}- returns
trueif the propvalueprovided to this component resides inChoices.selections
- returns
ChoicesConsumer
Render Props
See Choices render props
0.1.19
7 years ago
0.1.18
7 years ago
0.1.17
7 years ago
0.1.16
7 years ago
0.1.15
7 years ago
0.1.14
7 years ago
0.1.13
7 years ago
0.1.12
7 years ago
0.1.11
7 years ago
0.1.10
7 years ago
0.1.9
7 years ago
0.1.8
8 years ago
0.1.7
8 years ago
0.1.6
8 years ago
0.1.5
8 years ago
0.1.4
8 years ago
0.1.3
8 years ago
0.1.2
8 years ago
0.1.1
8 years ago
0.1.0
8 years ago