@mtes-mct/monitor-ui 3.3.1 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [3.3.1](https://github.com/MTES-MCT/monitor-ui/compare/v3.3.0...v3.3.1) (2023-02-28)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * **deps:** downgrade cypress version ([cd7c3bd](https://github.com/MTES-MCT/monitor-ui/commit/cd7c3bd327d8b2463995214dc3d40bd539ba0f0b))
7
+
1
8
  # [3.3.0](https://github.com/MTES-MCT/monitor-ui/compare/v3.2.0...v3.3.0) (2023-02-27)
2
9
 
3
10
 
@@ -18,5 +18,7 @@ export type SearchProps<T = string> = Omit<RsuiteAutoCompleteProps, 'as' | 'cont
18
18
  options?: Option<T>[] | undefined;
19
19
  queryMap?: ((record: Record<string, any>) => Option<T>) | undefined;
20
20
  queryUrl?: string | undefined;
21
+ /** Duration in milliseconds */
22
+ throttleDuration?: number;
21
23
  };
22
- export declare function Search<T = string>({ baseContainer, defaultValue, error, isLabelHidden, isLight, isSearchIconHidden, label, MenuItem, onChange, onQuery, options, queryMap, queryUrl, ...originalProps }: SearchProps<T>): JSX.Element;
24
+ export declare function Search<T = string>({ baseContainer, defaultValue, error, isLabelHidden, isLight, isSearchIconHidden, label, MenuItem, onChange, onQuery, options, queryMap, queryUrl, throttleDuration, ...originalProps }: SearchProps<T>): JSX.Element;
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import styled, { createGlobalStyle, ThemeProvider as ThemeProvider$1, css } from 'styled-components';
2
2
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
3
- import React, { useMemo, useCallback, useEffect, useReducer, useRef, useState, forwardRef, useImperativeHandle } from 'react';
3
+ import React, { useMemo, useCallback, useRef, useEffect, useReducer, useState, forwardRef, useImperativeHandle } from 'react';
4
4
  import { Dropdown as Dropdown$1, AutoComplete, Checkbox as Checkbox$1, DatePicker as DatePicker$1, DateRangePicker as DateRangePicker$1, TagPicker, Radio, Input, SelectPicker } from 'rsuite';
5
5
  import { useField, useFormikContext } from 'formik';
6
6
 
