Skip to content

Commit

Permalink
STSMACOM-860 Fix <DateRangeFilter> validation errors disappear when…
Browse files Browse the repository at this point in the history
… another facet value changes
  • Loading branch information
BogdanDenis committed Oct 18, 2024
1 parent aa0f86a commit 346359c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* Avoid deprecated `defaultProps` for functional components. Refs STSMACOM-835.
* Upgrade `notes` to `v4.0`. Refs STSMACOM-861.
* Improve confirmation modal footer for `ControlledVocab` component. Refs STSMACOM-863.
* Fix `<DateRangeFilter>` validation errors disappear when another facet value changes. Fixes STSMACOM-860.

## [9.1.3](https://github.com/folio-org/stripes-smart-components/tree/v9.1.3) (2024-05-06)
[Full Changelog](https://github.com/folio-org/stripes-smart-components/compare/v9.1.2...v9.1.3)
Expand Down
16 changes: 13 additions & 3 deletions lib/SearchAndSort/components/DateRangeFilter/DateRangeFilter.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { memo } from 'react';
import { PropTypes } from 'prop-types';
import { FormattedMessage } from 'react-intl';
import isEqual from 'lodash/isEqual';

import { Button, Datepicker, AVAILABLE_PLACEMENTS } from '@folio/stripes-components';

import { useSetRef, useSetRefOnFocus } from '../../../utils';
import { usePrevious, useSetRef, useSetRefOnFocus } from '../../../utils';
import {
isDateValid,
validateDateRange,
Expand Down Expand Up @@ -102,6 +103,7 @@ const TheComponent = ({
...defaultFilterState,
selectedValues
});
const previousSelectedValues = usePrevious(selectedValues, { startDate: '', endDate: '' });

const setFocusRef = useSetRef(focusRef);
const setFocusRefOnFocus = useSetRefOnFocus(focusRef);
Expand Down Expand Up @@ -180,11 +182,19 @@ const TheComponent = ({
};

React.useEffect(() => {
// only need to re-initialize when props change. internal state change shouldn't trigger this

if (isEqual(previousSelectedValues, selectedValues)) {
return;
}

setFilterValue({
...getInitialValidationData(selectedValues, dateFormat, requiredFields),
selectedValues
selectedValues,
});
}, [selectedValues, dateFormat, requiredFields]);

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [previousSelectedValues, selectedValues, dateFormat, requiredFields]);

return (
<>
Expand Down
19 changes: 9 additions & 10 deletions lib/utils/usePrevious.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import {
useRef,
useEffect,
} from 'react';
import { useState } from 'react';

const usePrevious = value => {
const ref = useRef();
const usePrevious = (value, defaultValue) => {
const [current, setCurrent] = useState(value);
const [previous, setPrevious] = useState(defaultValue);

useEffect(() => {
ref.current = value;
});
if (value !== current) {
setPrevious(current);
setCurrent(value);
}

return ref.current;
return previous;
};

export default usePrevious;
Expand Down

0 comments on commit 346359c

Please sign in to comment.