Choices/src/scripts/reducers/items.js

53 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-10-08 16:52:54 +02:00
export const defaultState = [];
export default function items(state = defaultState, action) {
switch (action.type) {
case 'ADD_ITEM': {
// Add object to items array
const newState = [...state, {
id: action.id,
choiceId: action.choiceId,
2016-10-18 20:23:07 +02:00
groupId: action.groupId,
value: action.value,
label: action.label,
active: true,
highlighted: false,
2017-07-19 19:48:46 +02:00
customProperties: action.customProperties,
2017-08-02 15:05:26 +02:00
placeholder: (action.placeholder || false),
2017-08-15 10:29:42 +02:00
keyCode: null,
}];
2017-08-15 10:29:42 +02:00
return newState.map((obj) => {
const item = obj;
2017-12-10 19:00:57 +01:00
item.highlighted = false;
return item;
});
}
case 'REMOVE_ITEM': {
// Set item to inactive
2017-08-15 10:29:42 +02:00
return state.map((obj) => {
const item = obj;
if (item.id === action.id) {
item.active = false;
2016-08-14 23:14:37 +02:00
}
return item;
});
}
case 'HIGHLIGHT_ITEM': {
2017-08-15 10:29:42 +02:00
return state.map((obj) => {
const item = obj;
if (item.id === action.id) {
item.highlighted = action.highlighted;
2016-08-14 23:14:37 +02:00
}
return item;
});
}
default: {
return state;
}
}
2017-10-08 16:52:54 +02:00
}