orc-shared 5.8.0-dev.7 → 5.8.0-dev.8

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.
@@ -8,6 +8,7 @@ var _InputBase = _interopRequireDefault(require("@material-ui/core/InputBase"));
8
8
  var _InputBaseProps = _interopRequireWildcard(require("./InputBaseProps"));
9
9
  var _classnames = _interopRequireDefault(require("classnames"));
10
10
  var _reactNumberFormat = require("react-number-format");
11
+ var _inputHelper = require("../../../utils/inputHelper");
11
12
  var _excluded = ["inputRef", "onChange"];
12
13
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
14
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
@@ -227,11 +228,13 @@ var InputBase = function InputBase(_ref) {
227
228
  timerId.current = null;
228
229
  }
229
230
  if (isAdvancedNumericInput) {
231
+ var _numericInputProps$de2;
230
232
  var formattingProps = _objectSpread({}, numericInputProps);
231
233
  delete formattingProps.blurFormattingSkipFixedDecimalScale;
232
234
  if ((numericInputProps == null ? void 0 : numericInputProps.blurFormattingSkipFixedDecimalScale) !== true) {
233
235
  formattingProps.fixedDecimalScale = true;
234
236
  }
237
+ updateValue = (0, _inputHelper.trimSpacesAndLeadingZeros)(updateValue, (_numericInputProps$de2 = numericInputProps == null ? void 0 : numericInputProps.defaultValue) != null ? _numericInputProps$de2 : "");
235
238
  updateValue = (0, _reactNumberFormat.numericFormatter)(updateValue, formattingProps);
236
239
  }
237
240
  update(updateValue, metadata);
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.trimSpacesAndLeadingZeros = exports.limitNumericValueLength = void 0;
5
+ (function () {
6
+ var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
7
+ enterModule && enterModule(module);
8
+ })();
9
+ (function () {
10
+ var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
11
+ enterModule && enterModule(module);
12
+ })();
13
+ var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
14
+ return a;
15
+ };
16
+ var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
17
+ return a;
18
+ };
19
+ var trimSpacesAndLeadingZeros = exports.trimSpacesAndLeadingZeros = function trimSpacesAndLeadingZeros(value, fallback) {
20
+ if (fallback === void 0) {
21
+ fallback = "";
22
+ }
23
+ var trimmed = (value != null ? value : "").trim().replace(/^0+/, "");
24
+ return trimmed === "" ? fallback : limitNumericValueLength(trimmed);
25
+ };
26
+ var limitNumericValueLength = exports.limitNumericValueLength = function limitNumericValueLength(value, maximum) {
27
+ if (maximum === void 0) {
28
+ maximum = 9;
29
+ }
30
+ return value.substring(0, maximum);
31
+ };
32
+ ;
33
+ (function () {
34
+ var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
35
+ if (!reactHotLoader) {
36
+ return;
37
+ }
38
+ reactHotLoader.register(trimSpacesAndLeadingZeros, "trimSpacesAndLeadingZeros", "/home/vsts/work/1/s/src/utils/inputHelper.js");
39
+ reactHotLoader.register(limitNumericValueLength, "limitNumericValueLength", "/home/vsts/work/1/s/src/utils/inputHelper.js");
40
+ })();
41
+ ;
42
+ (function () {
43
+ var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
44
+ leaveModule && leaveModule(module);
45
+ })();
46
+ ;
47
+ (function () {
48
+ var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
49
+ if (!reactHotLoader) {
50
+ return;
51
+ }
52
+ reactHotLoader.register(trimSpacesAndLeadingZeros, "trimSpacesAndLeadingZeros", "/home/vsts/work/1/s/src/utils/inputHelper.js");
53
+ reactHotLoader.register(limitNumericValueLength, "limitNumericValueLength", "/home/vsts/work/1/s/src/utils/inputHelper.js");
54
+ })();
55
+ ;
56
+ (function () {
57
+ var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
58
+ leaveModule && leaveModule(module);
59
+ })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orc-shared",
3
- "version": "5.8.0-dev.7",
3
+ "version": "5.8.0-dev.8",
4
4
  "description": "Shared code for Orckestra applications",
5
5
  "main": "./src/index.js",
