Skip to content

Commit

Permalink
display index updates to most currently modified date within range array
Browse files Browse the repository at this point in the history
  • Loading branch information
akaidc2 committed Nov 17, 2017
1 parent 0cb09ce commit 81dec08
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/node_modules
/umd
npm-debug.log*
.idea
18 changes: 10 additions & 8 deletions src/Calendar/withMultipleRanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const withMultipleRanges = compose(
passThrough: {
...passThrough,
Day: {
onClick: (date) => handleSelect(date, {selected, ...props}),
onClick: (date) => handleSelect(date, {selected, setDisplayIndex, ...props}),
handlers: {
onMouseOver: !isTouchDevice && props.selectionStart
? (e) => handleMouseOver(e, {selected, ...props})
Expand Down Expand Up @@ -101,34 +101,36 @@ function getSortedSelection({start, end}) {
: {start: end, end: start};
}

function handleSelect(date, {onSelect, selected, selectionStart, setSelectionStart, selectionStartIdx, setSelectionStartIdx}) {
function handleSelect(date, {onSelect, selected, selectionStart, setSelectionStart, selectionStartIdx, setSelectionStartIdx, setDisplayIndex}) {
const positionOfDate = determineIfDateAlreadySelected(date, selected);
const funcs = {onSelect, setSelectionStart, setSelectionStartIdx};
const funcs = {onSelect, setSelectionStart, setSelectionStartIdx, setDisplayIndex};

if(positionOfDate.value && !selectionStart) { //selecting an already defined range
const selectedDate = selected[positionOfDate.index];//not clone so modding this is modding selected
selectedDate.end = date; //not possible to have start/end reversed when clicking on already set range

updateSelectedState(selectedDate.start, positionOfDate.index, selected, funcs);//grab index of selected and set in state
updateSelectedState(positionOfDate.index, selectedDate.start, positionOfDate.index, selected, funcs);//grab index of selected and set in state
} else if (selectionStart) { //ending date range selection
if (positionOfDate.value === PositionTypes.START && !(date < selectionStart)) { //if in process and selecting start, assume they want to cancel
updateSelectedState(null, null, [...selected.slice(0, positionOfDate.index), ...selected.slice(positionOfDate.index+1)], funcs);
const displayIdx = positionOfDate.index > 0 ? positionOfDate.index -1 : 0;
updateSelectedState(displayIdx, null, null, [...selected.slice(0, positionOfDate.index), ...selected.slice(positionOfDate.index+1)], funcs);
} else {
selected[selectionStartIdx] = { //modifying passed in object without clone due to immediate set state
...getSortedSelection({
start: selectionStart,
end: date,
}),
};
updateSelectedState(null, null, selected, funcs);
updateSelectedState(positionOfDate.index, null, null, selected, funcs);
}
} else { //starting new date range
updateSelectedState(date, selected.length, selected.concat({eventType:EVENT_TYPES.START, start: date, end: date}), funcs)//length accounts for increase due to concat
updateSelectedState(selected.length, date, selected.length, selected.concat({eventType:EVENT_TYPES.START, start: date, end: date}), funcs)//length accounts for increase due to concat
}
}

function updateSelectedState(selectStart, selectStartIdx, selected, {onSelect, setSelectionStart, setSelectionStartIdx}) {
function updateSelectedState(displayIdx, selectStart, selectStartIdx, selected, {onSelect, setSelectionStart, setSelectionStartIdx, setDisplayIndex}) {
onSelect(selected);
setDisplayIndex(displayIdx);
setSelectionStart(selectStart);
setSelectionStartIdx(selectStartIdx);
}
Expand Down

0 comments on commit 81dec08

Please sign in to comment.