@synerise/ds-input-number 0.7.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,25 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [0.8.1](https://github.com/Synerise/synerise-design/compare/@synerise/ds-input-number@0.8.0...@synerise/ds-input-number@0.8.1) (2023-03-20)
7
+
8
+ **Note:** Version bump only for package @synerise/ds-input-number
9
+
10
+
11
+
12
+
13
+
14
+ # [0.8.0](https://github.com/Synerise/synerise-design/compare/@synerise/ds-input-number@0.7.0...@synerise/ds-input-number@0.8.0) (2023-03-08)
15
+
16
+
17
+ ### Features
18
+
19
+ * **data-format:** using data format tools in ds components ([ee52cba](https://github.com/Synerise/synerise-design/commit/ee52cbac5e798e048722aefb2e47a7058e0f4c9c))
20
+
21
+
22
+
23
+
24
+
6
25
  # [0.7.0](https://github.com/Synerise/synerise-design/compare/@synerise/ds-input-number@0.6.28...@synerise/ds-input-number@0.7.0) (2023-03-06)
7
26
 
8
27
 
@@ -1,4 +1,4 @@
1
- var _excluded = ["label", "description", "errorText", "raw", "error", "prefixel", "suffixel", "style", "tooltip", "tooltipConfig"];
1
+ var _excluded = ["label", "description", "errorText", "raw", "error", "prefixel", "suffixel", "style", "tooltip", "tooltipConfig", "valueFormatOptions"];
2
2
 
3
3
  function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
4
4
 
@@ -9,8 +9,10 @@ import { v4 as uuid } from 'uuid';
9
9
  import '@synerise/ds-core/dist/js/style';
10
10
  import Tooltip from '@synerise/ds-tooltip';
11
11
  import Icon, { InfoFillS } from '@synerise/ds-icon';
12
+ import { useDataFormat } from '@synerise/ds-data-format';
12
13
  import "./style/index.css";
13
14
  import * as S from './InputNumber.styles';
15
+ import { formatNumber, parseFormattedNumber } from './utils/inputNumber.utils';
14
16
 
15
17
  var InputNumber = function InputNumber(_ref) {
16
18
  var label = _ref.label,
@@ -23,12 +25,25 @@ var InputNumber = function InputNumber(_ref) {
23
25
  style = _ref.style,
24
26
  tooltip = _ref.tooltip,
25
27
  tooltipConfig = _ref.tooltipConfig,
28
+ valueFormatOptions = _ref.valueFormatOptions,
26
29
  antdProps = _objectWithoutPropertiesLoose(_ref, _excluded);
27
30
 
31
+ var _useDataFormat = useDataFormat(),
32
+ formatValue = _useDataFormat.formatValue,
33
+ thousandDelimiter = _useDataFormat.thousandDelimiter,
34
+ decimalDelimiter = _useDataFormat.decimalDelimiter;
35
+
36
+ formatValue(new Date(), {});
28
37
  var id = React.useMemo(function () {
29
38
  return uuid();
30
39
  }, []);
31
40
  var showError = Boolean(error || errorText);
41
+ var formatter = React.useCallback(function (value) {
42
+ return formatNumber(value, formatValue, thousandDelimiter, decimalDelimiter, valueFormatOptions);
43
+ }, [formatValue, valueFormatOptions, thousandDelimiter, decimalDelimiter]);
44
+ var parser = React.useCallback(function (value) {
45
+ return parseFormattedNumber(value, formatValue, thousandDelimiter, decimalDelimiter);
46
+ }, [formatValue, thousandDelimiter, decimalDelimiter]);
32
47
  return /*#__PURE__*/React.createElement(S.InputNumberContainer, null, label && !raw && /*#__PURE__*/React.createElement(S.ContentAbove, null, /*#__PURE__*/React.createElement(S.Label, {
33
48
  htmlFor: id
34
49
  }, label, (tooltip || tooltipConfig) && /*#__PURE__*/React.createElement(Tooltip, _extends({
@@ -47,7 +62,10 @@ var InputNumber = function InputNumber(_ref) {
47
62
  id: id,
48
63
  error: showError,
49
64
  className: showError ? 'error' : undefined,
50
- autoComplete: "off"
65
+ autoComplete: "off",
66
+ formatter: formatter,
67
+ parser: parser,
68
+ decimalSeparator: decimalDelimiter
51
69
  })), !!suffixel && /*#__PURE__*/React.createElement(S.Suffixel, null, suffixel)), (showError || description) && !raw && /*#__PURE__*/React.createElement(S.ContentBelow, null, showError && /*#__PURE__*/React.createElement(S.ErrorText, null, errorText), description && /*#__PURE__*/React.createElement(S.Description, null, description)));
52
70
  };
53
71
 
@@ -1,6 +1,7 @@
1
1
  import * as React from 'react';
2
2
  import { InputNumberProps } from 'antd/lib/input-number';
3
3
  import type { TooltipProps } from '@synerise/ds-tooltip';
4
+ import { NumberToFormatOptions } from '@synerise/ds-data-format';
4
5
  export interface Props extends InputNumberProps {
5
6
  errorText?: React.ReactNode | string;
6
7
  label?: React.ReactNode | string;
@@ -11,4 +12,5 @@ export interface Props extends InputNumberProps {
11
12
  raw?: boolean;
12
13
  tooltip?: string;
13
14
  tooltipConfig?: TooltipProps;
15
+ valueFormatOptions?: NumberToFormatOptions;
14
16
  }
@@ -0,0 +1,3 @@
1
+ import { Delimiter } from '@synerise/ds-data-format';
2
+ export declare const MAXIMUM_FRACTION_DIGITS = 20;
3
+ export declare const NUMBER_DELIMITER: Delimiter;
@@ -0,0 +1,2 @@
1
+ export var MAXIMUM_FRACTION_DIGITS = 20;
2
+ export var NUMBER_DELIMITER = '.';
@@ -0,0 +1,4 @@
1
+ import { ReactText } from 'react';
2
+ import { NumberToFormatOptions, Delimiter } from '@synerise/ds-data-format';
3
+ export declare const formatNumber: (value: string | number | undefined, formatValue: (value: number, options: NumberToFormatOptions) => string, thousandDelimiter: Delimiter, decimalDelimiter: Delimiter, valueFormatOptions?: NumberToFormatOptions | undefined) => string;
4
+ export declare const parseFormattedNumber: (value: string | undefined, formatValue: (value: number, options: NumberToFormatOptions) => string, thousandDelimiter: Delimiter, decimalDelimiter: Delimiter) => ReactText;
@@ -0,0 +1,61 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
2
+
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
4
+
5
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+
7
+ import { MAXIMUM_FRACTION_DIGITS, NUMBER_DELIMITER } from '../constants/inputNumber.constants'; // input 1: not formatted number string (on input change)
8
+ // input 2: not formatted number (on blur)
9
+ // output: formatted number string with decimal char
10
+
11
+ export var formatNumber = function formatNumber(value, formatValue, thousandDelimiter, decimalDelimiter, valueFormatOptions) {
12
+ if (value === undefined || value === '') return '';
13
+
14
+ var formatOptions = _objectSpread({
15
+ maximumFractionDigits: MAXIMUM_FRACTION_DIGITS
16
+ }, valueFormatOptions);
17
+
18
+ var notationDecimalChar = '';
19
+ var result = '';
20
+
21
+ if (typeof value === 'number') {
22
+ result = formatValue(value, formatOptions);
23
+ } else {
24
+ var lastChar = value == null ? void 0 : value.slice(-1);
25
+
26
+ if (lastChar === NUMBER_DELIMITER) {
27
+ notationDecimalChar = decimalDelimiter;
28
+ }
29
+
30
+ var numberResult = Number(value);
31
+
32
+ if (Number.isNaN(numberResult)) {
33
+ return '';
34
+ }
35
+
36
+ result = formatValue(numberResult, formatOptions);
37
+ result = "" + result + notationDecimalChar;
38
+ }
39
+
40
+ return result;
41
+ }; // input: formatted number string
42
+ // output: not formatted number string with decimal char
43
+
44
+ export var parseFormattedNumber = function parseFormattedNumber(value, formatValue, thousandDelimiter, decimalDelimiter) {
45
+ if (value === undefined || value === '') {
46
+ return '';
47
+ }
48
+
49
+ var result = value.split(thousandDelimiter).join('');
50
+
51
+ if (decimalDelimiter !== NUMBER_DELIMITER) {
52
+ result = result.replace(decimalDelimiter, NUMBER_DELIMITER);
53
+ }
54
+
55
+ if ((result.match(new RegExp("\\" + NUMBER_DELIMITER, 'g')) || []).length > 1) {
56
+ var lastDecimalDelimiterIndex = result.lastIndexOf(NUMBER_DELIMITER);
57
+ result = result.slice(0, lastDecimalDelimiterIndex);
58
+ }
59
+
60
+ return result;
61
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@synerise/ds-input-number",
3
- "version": "0.7.0",
3
+ "version": "0.8.1",
4
4
  "description": "Input-Number UI Component for the Synerise Design System",
5
5
  "license": "ISC",
6
6
  "repository": "Synerise/synerise-design",
@@ -34,7 +34,7 @@
34
34
  "dependencies": {
35
35
  "@synerise/ds-icon": "^0.53.0",
36
36
  "@synerise/ds-tooltip": "^0.13.0",
37
- "@synerise/ds-utils": "^0.24.1",
37
+ "@synerise/ds-utils": "^0.24.3",
38
38
  "uuid": "^7.0.2"
39
39
  },
40
40
  "peerDependencies": {
@@ -45,5 +45,5 @@
45
45
  "devDependencies": {
46
46
  "@types/uuid": "7.0.0"
47
47
  },
48
- "gitHead": "a9c9628df732abd42421880fd1aa61b7f7c580ee"
48
+ "gitHead": "41419f123b90134def2fecde455da13946fc2c0e"
49
49
  }