arfedulov / semantic-ui-calendar-react

Datepicker react component based on semantic-ui-react components
https://arfedulov.github.io/semantic-ui-calendar-react/
MIT License
259 stars 92 forks source link

minDate throws error when field is empty #176

Open stevecastaneda opened 5 years ago

stevecastaneda commented 5 years ago

Describe the bug When the field is initially rendered with a null or an empty string value, minDate param will throw an error. The sandbox linked below doesn't seem to detail the error, but locally I get this:

TypeError: Cannot read property 'position' of undefined

Thrown by this piece of code:

function getInitialDatePosition(initDate, values, selectablePositions) {
  var selectable = selectablePositions.reduce(function (acc, pos) {
    acc.push({
      value: values[pos],
      position: pos
    });
    return acc;
  }, []);
  var res = find_1.default(selectable, function (item) {
    return item.value === initDate;
  });

  if (res) {
    return res.position;
  }

  return selectable[0].position;
}

Likely due to selectablePositions being an empty array when the function is called.

To Reproduce

  1. See: https://codesandbox.io/embed/cocky-currying-sxems
  2. Leave the initial state on date to an empty string.
  3. Focus the field to open the date input.
  4. Sandbox will throw an error. Refresh the viewer to start over.
  5. Add "12-01-2019" to the default state on date.
  6. Focus the date input.
  7. The minimum date works as expected (see console log).

When you start with a default date instead of an empty string, and then clear the date input field by backspacing or clearing with select/delete, the date input doesn't throw an error. It's only when the component is first rendered without a date or empty string it seems.

Expected behavior When the field is empty, calendar should still open with a minimum date.

Dependencies versions Provide version numbers of following packages:

stevecastaneda commented 5 years ago

After a bit more testing, when trying to use the moment package to calculate a minimum date, the error only seems to throw when any calculation is done. .add(30, "days") for example should work, but the error is thrown.

If you remove the .add function call, and just use .startOf, the error is not thrown.

zepster commented 4 years ago
<Form.Input
  minDate={moment().add(1, 'd')}
/>

If today is last day of month and we set minDate as next (switch month), we will crash, coz calendar can not contains available dates which to be selected

 return selectable[0].position;

how said @stevecastaneda