From 81dec08d82388bce111048bc35b4376bc0028f91 Mon Sep 17 00:00:00 2001 From: akaidc2 Date: Thu, 16 Nov 2017 21:36:57 -0500 Subject: [PATCH] display index updates to most currently modified date within range array --- .gitignore | 1 + src/Calendar/withMultipleRanges.js | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index c452c584..00e00d97 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /node_modules /umd npm-debug.log* +.idea \ No newline at end of file diff --git a/src/Calendar/withMultipleRanges.js b/src/Calendar/withMultipleRanges.js index bf8faec8..bb6ba91d 100644 --- a/src/Calendar/withMultipleRanges.js +++ b/src/Calendar/withMultipleRanges.js @@ -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}) @@ -101,18 +101,19 @@ 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({ @@ -120,15 +121,16 @@ function handleSelect(date, {onSelect, selected, selectionStart, setSelectionSta 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); }