@@ -3388,6 +3388,8 @@ const createInstance = (defaults) => {
3388
3388
  const ky = createInstance();
3389
3389
  var ky$1 = ky;
3390
3390
 
3391
+ function c(u,e,c){var i=this,a=useRef(null),o=useRef(0),f=useRef(null),l=useRef([]),m=useRef(),v=useRef(),d=useRef(u),p=useRef(!0);useEffect(function(){d.current=u;},[u]);var g=!e&&0!==e&&"undefined"!=typeof window;if("function"!=typeof u)throw new TypeError("Expected a function");e=+e||0;var w=!!(c=c||{}).leading,s=!("trailing"in c)||!!c.trailing,x="maxWait"in c,y=x?Math.max(+c.maxWait||0,e):null;useEffect(function(){return p.current=!0,function(){p.current=!1;}},[]);var h=useMemo(function(){var r=function(r){var n=l.current,t=m.current;return l.current=m.current=null,o.current=r,v.current=d.current.apply(t,n)},n=function(r,n){g&&cancelAnimationFrame(f.current),f.current=g?requestAnimationFrame(r):setTimeout(r,n);},t=function(r){if(!p.current)return !1;var n=r-a.current;return !a.current||n>=e||n<0||x&&r-o.current>=y},u=function(n){return f.current=null,s&&l.current?r(n):(l.current=m.current=null,v.current)},c=function r(){var c=Date.now();if(t(c))return u(c);if(p.current){var i=e-(c-a.current),f=x?Math.min(i,y-(c-o.current)):i;n(r,f);}},h=function(){var u=Date.now(),d=t(u);if(l.current=[].slice.call(arguments),m.current=i,a.current=u,d){if(!f.current&&p.current)return o.current=a.current,n(c,e),w?r(a.current):v.current;if(x)return n(c,e),r(a.current)}return f.current||n(c,e),v.current};return h.cancel=function(){f.current&&(g?cancelAnimationFrame(f.current):clearTimeout(f.current)),o.current=0,l.current=a.current=m.current=f.current=null;},h.isPending=function(){return !!f.current},h.flush=function(){return f.current?u(Date.now()):v.current},h},[w,x,e,y,s,g]);return h}
3392
+
3391
3393
  const FieldError = styled.p `
3392
3394
  color: ${p => p.theme.color.maximumRed};
3393
3395
  display: ${p => (p.isDisabled ? 'none' : 'block')};
@@ -3928,7 +3930,7 @@ function normalizeString(text) {
3928
3930
  return cleanText.length > 0 ? cleanText : undefined;
3929
3931
  }
3930
3932
 
3931
- function Search({ baseContainer, defaultValue, error, isLabelHidden, isLight = false, isSearchIconHidden = false, label, MenuItem, onChange, onQuery, options, queryMap, queryUrl, ...originalProps }) {
3933
+ function Search({ baseContainer, defaultValue, error, isLabelHidden, isLight = false, isSearchIconHidden = false, label, MenuItem, onChange, onQuery, options, queryMap, queryUrl, throttleDuration = 200, ...originalProps }) {
3932
3934
  // eslint-disable-next-line no-null/no-null
3933
3935
  const boxRef = useRef(null);
3934
3936
  const queryRef = useRef(undefined);
@@ -3956,6 +3958,11 @@ function Search({ baseContainer, defaultValue, error, isLabelHidden, isLight = f
3956
3958
  setInputValue('');
3957
3959
  setIsOpen(false);
3958
3960
  }, []);
3961
+ const fetchQueryResponse = c(async (_searched, _queryUrl, _queryMap) => {
3962
+ const url = _queryUrl.replace('%s', _searched);
3963
+ const rawData = await ky$1.get(url).json();
3964
+ return rawData.map(_queryMap);
3965
+ }, throttleDuration);
3959
3966
  const handleChange = useCallback(async (nextQuery) => {
3960
3967
  if (!(typeof nextQuery === 'string')) {
3961
3968
  return;
@@ -3963,9 +3970,7 @@ function Search({ baseContainer, defaultValue, error, isLabelHidden, isLight = f
3963
3970
  setInputValue(nextQuery);
3964
3971
  queryRef.current = normalizeString(nextQuery);
3965
3972
  if (queryUrl && queryMap && queryRef.current) {
3966
- const url = queryUrl.replace('%s', queryRef.current);
3967
- const rawData = await ky$1.get(url).json();
3968
- const nextData = rawData.map(queryMap);
3973
+ const nextData = (await fetchQueryResponse(queryRef.current, queryUrl, queryMap)) || [];
3969
3974
  setAutoGeneratedOptions(nextData);
3970
3975
  }
3971
3976
  setIsOpen(Boolean(queryRef.current));
@@ -3975,7 +3980,7 @@ function Search({ baseContainer, defaultValue, error, isLabelHidden, isLight = f
3975
3980
  if (onQuery) {
3976
3981
  onQuery(queryRef.current);
3977
3982
  }
3978
- }, [onChange, onQuery, queryMap, queryUrl]);
3983
+ }, [onChange, onQuery, queryMap, queryUrl, fetchQueryResponse]);
3979
3984
  const handleSelect = useCallback((nextValue) => {
3980
3985
  if (onChange) {
3981
3986
  onChange(nextValue);
@@ -5697,8 +5702,6 @@ const StyledInput$2 = styled(Input) `
5697
5702
 
5698
5703
  const noop = () => { };
5699
5704
 
5700
- function c(u,e,c){var i=this,a=useRef(null),o=useRef(0),f=useRef(null),l=useRef([]),m=useRef(),v=useRef(),d=useRef(u),p=useRef(!0);useEffect(function(){d.current=u;},[u]);var g=!e&&0!==e&&"undefined"!=typeof window;if("function"!=typeof u)throw new TypeError("Expected a function");e=+e||0;var w=!!(c=c||{}).leading,s=!("trailing"in c)||!!c.trailing,x="maxWait"in c,y=x?Math.max(+c.maxWait||0,e):null;useEffect(function(){return p.current=!0,function(){p.current=!1;}},[]);var h=useMemo(function(){var r=function(r){var n=l.current,t=m.current;return l.current=m.current=null,o.current=r,v.current=d.current.apply(t,n)},n=function(r,n){g&&cancelAnimationFrame(f.current),f.current=g?requestAnimationFrame(r):setTimeout(r,n);},t=function(r){if(!p.current)return !1;var n=r-a.current;return !a.current||n>=e||n<0||x&&r-o.current>=y},u=function(n){return f.current=null,s&&l.current?r(n):(l.current=m.current=null,v.current)},c=function r(){var c=Date.now();if(t(c))return u(c);if(p.current){var i=e-(c-a.current),f=x?Math.min(i,y-(c-o.current)):i;n(r,f);}},h=function(){var u=Date.now(),d=t(u);if(l.current=[].slice.call(arguments),m.current=i,a.current=u,d){if(!f.current&&p.current)return o.current=a.current,n(c,e),w?r(a.current):v.current;if(x)return n(c,e),r(a.current)}return f.current||n(c,e),v.current};return h.cancel=function(){f.current&&(g?cancelAnimationFrame(f.current):clearTimeout(f.current)),o.current=0,l.current=a.current=m.current=f.current=null;},h.isPending=function(){return !!f.current},h.flush=function(){return f.current?u(Date.now()):v.current},h},[w,x,e,y,s,g]);return h}
5701
-
5702
5705
  function isNumeric(val) {
5703
5706
  // parseFloat NaNs numeric-cast false positives (null|true|false|"")
5704
5707
  // ...but misinterprets leading-number strings, particularly hex literals ("0x...")