mirror of
https://github.com/Choices-js/Choices.git
synced 2024-05-19 05:56:33 +02:00
44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
const choices = (state = [], action) => {
|
|
switch (action.type) {
|
|
case 'ADD_ITEM':
|
|
// Add object to items array
|
|
return [...state, {
|
|
id: parseInt(action.id),
|
|
value: action.value,
|
|
active: true,
|
|
selected: false
|
|
}];
|
|
|
|
case 'UNSELECT_ALL':
|
|
return state.map((item) => {
|
|
if(item.selected) {
|
|
item.selected = false;
|
|
}
|
|
return item;
|
|
});
|
|
|
|
case 'REMOVE_ITEM':
|
|
// Set item to inactive
|
|
return state.map((item) => {
|
|
if(item.id === parseInt(action.id)) {
|
|
item.active = false;
|
|
}
|
|
return item;
|
|
});
|
|
|
|
case 'SELECT_ITEM':
|
|
return state.map((item) => {
|
|
if(item.id === parseInt(action.id)) {
|
|
item.selected = action.value;
|
|
}
|
|
|
|
return item;
|
|
});
|
|
|
|
|
|
default:
|
|
return state;
|
|
}
|
|
}
|
|
|
|
export default choices |