6
6
  "exports": {
@@ -4,6 +4,7 @@ import InputBaseMUI from "@material-ui/core/InputBase";
4
4
  import InputBaseProps, { isInputProps } from "./InputBaseProps";
5
5
  import classNames from "classnames";
6
6
  import { NumericFormat, numericFormatter } from "react-number-format";
7
+ import { trimSpacesAndLeadingZeros } from "../../../utils/inputHelper";
7
8
 
8
9
  export const useStyles = makeStyles(theme => ({
9
10
  container: {
@@ -207,6 +208,7 @@ const InputBase = ({ inputProps }) => {
207
208
  formattingProps.fixedDecimalScale = true;
208
209
  }
209
210
 
211
+ updateValue = trimSpacesAndLeadingZeros(updateValue, numericInputProps?.defaultValue ?? "");
210
212
  updateValue = numericFormatter(updateValue, formattingProps);
211
213
  }
212
214
 
@@ -643,6 +643,55 @@ describe("AdvancedNumericInput", () => {
643
643
  expect(update, "to have calls satisfying", [{ args: ["12.20", metadata] }]);
644
644
  });
645
645
 
646
+ it("OnBlur trims spaces and leading zeros of values", () => {
647
+ const inputProps = new InputBaseProps();
648
+ const aLabel = "aLabel";
649
+ const aValue = "012.2";
650
+ const metadata = {
651
+ test: "value",
652
+ };
653
+
654
+ inputProps.set(InputBaseProps.propNames.update, update);
655
+ inputProps.set(InputBaseProps.propNames.value, aValue);
656
+ inputProps.set(InputBaseProps.propNames.label, aLabel);
657
+ inputProps.set(InputBaseProps.propNames.type, "AdvancedNumericInput");
658
+ inputProps.set(InputBaseProps.propNames.numericInputProps, { decimalScale: 1 });
659
+ inputProps.set(InputBaseProps.propNames.metadata, metadata);
660
+
661
+ const component = <InputBase inputProps={inputProps} />;
662
+ const mountedComponent = mount(component);
663
+ const input = mountedComponent.find("input");
664
+
665
+ input.simulate("blur", {});
666
+
667
+ // Update is called twice, once from onChangeHandler (from React) and once from onBlurInternal
668
+ expect(update, "to have calls satisfying", [{ args: ["12.2", metadata] }, { args: ["12.2", metadata] }]);
669
+ });
670
+
671
+ it("OnBlur use default value if trim spaces and leading zeroes returns empty ", () => {
672
+ const inputProps = new InputBaseProps();
673
+ const aLabel = "aLabel";
674
+ const aValue = "";
675
+ const metadata = {
676
+ test: "value",
677
+ };
678
+
679
+ inputProps.set(InputBaseProps.propNames.update, update);
680
+ inputProps.set(InputBaseProps.propNames.value, aValue);
681
+ inputProps.set(InputBaseProps.propNames.label, aLabel);
682
+ inputProps.set(InputBaseProps.propNames.type, "AdvancedNumericInput");
683
+ inputProps.set(InputBaseProps.propNames.numericInputProps, { decimalScale: 2, defaultValue: "84" });
684
+ inputProps.set(InputBaseProps.propNames.metadata, metadata);
685
+
686
+ const component = <InputBase inputProps={inputProps} />;
687
+ const mountedComponent = mount(component);
688
+ const input = mountedComponent.find("input");
689
+
690
+ input.simulate("blur", {});
691
+
692
+ expect(update, "to have calls satisfying", [{ args: ["84.00", metadata] }]);
693
+ });
694
+
646
695
  it("Onblur send a formatted string instead of a number", () => {
647
696
  const inputProps = new InputBaseProps();
648
697
  const aLabel = "aLabel";
@@ -0,0 +1,6 @@
1
+ export const trimSpacesAndLeadingZeros = (value, fallback = "") => {
2
+ const trimmed = (value ?? "").trim().replace(/^0+/, "");
3
+ return trimmed === "" ? fallback : limitNumericValueLength(trimmed);
4
+ };
5
+
6
+ export const limitNumericValueLength = (value, maximum = 9) => value.substring(0, maximum);
@@ -0,0 +1,31 @@
1
+ import { limitNumericValueLength, trimSpacesAndLeadingZeros } from "./inputHelper";
2
+
3
+ describe("Numeric Input Helper", () => {
4
+ it("trimSpacesAndLeadingZeros trims spaces and leading zeros", () => {
5
+ expect(trimSpacesAndLeadingZeros, "when called with", [" 00013 "], "to equal", "13");
6
+ });
7
+
8
+ it("trimSpacesAndLeadingZeros trims spaces and leading zeros returning fallback value", () => {
9
+ expect(trimSpacesAndLeadingZeros, "when called with", [" 000 ", "def"], "to equal", "def");
10
+ });
11
+
12
+ it("trimSpacesAndLeadingZeros should work on floating values", () => {
13
+ expect(trimSpacesAndLeadingZeros, "when called with", ["042.2", "def"], "to equal", "42.2");
14
+ });
15
+
16
+ it("trimSpacesAndLeadingZeros returns fallback on null value", () => {
17
+ expect(trimSpacesAndLeadingZeros, "when called with", [null, "def"], "to equal", "def");
18
+ });
19
+
20
+ it("trimSpacesAndLeadingZeros returns fallback on undefined value", () => {
21
+ expect(trimSpacesAndLeadingZeros, "when called with", [undefined, "def"], "to equal", "def");
22
+ });
23
+
24
+ it("limitNumericValueLength trims extra characters", () => {
25
+ expect(limitNumericValueLength, "when called with", ["01234567890"], "to equal", "012345678");
26
+ });
27
+
28
+ it("limitNumericValueLength trims extra characters with another maximum", () => {
29
+ expect(limitNumericValueLength, "when called with", ["01234567890", 5], "to equal", "01234");
30
+ });
31
+ });