@rolster/react-components 18.13.4 → 18.13.5

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/dist/es/index.js CHANGED
@@ -1369,41 +1369,63 @@ const MAX_ELEMENTS = 6;
1369
1369
  function useAutocompleteField({ disabled, formControl, onSelect, onValue, suggestions }) {
1370
1370
  const [pattern, setPattern] = useState('');
1371
1371
  const [coincidences, setCoincidences] = useState([]);
1372
- const [store, setStore] = useState({
1372
+ const currentStore = useRef({
1373
1373
  pattern: '',
1374
1374
  coincidences: [],
1375
1375
  previous: null
1376
1376
  });
1377
1377
  const listControl = useListControl({ suggestions, formControl });
1378
1378
  const { collection, inputRef, navigationElement, navigationInput, setFocused, setValue, setVisible } = listControl;
1379
+ const initializedState = useRef(false);
1380
+ const initializedCollection = useRef(false);
1379
1381
  const changeInternal = useRef(false);
1380
- useEffect(() => filterSuggestions(pattern, true), [suggestions]);
1381
- useEffect(() => filterSuggestions(pattern), [pattern]);
1382
1382
  useEffect(() => {
1383
+ refreshCoincidences(pattern, true);
1384
+ }, [suggestions]);
1385
+ useEffect(() => {
1386
+ refreshCoincidences(pattern);
1387
+ }, [pattern]);
1388
+ useEffect(() => {
1389
+ if (!initializedState.current || !initializedCollection.current) {
1390
+ initializedState.current = true;
1391
+ return;
1392
+ }
1383
1393
  if (changeInternal.current) {
1384
1394
  changeInternal.current = false;
1395
+ return;
1385
1396
  }
1386
- else {
1387
- resetState(formControl?.state);
1388
- }
1397
+ refresh(collection, formControl?.state);
1389
1398
  }, [formControl?.state]);
1390
- useEffect(() => resetCollection(collection, formControl?.state), [collection]);
1399
+ useEffect(() => {
1400
+ if (!initializedCollection.current || !initializedState.current) {
1401
+ initializedCollection.current = true;
1402
+ return;
1403
+ }
1404
+ refresh(collection, formControl?.state);
1405
+ }, [collection]);
1406
+ function refresh(collection, state) {
1407
+ if (!state) {
1408
+ return setValue('');
1409
+ }
1410
+ const element = collection.find(state);
1411
+ if (element) {
1412
+ return setValue(element.description);
1413
+ }
1414
+ setValue('');
1415
+ setFormState(undefined);
1416
+ }
1391
1417
  function setFormState(value) {
1392
1418
  if (formControl) {
1393
1419
  changeInternal.current = true;
1394
1420
  formControl.setState(value);
1395
1421
  }
1396
1422
  }
1397
- function resetCollection(collection, state) {
1398
- setValue(state ? collection.find(state)?.description || '' : '');
1399
- }
1400
- function resetState(state) {
1401
- resetCollection(collection, state);
1402
- }
1403
1423
  function onClickControl() {
1404
1424
  if (!disabled) {
1405
1425
  setVisible(true);
1406
- setTimeout(() => inputRef?.current?.focus(), DURATION_ANIMATION$1);
1426
+ setTimeout(() => {
1427
+ inputRef?.current?.focus();
1428
+ }, DURATION_ANIMATION$1);
1407
1429
  }
1408
1430
  }
1409
1431
  function onFocusInput() {
@@ -1457,15 +1479,15 @@ function useAutocompleteField({ disabled, formControl, onSelect, onValue, sugges
1457
1479
  onValue(value);
1458
1480
  }
1459
1481
  }
1460
- function filterSuggestions(pattern, reboot = false) {
1461
- const result = createStoreAutocomplete({
1482
+ function refreshCoincidences(pattern, reboot = false) {
1483
+ const { collection, store } = createStoreAutocomplete({
1462
1484
  pattern,
1463
1485
  suggestions,
1464
1486
  reboot,
1465
- store
1487
+ store: currentStore.current
1466
1488
  });
1467
- setCoincidences(result.collection.slice(0, MAX_ELEMENTS));
1468
- setStore(result.store);
1489
+ currentStore.current = store;
1490
+ setCoincidences(collection.slice(0, MAX_ELEMENTS));
1469
1491
  }
1470
1492
  return {
1471
1493
  coincidences,
@@ -1906,28 +1928,44 @@ function RlsFormNavigation({ children, visible, rlsTheme }) {
1906
1928
  function useSelectField({ suggestions, formControl, onSelect, onValue }) {
1907
1929
  const listControl = useListControl({ suggestions, formControl });
1908
1930
  const { collection, inputRef, visible, navigationElement, navigationInput, setFocused, setValue, setVisible } = listControl;
1931
+ const initializedState = useRef(false);
1932
+ const initializedCollection = useRef(false);
1909
1933
  const changeInternal = useRef(false);
1910
1934
  useEffect(() => {
1935
+ if (!initializedState.current || !initializedCollection.current) {
1936
+ initializedState.current = true;
1937
+ return;
1938
+ }
1911
1939
  if (changeInternal.current) {
1912
1940
  changeInternal.current = false;
1941
+ return;
1913
1942
  }
1914
- else {
1915
- resetState(formControl?.state);
1916
- }
1943
+ refresh(collection, formControl?.state);
1917
1944
  }, [formControl?.state]);
1918
- useEffect(() => resetCollection(collection, formControl?.state), [collection]);
1945
+ useEffect(() => {
1946
+ if (!initializedCollection.current || !initializedState.current) {
1947
+ initializedCollection.current = true;
1948
+ return;
1949
+ }
1950
+ refresh(collection, formControl?.state);
1951
+ }, [collection]);
1952
+ function refresh(collection, state) {
1953
+ if (!state) {
1954
+ return setValue('');
1955
+ }
1956
+ const element = collection.find(state);
1957
+ if (element) {
1958
+ return setValue(element.description);
1959
+ }
1960
+ setValue('');
1961
+ setFormState(undefined);
1962
+ }
1919
1963
  function setFormState(value) {
1920
1964
  if (formControl) {
1921
1965
  changeInternal.current = true;
1922
1966
  formControl.setState(value);
1923
1967
  }
1924
1968
  }
1925
- function resetCollection(collection, state) {
1926
- setValue(state ? collection.find(state)?.description || '' : '');
1927
- }
1928
- function resetState(state) {
1929
- resetCollection(collection, state);
1930
- }
1931
1969
  function onFocusInput() {
1932
1970
  setFocused(true);
1933
1971
  }
@@ -1953,9 +1991,8 @@ function useSelectField({ suggestions, formControl, onSelect, onValue }) {
1953
1991
  }
1954
1992
  }
1955
1993
  function onClickAction() {
1956
- const newVisible = !visible;
1957
- setVisible(newVisible);
1958
- if (newVisible) {
1994
+ setVisible(!visible);
1995
+ if (!visible) {
1959
1996
  inputRef?.current?.focus();
1960
1997
  }
1961
1998
  }