Hello again!
I'm running into an issue where the onChange() event handler is firing twice upon making a selection. Building upon the scenario given in #117, here is the updated context:
I am using 3 react-responsive single selects to select a start and end date range for a data visualization. Data is in form of years and quarters, where each year has anywhere from 1-4 quarters associated with that specific year.
The first two dropdowns, start year
and start quarter
control the start range.
The third dropdown, end year
, controls the end range.
Every time a user makes a new selection from one of these dropdowns, a chart is updated to display the newly selected date range and its associated data.
Updated Data Structures:
The quarterData shows that for each year/quarter pair any amount of data could be associated with it, ie. GDPGrowth, GDPPlus, attrition, ect.
const quarterData = [
{ year: 2000, quarter: 1, GDPGrowth: 1.3 },
{ year: 2000, quarter: 2, GDPGrowth: -2 },
{ year: 2001, quarter: 1, GDPGrowth: 2.5 },
{ year: 2002, quarter: 1, GDPGrowth: 4.5 },
{ year: 2002, quarter: 2, GDPGrowth: 2.8 },
{ year: 2002, quarter: 3, GDPGrowth: -3.2 },
{ year: 2002, quarter: 4, GDPGrowth: -0.1 },
{ year: 2003, quarter: 1, GDPGrowth: 4.5 },
{ year: 2003, quarter: 2, GDPGrowth: 2.8 },
{ year: 2003, quarter: 3, GDPGrowth: -3.2 },
{ year: 2003, quarter: 4, GDPGrowth: -0.1 },
{ year: 2004, quarter: 1, GDPGrowth: 4.5 },
{ year: 2004, quarter: 2, GDPGrowth: 2.8 },
{ year: 2004, quarter: 3, GDPGrowth: -3.2 },
{ year: 2004, quarter: 4, GDPGrowth: -0.1 },
{ year: 2005, quarter: 1, GDPGrowth: -0.9 }
];
In this data set, we have the associated quarters for each year options. Each quarter has its index in the quarterData
dataset.
const yearData = [
{
year: 2000,
quarters: [{ quarter: 1, index: 0 }, { quarter: 2, index: 1 }],
yearIndex: 0
},
{
year: 2001,
quarters: [{ quarter: 1, index: 2 }],
yearIndex: 1
},
{
year: 2002,
quarters: [
{ quarter: 1, index: 3 },
{ quarter: 2, index: 4 },
{ quarter: 3, index: 5 },
{ quarter: 4, index: 6 }
],
yearIndex: 2
},
{
year: 2003,
quarters: [
{ quarter: 1, index: 7 },
{ quarter: 2, index: 8 },
{ quarter: 3, index: 9 },
{ quarter: 4, index: 10 }
],
yearIndex: 3
},
{
year: 2004,
quarters: [
{ quarter: 1, index: 11 },
{ quarter: 2, index: 12 },
{ quarter: 3, index: 13 },
{ quarter: 4, index: 14 }
],
yearIndex: 4
},
{ year: 2005, quarters: [{ quarter: 1, index: 15 }], yearIndex: 5 }
];
The Data that would used for the start year options:
The value corresponds to the years' index in the yearData
array.
years: [ {text: 2000, value: 0}, {text: 2001, value: 1}, {text: 2002, value: 2}, {text: 2003, value: 3},
{text: 2004, value: 4}, {text: 2005, value: 5} ]
The data that is used for the quarter options when the year 2000 is selected from the start year dropdown:
The value corresponds to the quarter's index in the quarterData
array
quarters: [ {text: 1, value: 0}, {text: 2, value: 1} ]
It is also helpful to know the rules for displaying data that influence how the dropdowns function.
-
The user cannot select a year in the past from the end year dropdowm
a. We limit the end year options to ensure the user never selects a year from the end year dropdown that is earlier than the start year dropdown.
b. Every time the user selects a new start year, the end year options will update accordingly
-
At least 1 quarter of data must be used to power the data visualization
a. For example, if a start year only has one quarter available, we do not allow the user to select that same year as an end year because that would mean that the range from the start year/quarter to the end year could not display more than one quarter
b. I.e. Using the data above, if the user selected the start year as 2001, we would not allow the user to select 2001 from the end year dropdown because 2001 only has one available quarter of data
-
If the user selects a year/quarter in the future (ie. the start year is greater than the end year), then we either:
a. If the last quarter in the start is selected, then we push the selected year by one more than newly selected start year.
b. Set the end year to be the newly selected start year
c. For example:
i. 2001 Q1 is the Start and 2002 is the End
ii. The user selects a start year of 2003
iii. This date is in the future, the new start year will be 2003 and the new start quarter will be the first available quarter, Q1. The last quarter is not selected, so the new end year will be 2003
iv. If the user then selects Q4, without changing the start year, the new start will be 2003 Q4, to satisfy the rule of displaying at least 1 quarter of data, the end year must be pushed to 2004.
With this context in mind, here is the issue at hand:
The event handler for this.onChangeEndYear
is firing twice every selection. I think the issue stems from the key
attribute set to the end-year react-responsive single select.
- if the key is set to a static variable, ie.
key={2}
- event handler only fires twice when selecting the first year in the dataset, 2000
- it fires to update the end-year to the year 2000, it then fires again to update the end-year as 2005. Upon clicking the end-year dropdown, it displays the year 2000. Then clicking the end-year dropdown, it displays the year 2005.
- if the key is set to a dynamic variable, it must be set to a key that will not clash with the
start-quarter
key, i.e. `key = {endSelectedYear.yearIndex}
- the onChange() event handler fires twice when selecting any year from the end-year dropdown
- it only causes UI issues when selecting the first year in the dataset, the year 2000
- it fires to update the end-year to the year 2000, it then fires again to update the end-year as 2005. Upon clicking the end-year dropdown, it displays the year 2005 as the selected year and the UI will not update to display the correctly selected year, 2000.
Here is a codepen link to the above issue I am describing: https://codesandbox.io/s/7jxjlly7jj
Please let me know if you need more context. Im happy to provide further information that will help get to the bottom of this issue. Thanks!!