@oliasoft-open-source/react-ui-library 4.12.4 → 4.13.0-beta-2

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/index.d.ts CHANGED
@@ -1038,9 +1038,11 @@ export declare interface IPortalProps {
1038
1038
  }
1039
1039
 
1040
1040
  declare interface IPredefinedOption {
1041
- value?: string;
1041
+ value: string;
1042
1042
  label: string;
1043
1043
  valueKey?: string;
1044
+ unitkey?: string;
1045
+ linked?: boolean;
1044
1046
  }
1045
1047
 
1046
1048
  export declare interface IPrintHeaderProps {
@@ -1560,17 +1562,13 @@ export declare interface IUnitInputProps {
1560
1562
  unitkey?: string;
1561
1563
  initUnit?: string;
1562
1564
  noConversion?: boolean;
1563
- doNotConvertValue?: boolean;
1564
1565
  testId?: string;
1565
1566
  warning?: string | boolean | null;
1566
1567
  predefinedOptions?: IPredefinedOption[] | null;
1567
- initialPredefinedOption?: boolean;
1568
- shouldLinkAutomaticly?: boolean;
1569
- selectedPredefinedOptionKey?: string;
1568
+ initialPredefinedOption?: string;
1570
1569
  validationCallback?: (name: string, error: string | null) => any;
1571
1570
  disabledValidation?: boolean;
1572
1571
  allowEmpty?: boolean;
1573
- autoValue?: string;
1574
1572
  onChange?: TOnChangeEventHandler;
1575
1573
  onSwitchUnit?: (unit: string) => void;
1576
1574
  onClick?: MouseEventHandler<HTMLInputElement>;
@@ -1872,7 +1870,7 @@ declare type TUnitTableCellShape = TCellShape | IUnitTableStaticCell | IUnitTabl
1872
1870
 
1873
1871
  declare type UnitContextType = any;
1874
1872
 
1875
- export declare const UnitInput: ({ name, placeholder, disabled, disabledUnit, error, left, small, width, value, unitkey, initUnit, noConversion, onChange, onClick, onFocus, onSwitchUnit, unitTemplate, doNotConvertValue, testId, warning, predefinedOptions, initialPredefinedOption, shouldLinkAutomaticly, selectedPredefinedOptionKey, validationCallback, disabledValidation, allowEmpty, autoValue, convertBackToStorageUnit, enableCosmeticRounding, enableDisplayRounding, }: IUnitInputProps) => JSX_2.Element;
1873
+ export declare const UnitInput: ({ name, placeholder, disabled, disabledUnit, error, left, small, width, value, unitkey, initUnit, noConversion, onChange, onClick, onFocus, onSwitchUnit, unitTemplate, testId, warning, predefinedOptions, initialPredefinedOption, validationCallback, disabledValidation, allowEmpty, convertBackToStorageUnit, enableCosmeticRounding, enableDisplayRounding, }: IUnitInputProps) => JSX_2.Element;
1876
1874
 
1877
1875
  export declare const UnitTable: ({ table, unitConfig, convertBackToStorageUnit, enableCosmeticRounding, enableDisplayRounding, }: IUnitTableProps) => JSX_2.Element;
1878
1876
 
package/dist/index.js CHANGED
@@ -15,7 +15,7 @@ import React__default, { useContext, isValidElement, useState, useRef, useEffect
15
15
  import * as PropTypes from "prop-types";
16
16
  import PropTypes__default from "prop-types";
17
17
  import ReactDOM, { createPortal, unstable_batchedUpdates, render } from "react-dom";
18
- import { noop as noop$3, set, get as get$2, isString as isString$3, isNumber as isNumber$1, isBoolean as isBoolean$2, isFunction as isFunction$3, isEmpty, isArray as isArray$1, toNumber, debounce as debounce$2, isObject as isObject$5, isEqual as isEqual$4 } from "lodash";
18
+ import { noop as noop$3, set, get as get$2, isString as isString$3, isNumber as isNumber$1, isBoolean as isBoolean$2, isFunction as isFunction$3, isEmpty, isArray as isArray$1, toNumber, debounce as debounce$2, isObject as isObject$5, isNil, isEqual as isEqual$4 } from "lodash";
19
19
  import { toString as toString$1, isScientificStringNum, roundToPrecision, toNum, roundToFixed, validateNumber, cleanNumStr, stripLeadingZeros, unitFromQuantity, getUnit, KNOWN_UNITS, ALT_UNITS, isValueWithUnit, getValue as getValue$1, withUnit, convertSamePrecision, convertAndGetValue, split as split$1, label as label$b, isValidNum, getAltUnitsListByQuantity, altUnitsList, getUnitsForQuantity, roundByMagnitudeToFixed } from "@oliasoft-open-source/units";
20
20
  var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
21
21
  function getDefaultExportFromCjs(x2) {
@@ -70259,6 +70259,134 @@ const initializeContext = (context2) => {
70259
70259
  UnitContext = context2;
70260
70260
  }
70261
70261
  };
70262
+ const extractValue = (value) => {
70263
+ if (isNil(value)) return value;
70264
+ return isValueWithUnit(value) ? getValue$1(value) : value;
70265
+ };
70266
+ const PredefinedOptionsMenu = ({
70267
+ predefinedOptions,
70268
+ displayLayer,
70269
+ unitkey: rootUnitKey,
70270
+ unitTemplate,
70271
+ onPredefinedOptionSelect,
70272
+ disabled: disabled2,
70273
+ initialPredefinedOption,
70274
+ predefinedOptionsMenuState,
70275
+ setPredefinedOptionsMenuState,
70276
+ testId
70277
+ }) => {
70278
+ const extractedValue = extractValue(displayLayer == null ? void 0 : displayLayer.value);
70279
+ const [foundPredefinedMenuOption, setFoundPredefinedMenuOption] = useState(void 0);
70280
+ const convertPredefinedOptions = ({
70281
+ value,
70282
+ unitkey: optionUnitKey
70283
+ }) => {
70284
+ if (!value || !rootUnitKey) return value;
70285
+ const selectedUnitKey = optionUnitKey || rootUnitKey;
70286
+ const preferredOptionUnit = getPreferredUnit(selectedUnitKey, unitTemplate);
70287
+ const { value: resultValue } = safeConvertValue({
70288
+ value,
70289
+ toUnit: preferredOptionUnit,
70290
+ unitkey: selectedUnitKey,
70291
+ defaultFromUnit: "",
70292
+ doNotConvertValue: false
70293
+ });
70294
+ return withUnit(resultValue, preferredOptionUnit);
70295
+ };
70296
+ const createPredefinedOption = (el2) => {
70297
+ const convertedValue = convertPredefinedOptions(el2);
70298
+ const [inputValue = "", unit2 = ""] = split$1(convertedValue);
70299
+ return {
70300
+ type: MenuType.OPTION,
70301
+ inline: true,
70302
+ onClick: () => {
70303
+ const validation = validateNumber(inputValue);
70304
+ if (validation.valid && !disabled2) {
70305
+ setPredefinedOptionsMenuState(PredefinedOptionsMenuState.PREDEFINED);
70306
+ onPredefinedOptionSelect(inputValue, el2);
70307
+ setFoundPredefinedMenuOption(el2);
70308
+ }
70309
+ },
70310
+ label: /* @__PURE__ */ jsxs(Fragment$1, { children: [
70311
+ /* @__PURE__ */ jsx(Text, { children: el2.label }),
70312
+ /* @__PURE__ */ jsx(Spacer, { width: "20px", height: "0" })
70313
+ ] }),
70314
+ description: `${inputValue} ${unit2}`,
70315
+ selected: withUnit(displayLayer == null ? void 0 : displayLayer.value, (displayLayer == null ? void 0 : displayLayer.unit) ?? "") === convertedValue && predefinedOptionsMenuState === PredefinedOptionsMenuState.PREDEFINED
70316
+ };
70317
+ };
70318
+ const initialPredefinedOptionObject = useMemo$1(() => {
70319
+ return predefinedOptions.find(
70320
+ (option2) => option2.value === initialPredefinedOption || option2.label === initialPredefinedOption || option2.valueKey === initialPredefinedOption
70321
+ );
70322
+ }, [initialPredefinedOption, predefinedOptions]);
70323
+ useEffect(() => {
70324
+ if (initialPredefinedOptionObject) {
70325
+ setPredefinedOptionsMenuState(PredefinedOptionsMenuState.PREDEFINED);
70326
+ setFoundPredefinedMenuOption(initialPredefinedOptionObject);
70327
+ onPredefinedOptionSelect(
70328
+ initialPredefinedOptionObject.value,
70329
+ initialPredefinedOptionObject
70330
+ );
70331
+ }
70332
+ }, [initialPredefinedOptionObject]);
70333
+ useEffect(() => {
70334
+ const foundOption = predefinedOptions.find((option2) => {
70335
+ const convertedValue = convertPredefinedOptions(option2);
70336
+ return convertedValue === withUnit(displayLayer == null ? void 0 : displayLayer.value, (displayLayer == null ? void 0 : displayLayer.unit) ?? "");
70337
+ });
70338
+ if (foundOption) {
70339
+ setPredefinedOptionsMenuState(PredefinedOptionsMenuState.PREDEFINED);
70340
+ setFoundPredefinedMenuOption(foundOption);
70341
+ } else {
70342
+ setPredefinedOptionsMenuState(PredefinedOptionsMenuState.CUSTOM);
70343
+ setFoundPredefinedMenuOption(void 0);
70344
+ }
70345
+ }, [extractedValue, predefinedOptions]);
70346
+ const sectionsPredefinedMenu = [
70347
+ {
70348
+ type: MenuType.OPTION,
70349
+ inline: true,
70350
+ onClick: () => {
70351
+ if (!disabled2) {
70352
+ setPredefinedOptionsMenuState(PredefinedOptionsMenuState.CUSTOM);
70353
+ }
70354
+ },
70355
+ label: "Custom",
70356
+ selected: predefinedOptionsMenuState === PredefinedOptionsMenuState.CUSTOM
70357
+ },
70358
+ ...predefinedOptions.map(createPredefinedOption)
70359
+ ];
70360
+ return /* @__PURE__ */ jsx(
70361
+ Tooltip,
70362
+ {
70363
+ text: predefinedOptionsMenuState === PredefinedOptionsMenuState.PREDEFINED ? foundPredefinedMenuOption == null ? void 0 : foundPredefinedMenuOption.label : "",
70364
+ children: /* @__PURE__ */ jsx(
70365
+ Menu,
70366
+ {
70367
+ testId: testId && `${testId}-predefined-menu`,
70368
+ maxHeight: 380,
70369
+ groupOrder: "first",
70370
+ disabled: disabled2,
70371
+ menu: {
70372
+ colored: true,
70373
+ trigger: "Component",
70374
+ component: /* @__PURE__ */ jsx(
70375
+ Button$1,
70376
+ {
70377
+ groupOrder: "first",
70378
+ active: predefinedOptionsMenuState === PredefinedOptionsMenuState.PREDEFINED,
70379
+ icon: predefinedOptionsMenuState === PredefinedOptionsMenuState.PREDEFINED ? "link" : "unlink"
70380
+ }
70381
+ ),
70382
+ small: true,
70383
+ sections: sectionsPredefinedMenu
70384
+ }
70385
+ }
70386
+ )
70387
+ }
70388
+ );
70389
+ };
70262
70390
  const predefinedMenuActive = "_predefinedMenuActive_ye9w9_1";
70263
70391
  const inputWrapper = "_inputWrapper_ye9w9_4";
70264
70392
  const styles = {
@@ -70283,28 +70411,24 @@ const UnitInput = ({
70283
70411
  onFocus = noop$3,
70284
70412
  onSwitchUnit = noop$3,
70285
70413
  unitTemplate,
70286
- doNotConvertValue = false,
70287
70414
  testId,
70288
70415
  warning: warning2 = null,
70289
70416
  predefinedOptions,
70290
- initialPredefinedOption = false,
70291
- shouldLinkAutomaticly = true,
70292
- selectedPredefinedOptionKey,
70417
+ initialPredefinedOption = "",
70293
70418
  validationCallback = () => ({ name: "", error: null }),
70294
70419
  disabledValidation = false,
70295
70420
  allowEmpty = false,
70296
- autoValue,
70297
70421
  convertBackToStorageUnit = false,
70298
70422
  enableCosmeticRounding = true,
70299
70423
  enableDisplayRounding = false
70300
70424
  }) => {
70301
70425
  const context2 = useUnitContext();
70302
70426
  const runAfterUpdate = useRunAfterUpdate();
70427
+ const [predefinedOptionsMenuState, setPredefinedOptionsMenuState] = useState(PredefinedOptionsMenuState.CUSTOM);
70303
70428
  if (typeof value === "number") {
70304
70429
  value = `${value}`;
70305
70430
  }
70306
70431
  const [propValue = "", propUnit = ""] = value !== void 0 ? split$1(value) : [];
70307
- const propAutoUnit = autoValue ? getUnit(autoValue) : "";
70308
70432
  const preferredUnit = useMemo$1(
70309
70433
  () => getPreferredUnit(unitkey, unitTemplate || (context2 == null ? void 0 : context2.unitTemplate)),
70310
70434
  [unitkey, unitTemplate, context2 == null ? void 0 : context2.unitTemplate]
@@ -70317,39 +70441,15 @@ const UnitInput = ({
70317
70441
  toUnit: initDisplayUnit,
70318
70442
  unitkey,
70319
70443
  defaultFromUnit: propUnit,
70320
- doNotConvertValue
70444
+ doNotConvertValue: noConversion
70321
70445
  });
70322
- const { value: convertedAutoValue } = safeConvertValue({
70323
- value: autoValue,
70324
- toUnit: initDisplayUnit,
70325
- unitkey,
70326
- defaultFromUnit: propAutoUnit,
70327
- doNotConvertValue
70328
- });
70329
- const initDisplayLayer = convertedValue !== "" ? { value: convertedValue, unit: initDisplayUnit } : convertedAutoValue !== "" ? { value: convertedAutoValue, unit: initDisplayUnit } : { value: propValue, unit: propUnit };
70446
+ const initDisplayLayer = convertedValue !== "" ? { value: convertedValue, unit: initDisplayUnit } : { value: propValue, unit: propUnit };
70330
70447
  const [displayLayer, setDisplayLayer] = useState(initDisplayLayer);
70331
70448
  const derivedAllowEmpty = allowEmpty || (displayLayer == null ? void 0 : displayLayer.value) === void 0;
70332
- const [predefinedOptionsMenuState, setPredefinedOptionsMenuState] = useState(
70333
- initialPredefinedOption ? PredefinedOptionsMenuState.PREDEFINED : PredefinedOptionsMenuState.CUSTOM
70334
- );
70335
- const isAutoValue = propValue === "" && autoValue;
70336
- const disableInternalErrorValidationMessages = !!(disabledValidation || autoValue);
70337
- const foundPredefinedMenuOption = predefinedOptions && predefinedOptions.find((el2) => {
70338
- if (!(el2 == null ? void 0 : el2.value)) return;
70339
- if (selectedPredefinedOptionKey) {
70340
- return selectedPredefinedOptionKey === el2.valueKey;
70341
- }
70342
- const predefinedMenuItemUnit = isValueWithUnit(el2.value) ? getUnit(el2.value) : "";
70343
- const { value: resultValue = value } = convertUnit({
70344
- value: String(value),
70345
- unitkey,
70346
- toUnit: predefinedMenuItemUnit
70347
- });
70348
- return withUnit(resultValue, predefinedMenuItemUnit) === el2.value;
70349
- });
70449
+ const disableInternalErrorValidationMessages = !!disabledValidation;
70350
70450
  const getAlternativeUnits = () => {
70351
- const valueToList = isAutoValue ? autoValue : value ?? "";
70352
- const initToUnit = displayLayer.unit;
70451
+ const valueToList = value ?? "";
70452
+ const initToUnit = displayLayer == null ? void 0 : displayLayer.unit;
70353
70453
  const shouldConvert = checkConversion({
70354
70454
  value: valueToList,
70355
70455
  unitkey,
@@ -70407,7 +70507,7 @@ const UnitInput = ({
70407
70507
  toUnit: initDisplayUnit,
70408
70508
  unitkey,
70409
70509
  defaultFromUnit: propUnit,
70410
- doNotConvertValue
70510
+ doNotConvertValue: noConversion
70411
70511
  });
70412
70512
  newValue = resultValue;
70413
70513
  newUnit = initDisplayUnit;
@@ -70417,96 +70517,29 @@ const UnitInput = ({
70417
70517
  toUnit: displayLayer.unit,
70418
70518
  unitkey,
70419
70519
  defaultFromUnit: propUnit,
70420
- doNotConvertValue
70520
+ doNotConvertValue: noConversion
70421
70521
  });
70422
70522
  newValue = resultValue;
70423
70523
  newUnit = displayLayer.unit;
70424
70524
  }
70425
- if (newValue !== void 0) {
70525
+ if (newValue !== void 0 && !initialPredefinedOption) {
70426
70526
  setDisplayLayer({ value: newValue, unit: newUnit });
70427
70527
  }
70428
- if (predefinedOptions) {
70429
- if (foundPredefinedMenuOption && shouldLinkAutomaticly) {
70430
- setPredefinedOptionsMenuState(PredefinedOptionsMenuState.PREDEFINED);
70431
- } else {
70432
- setPredefinedOptionsMenuState(PredefinedOptionsMenuState.CUSTOM);
70433
- }
70434
- }
70435
70528
  }
70436
- }, [initDisplayUnit, value, error2, shouldLinkAutomaticly]);
70529
+ }, [initDisplayUnit, value, error2]);
70437
70530
  const alternativeUnits = getAlternativeUnits();
70438
70531
  const displayUnitLabel = label$b(displayLayer.unit) || displayLayer.unit || "";
70439
70532
  const noConvert = noConversion || !alternativeUnits || alternativeUnits && alternativeUnits.length === 1;
70440
70533
  const stringName = getStringName(name2);
70441
- let sectionsPredefinedMenu;
70442
- const createPredefinedOption = (el2) => {
70443
- const elementValue = (el2 == null ? void 0 : el2.value) ? el2.value : "";
70444
- const [value2 = "", unit2 = ""] = isValueWithUnit(elementValue) ? split$1(elementValue) : [elementValue];
70445
- return {
70446
- type: MenuType.OPTION,
70447
- inline: true,
70448
- onClick: () => {
70449
- const validation = validateNumber(value2);
70450
- if (validation.valid && !disabled2) {
70451
- setPredefinedOptionsMenuState(PredefinedOptionsMenuState.PREDEFINED);
70452
- onChange({
70453
- target: {
70454
- value: elementValue,
70455
- name: typeof name2 === "string" ? name2 : (name2 == null ? void 0 : name2.fieldName) || "",
70456
- predefinedSelected: true,
70457
- predefinedOption: el2
70458
- }
70459
- });
70460
- }
70461
- },
70462
- label: /* @__PURE__ */ jsxs(Fragment$1, { children: [
70463
- /* @__PURE__ */ jsx(Text, { children: el2.label }),
70464
- /* @__PURE__ */ jsx(Spacer, { width: "20px", height: "0" })
70465
- ] }),
70466
- description: isValueWithUnit(elementValue) ? `${value2} ${unit2}` : elementValue,
70467
- selected: foundPredefinedMenuOption === el2 && predefinedOptionsMenuState === PredefinedOptionsMenuState.PREDEFINED
70468
- };
70469
- };
70470
- sectionsPredefinedMenu = [
70471
- {
70472
- type: MenuType.OPTION,
70473
- inline: true,
70474
- onClick: () => {
70475
- if (predefinedOptionsMenuState !== PredefinedOptionsMenuState.CUSTOM && !disabled2) {
70476
- onChange({
70477
- target: {
70478
- value: withUnit(displayLayer.value, displayLayer.unit),
70479
- name: typeof name2 === "string" ? name2 : (name2 == null ? void 0 : name2.fieldName) || ""
70480
- }
70481
- });
70482
- }
70483
- },
70484
- label: "Custom",
70485
- selected: predefinedOptionsMenuState === PredefinedOptionsMenuState.CUSTOM
70486
- }
70487
- ];
70488
- if (predefinedOptions == null ? void 0 : predefinedOptions.length) {
70489
- const dynamicOptions = predefinedOptions.map(createPredefinedOption);
70490
- sectionsPredefinedMenu = [...sectionsPredefinedMenu, ...dynamicOptions];
70491
- }
70492
70534
  const getPlaceholder = (placeholder22) => {
70493
- if (isAutoValue) {
70494
- const { value: resultValue } = safeConvertValue({
70495
- value: autoValue,
70496
- toUnit: displayLayer.unit,
70497
- unitkey,
70498
- defaultFromUnit: propAutoUnit,
70499
- doNotConvertValue
70500
- });
70501
- return resultValue;
70502
- } else if (isValueWithUnit(placeholder22)) {
70535
+ if (isValueWithUnit(placeholder22)) {
70503
70536
  const placeholderUnit = getUnit(placeholder22);
70504
70537
  const { value: resultValue } = safeConvertValue({
70505
70538
  value: placeholder22,
70506
70539
  toUnit: displayLayer.unit,
70507
70540
  unitkey,
70508
70541
  defaultFromUnit: placeholderUnit,
70509
- doNotConvertValue
70542
+ doNotConvertValue: noConversion
70510
70543
  });
70511
70544
  return resultValue;
70512
70545
  }
@@ -70518,32 +70551,31 @@ const UnitInput = ({
70518
70551
  className: predefinedOptionsMenuState === PredefinedOptionsMenuState.PREDEFINED ? cx$2(styles.predefinedMenuActive) : "",
70519
70552
  children: /* @__PURE__ */ jsxs(InputGroup, { small: small2, width: width2, children: [
70520
70553
  predefinedOptions && /* @__PURE__ */ jsx(
70521
- Tooltip,
70554
+ PredefinedOptionsMenu,
70522
70555
  {
70523
- text: predefinedOptionsMenuState === PredefinedOptionsMenuState.PREDEFINED ? foundPredefinedMenuOption == null ? void 0 : foundPredefinedMenuOption.label : "",
70524
- children: /* @__PURE__ */ jsx(
70525
- Menu,
70526
- {
70527
- maxHeight: 380,
70528
- groupOrder: "first",
70529
- testId: testId && `${testId}-predefined-menu`,
70530
- disabled: disabled2,
70531
- menu: {
70532
- colored: true,
70533
- trigger: "Component",
70534
- component: /* @__PURE__ */ jsx(
70535
- Button$1,
70536
- {
70537
- groupOrder: "first",
70538
- active: predefinedOptionsMenuState === PredefinedOptionsMenuState.PREDEFINED,
70539
- icon: predefinedOptionsMenuState === PredefinedOptionsMenuState.PREDEFINED ? "link" : "unlink"
70540
- }
70541
- ),
70542
- small: small2,
70543
- sections: sectionsPredefinedMenu
70544
- }
70556
+ testId,
70557
+ unitTemplate: unitTemplate || (context2 == null ? void 0 : context2.unitTemplate),
70558
+ unitkey,
70559
+ predefinedOptions,
70560
+ displayLayer,
70561
+ onPredefinedOptionSelect: (newValue, option2) => {
70562
+ if (!(option2 == null ? void 0 : option2.linked)) {
70563
+ onChange({
70564
+ target: {
70565
+ value: newValue,
70566
+ name: stringName
70567
+ }
70568
+ });
70569
+ setDisplayLayer({
70570
+ value: newValue,
70571
+ unit: getUnit(newValue) || displayLayer.unit
70572
+ });
70545
70573
  }
70546
- )
70574
+ },
70575
+ disabled: disabled2,
70576
+ initialPredefinedOption,
70577
+ predefinedOptionsMenuState,
70578
+ setPredefinedOptionsMenuState
70547
70579
  }
70548
70580
  ),
70549
70581
  /* @__PURE__ */ jsx("div", { className: styles.inputWrapper, children: /* @__PURE__ */ jsx(
@@ -70590,7 +70622,7 @@ const UnitInput = ({
70590
70622
  inline: true,
70591
70623
  onClick: (evt) => {
70592
70624
  evt.stopPropagation();
70593
- onClickUnit(isAutoValue ? "" : value2, altUnit);
70625
+ onClickUnit(value2, altUnit);
70594
70626
  },
70595
70627
  description: displayUnit,
70596
70628
  selected: displayUnit === displayUnitLabel,