@mtes-mct/monitor-ui 4.0.2 → 4.0.3

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
+ ## [4.0.2](https://github.com/MTES-MCT/monitor-ui/compare/v4.0.1...v4.0.2) (2023-03-29)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * **fields:** handle value prop change in Select & MultiSelect ([db2f931](https://github.com/MTES-MCT/monitor-ui/commit/db2f931256b82dd8c1c0c7ddf1b2884154142260))
7
+
1
8
  ## [4.0.1](https://github.com/MTES-MCT/monitor-ui/compare/v4.0.0...v4.0.1) (2023-03-29)
2
9
 
3
10
 
package/index.js CHANGED
@@ -22295,25 +22295,15 @@ function getRsuiteDataFromOptions(options, optionValueKey) {
22295
22295
  }));
22296
22296
  }
22297
22297
 
22298
- /**
22299
- * Add the provided `item` in a collection if this collection doesn't include it or remove it if this collection does.
22300
- *
22301
- * This is useful with object collections and polymorphic types.
22302
- */
22303
- function toggleInCollection(item, collection) {
22304
- const isInCollection = includes(item, collection);
22305
- return isInCollection ? reject(equals(item), collection) : [...collection, item];
22306
- }
22307
-
22308
22298
  function MultiSelect({ baseContainer, disabled = false, error, isLabelHidden = false, isLight = false, isUndefinedWhenDisabled = false, label, onChange, options, optionValueKey, searchable = false, value, ...originalProps }) {
22309
22299
  // eslint-disable-next-line no-null/no-null
22310
22300
  const boxRef = useRef(null);
22311
- const selectedOptionValuesRef = useRef(value || []);
22312
22301
  const [isOpen, setIsOpen] = useState(false);
22313
22302
  const { controlledOnChange, controlledValue } = useFieldControl(value, onChange, {
22314
22303
  disabled,
22315
22304
  isUndefinedWhenDisabled
22316
22305
  });
22306
+ const controlledRsuiteValue = useMemo(() => (controlledValue || []).map(controlledValueItem => getRsuiteValueFromOptionValue(controlledValueItem, optionValueKey)), [controlledValue, optionValueKey]);
22317
22307
  const controlledError = useMemo(() => normalizeString(error), [error]);
22318
22308
  const data = useMemo(() => getRsuiteDataFromOptions(options, optionValueKey), [options, optionValueKey]);
22319
22309
  const hasError = useMemo(() => Boolean(controlledError), [controlledError]);
@@ -22322,16 +22312,15 @@ function MultiSelect({ baseContainer, disabled = false, error, isLabelHidden = f
22322
22312
  const close = useCallback(() => {
22323
22313
  setIsOpen(false);
22324
22314
  }, []);
22325
- const handleClean = useCallback(() => {
22326
- selectedOptionValuesRef.current = [];
22327
- controlledOnChange(undefined);
22328
- }, [controlledOnChange]);
22329
- const handleSelect = useCallback((_, selectedItem) => {
22330
- const nextValues = toggleInCollection(selectedItem.optionValue, selectedOptionValuesRef.current);
22331
- selectedOptionValuesRef.current = nextValues;
22332
- const normalizedNextValue = !nextValues.length ? undefined : nextValues;
22315
+ const handleChange = useCallback((nextOptionRsuiteValues) => {
22316
+ const nextValue = nextOptionRsuiteValues
22317
+ ? options
22318
+ .filter(option => nextOptionRsuiteValues.includes(optionValueKey ? option.value[optionValueKey] : String(option.value)))
22319
+ .map(option => option.value)
22320
+ : [];
22321
+ const normalizedNextValue = !nextValue.length ? undefined : nextValue;
22333
22322
  controlledOnChange(normalizedNextValue);
22334
- }, [controlledOnChange]);
22323
+ }, [controlledOnChange, options, optionValueKey]);
22335
22324
  const renderMenuItem = useCallback((node) => jsx("span", { title: String(node), children: String(node) }), []);
22336
22325
  const toggle = useCallback((event) => {
22337
22326
  let targetElement = event.target;
@@ -22351,9 +22340,9 @@ function MultiSelect({ baseContainer, disabled = false, error, isLabelHidden = f
22351
22340
  useEffect(() => {
22352
22341
  forceUpdate();
22353
22342
  }, [forceUpdate]);
22354
- return (jsxs(Field$2, { children: [jsx(Label, { disabled: disabled, hasError: hasError, htmlFor: originalProps.name, isHidden: isLabelHidden, children: label }), jsx(Box$4, { ref: boxRef, "$hasError": hasError, "$isActive": isOpen, "$isLight": isLight, onClick: toggle, children: boxRef.current && (jsx(TagPicker, { container: boxRef.current, data: data, disabled: disabled, id: originalProps.name, onClean: handleClean, onClick: toggle,
22343
+ return (jsxs(Field$2, { children: [jsx(Label, { disabled: disabled, hasError: hasError, htmlFor: originalProps.name, isHidden: isLabelHidden, children: label }), jsx(Box$4, { ref: boxRef, "$hasError": hasError, "$isActive": isOpen, "$isLight": isLight, onClick: toggle, children: boxRef.current && (jsx(TagPicker, { container: boxRef.current, data: data, disabled: disabled, id: originalProps.name,
22355
22344
  // Since we customized `ItemDataType` type by adding `optionValue`, we have an optional vs required conflict
22356
- onSelect: handleSelect, open: isOpen, renderMenuItem: renderMenuItem, searchable: searchable, value: controlledValue, ...originalProps }, key)) }), hasError && jsx(FieldError, { children: controlledError })] }));
22345
+ onChange: handleChange, onClick: toggle, open: isOpen, renderMenuItem: renderMenuItem, searchable: searchable, value: controlledRsuiteValue, ...originalProps }, key)) }), hasError && jsx(FieldError, { children: controlledError })] }));
22357
22346
  }
22358
22347
  const Box$4 = styled.div `
22359
22348
  position: relative;