@carto/meridian-ds 2.1.0 → 2.1.1

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
@@ -4,6 +4,10 @@
4
4
 
5
5
  ## 2.0
6
6
 
7
+ ### 2.1.1
8
+
9
+ - MultipleSelectField: select all filter is not working from empty state [#231](https://github.com/CartoDB/meridian-ds/pull/231)
10
+
7
11
  ### 2.1.0
8
12
 
9
13
  - New Link components and Button with externalLink behavior [#202](https://github.com/CartoDB/meridian-ds/pull/202)
@@ -647,20 +647,33 @@ function useMultipleSelectField({
647
647
  onChange
648
648
  }) {
649
649
  const [currentOptions, setCurrentOptions] = React.useState(selectedOptions ?? []);
650
- const areAllSelected = options.length === currentOptions.length;
651
- const areAnySelected = currentOptions.length > 0;
650
+ const isInternalUpdate = React.useRef(false);
652
651
  React.useEffect(() => {
653
- if (currentOptions !== selectedOptions) {
654
- setCurrentOptions(currentOptions);
652
+ if (!isInternalUpdate.current) {
653
+ setCurrentOptions(selectedOptions ?? []);
655
654
  }
655
+ isInternalUpdate.current = false;
656
656
  }, [selectedOptions]);
657
+ const enabledOptions = React.useMemo(
658
+ () => options.filter(({ disabled }) => !disabled),
659
+ [options]
660
+ );
661
+ const enabledOptionValues = React.useMemo(
662
+ () => enabledOptions.map(({ value }) => value),
663
+ [enabledOptions]
664
+ );
665
+ const areAllSelected = enabledOptionValues.length > 0 && enabledOptionValues.every((value) => currentOptions.includes(value));
666
+ const areAnySelected = currentOptions.length > 0;
657
667
  const handleChange = (event) => {
668
+ if (isInternalUpdate.current) {
669
+ return;
670
+ }
658
671
  const {
659
672
  target: { value }
660
673
  } = event;
661
- const options2 = typeof value === "string" ? value.split(",") : value.filter((v) => v !== void 0);
662
- setCurrentOptions(options2);
663
- onChange(options2);
674
+ const newOptions = typeof value === "string" ? value.split(",") : value.filter((v) => v !== void 0);
675
+ setCurrentOptions(newOptions);
676
+ onChange(newOptions);
664
677
  };
665
678
  const selectAll = () => {
666
679
  const optionsValues = options == null ? void 0 : options.filter(({ disabled }) => !disabled).map(({ value }) => value);
@@ -668,18 +681,17 @@ function useMultipleSelectField({
668
681
  const allSelected = optionsValues.every(
669
682
  (value) => currentOptions.includes(value)
670
683
  );
671
- if (allSelected) {
672
- setCurrentOptions([]);
673
- onChange([]);
674
- } else {
675
- setCurrentOptions(optionsValues);
676
- onChange(optionsValues);
677
- }
684
+ const newValue = allSelected ? [] : optionsValues;
685
+ isInternalUpdate.current = true;
686
+ setCurrentOptions(newValue);
687
+ onChange(newValue);
678
688
  }
679
689
  };
680
690
  const unselectAll = () => {
681
- setCurrentOptions([]);
682
- onChange([]);
691
+ const newValue = [];
692
+ isInternalUpdate.current = true;
693
+ setCurrentOptions(newValue);
694
+ onChange(newValue);
683
695
  };
684
696
  return {
685
697
  areAllSelected,
@@ -647,20 +647,33 @@ function useMultipleSelectField({
647
647
  onChange
648
648
  }) {
649
649
  const [currentOptions, setCurrentOptions] = useState(selectedOptions ?? []);
650
- const areAllSelected = options.length === currentOptions.length;
651
- const areAnySelected = currentOptions.length > 0;
650
+ const isInternalUpdate = useRef(false);
652
651
  useEffect(() => {
653
- if (currentOptions !== selectedOptions) {
654
- setCurrentOptions(currentOptions);
652
+ if (!isInternalUpdate.current) {
653
+ setCurrentOptions(selectedOptions ?? []);
655
654
  }
655
+ isInternalUpdate.current = false;
656
656
  }, [selectedOptions]);
657
+ const enabledOptions = useMemo(
658
+ () => options.filter(({ disabled }) => !disabled),
659
+ [options]
660
+ );
661
+ const enabledOptionValues = useMemo(
662
+ () => enabledOptions.map(({ value }) => value),
663
+ [enabledOptions]
664
+ );
665
+ const areAllSelected = enabledOptionValues.length > 0 && enabledOptionValues.every((value) => currentOptions.includes(value));
666
+ const areAnySelected = currentOptions.length > 0;
657
667
  const handleChange = (event) => {
668
+ if (isInternalUpdate.current) {
669
+ return;
670
+ }
658
671
  const {
659
672
  target: { value }
660
673
  } = event;
661
- const options2 = typeof value === "string" ? value.split(",") : value.filter((v) => v !== void 0);
662
- setCurrentOptions(options2);
663
- onChange(options2);
674
+ const newOptions = typeof value === "string" ? value.split(",") : value.filter((v) => v !== void 0);
675
+ setCurrentOptions(newOptions);
676
+ onChange(newOptions);
664
677
  };
665
678
  const selectAll = () => {
666
679
  const optionsValues = options == null ? void 0 : options.filter(({ disabled }) => !disabled).map(({ value }) => value);
@@ -668,18 +681,17 @@ function useMultipleSelectField({
668
681
  const allSelected = optionsValues.every(
669
682
  (value) => currentOptions.includes(value)
670
683
  );
671
- if (allSelected) {
672
- setCurrentOptions([]);
673
- onChange([]);
674
- } else {
675
- setCurrentOptions(optionsValues);
676
- onChange(optionsValues);
677
- }
684
+ const newValue = allSelected ? [] : optionsValues;
685
+ isInternalUpdate.current = true;
686
+ setCurrentOptions(newValue);
687
+ onChange(newValue);
678
688
  }
679
689
  };
680
690
  const unselectAll = () => {
681
- setCurrentOptions([]);
682
- onChange([]);
691
+ const newValue = [];
692
+ isInternalUpdate.current = true;
693
+ setCurrentOptions(newValue);
694
+ onChange(newValue);
683
695
  };
684
696
  return {
685
697
  areAllSelected,
@@ -1 +1 @@
1
- {"version":3,"file":"useMultipleSelectField.d.ts","sourceRoot":"","sources":["../../../../../src/components/molecules/MultipleSelectField/useMultipleSelectField.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AACtD,OAAO,KAAK,EACV,yBAAyB,EACzB,wBAAwB,EACzB,MAAM,uBAAuB,CAAA;AAE9B,MAAM,CAAC,OAAO,UAAU,sBAAsB,CAAC,EAC7C,eAAe,EACf,OAAO,EACP,QAAQ,GACT,EAAE,IAAI,CACL,QAAQ,CAAC,wBAAwB,CAAC,EAClC,iBAAiB,GAAG,SAAS,GAAG,UAAU,CAC3C;;;;0BAeU,iBAAiB,CAAC,yBAAyB,CAAC,OAAO,CAAC,EAAE,CAAC;;;EA+CjE"}
1
+ {"version":3,"file":"useMultipleSelectField.d.ts","sourceRoot":"","sources":["../../../../../src/components/molecules/MultipleSelectField/useMultipleSelectField.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AACtD,OAAO,KAAK,EACV,yBAAyB,EACzB,wBAAwB,EACzB,MAAM,uBAAuB,CAAA;AAE9B,MAAM,CAAC,OAAO,UAAU,sBAAsB,CAAC,EAC7C,eAAe,EACf,OAAO,EACP,QAAQ,GACT,EAAE,IAAI,CACL,QAAQ,CAAC,wBAAwB,CAAC,EAClC,iBAAiB,GAAG,SAAS,GAAG,UAAU,CAC3C;;;;0BA4BU,iBAAiB,CAAC,yBAAyB,CAAC,OAAO,CAAC,EAAE,CAAC;;;EAsDjE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@carto/meridian-ds",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "CARTO Meridian Design System",
5
5
  "type": "module",
6
6
  "scripts": {