@invoicebox/ui 0.1.0-alpha.63 → 0.1.0-alpha.65

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.
@@ -1,2 +1,3 @@
1
1
  import { TCountryRule, TSupportedCountries } from './types';
2
+ export declare const MASK_DIGIT_ITEM = "9";
2
3
  export declare const allCountriesPhoneRules: Record<TSupportedCountries, TCountryRule>;
@@ -3,7 +3,8 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.allCountriesPhoneRules = void 0;
6
+ exports.allCountriesPhoneRules = exports.MASK_DIGIT_ITEM = void 0;
7
+ var MASK_DIGIT_ITEM = exports.MASK_DIGIT_ITEM = '9';
7
8
  var allCountriesPhoneRules = exports.allCountriesPhoneRules = {
8
9
  RUS: {
9
10
  placeholder: '+7 (XXX) XXX-XX-XX',
@@ -22,13 +22,14 @@ var useValidateInitialValue = exports.useValidateInitialValue = function useVali
22
22
  (0, _react.useEffect)(function () {
23
23
  if (isInitialValidateDone) return;
24
24
  if (initialValue) {
25
+ var notFormattedValue = _phoneInputLogic.phoneInputLogic.getOnlyNumbersFromString(initialValue);
25
26
  var currentCountry = Object.entries(currentCountriesPhoneRules).find(function (item) {
26
- return _phoneInputLogic.phoneInputLogic.getIsHaveStartSequenceInString(initialValue, item[1].startSubsequence);
27
+ return _phoneInputLogic.phoneInputLogic.getIsHaveStartSequenceInString(notFormattedValue, item[1].startSubsequence);
27
28
  });
28
29
  var _countryCode = currentCountry ? currentCountry[0] : _types.RUS_COUNTRY_CODE;
29
- var isValidInputValue = _phoneInputLogic.phoneInputLogic.getIsValidPhoneInput(initialValue, currentCountry ? currentCountry[1] : _constants.allCountriesPhoneRules.RUS);
30
+ var isValidInputValue = _phoneInputLogic.phoneInputLogic.getIsValidPhoneInput(notFormattedValue, currentCountry ? currentCountry[1] : _constants.allCountriesPhoneRules.RUS);
30
31
  if (isValidInputValue) {
31
- onValid(initialValue, _countryCode);
32
+ onValid(notFormattedValue, _countryCode);
32
33
  } else {
33
34
  onUnValid(_countryCode);
34
35
  }
@@ -86,23 +86,38 @@ var PhoneInput = exports.PhoneInput = function PhoneInput(_ref) {
86
86
  setCountry(countryCode);
87
87
  }, [onChange]);
88
88
  (0, _useValidateInitialValue.useValidateInitialValue)(value, currentCountriesPhoneRules, handleValid, handleUnValid);
89
- var handleInputValueChange = (0, _react.useCallback)(function (currentInputValue) {
90
- var notFormattedValue = _phoneInputLogic.phoneInputLogic.getOnlyNumbersFromString(currentInputValue);
89
+
90
+ // хак для того чтобы сдвигать каретку в случае если при вводе "9" надо заменить на "79", каретка не двигается сама и остаётся между "7" и "9" без этой функции
91
+ var moveCaretByValue = (0, _react.useCallback)(function (event) {
92
+ return function (positionCaret) {
93
+ setTimeout(function () {
94
+ event.target.setSelectionRange(positionCaret, positionCaret);
95
+ }, 0);
96
+ };
97
+ }, []);
98
+ var getIsValidValue = (0, _react.useCallback)(function (currentValue) {
91
99
  var currentCountry = currentCountriesPhoneRules[country];
92
100
  var newCountry = Object.entries(currentCountriesPhoneRules).find(function (item) {
93
- return _phoneInputLogic.phoneInputLogic.getIsHaveStartSequenceInString(notFormattedValue, item[1].startSubsequence) && currentCountry.flag !== item[1].flag;
101
+ return _phoneInputLogic.phoneInputLogic.getIsHaveStartSequenceInString(currentValue, item[1].startSubsequence) && currentCountry.flag !== item[1].flag;
94
102
  });
95
103
  var isNeedChangeCountry = newCountry && currentCountry.startSubsequence !== newCountry[1].startSubsequence;
96
- if (isNeedChangeCountry && newCountry) {
104
+ var isNeedAndCanChangeCountry = isNeedChangeCountry && !!newCountry;
105
+ if (isNeedAndCanChangeCountry) {
97
106
  handleChangeCountry(newCountry[0]);
98
107
  }
99
- var isValidInputValue = _phoneInputLogic.phoneInputLogic.getIsValidPhoneInput(notFormattedValue, isNeedChangeCountry && newCountry ? newCountry[1] : currentCountry);
108
+ return _phoneInputLogic.phoneInputLogic.getIsValidPhoneInput(currentValue, isNeedAndCanChangeCountry ? newCountry[1] : currentCountry);
109
+ }, [country, currentCountriesPhoneRules, handleChangeCountry]);
110
+ var handleInputValueChange = (0, _react.useCallback)(function (currentInputValue, event) {
111
+ var notFormattedValue = _phoneInputLogic.phoneInputLogic.getOnlyNumbersFromString(currentInputValue);
112
+ var moveCaret = event ? moveCaretByValue(event) : undefined;
113
+ var intermediateValue = country === _types.RUS_COUNTRY_CODE ? _phoneInputLogic.phoneInputLogic.formatRusPhoneToUnifiedView(notFormattedValue, moveCaret) : notFormattedValue;
114
+ var isValidInputValue = getIsValidValue(intermediateValue);
100
115
  setIsHaveInputError(!isValidInputValue);
101
- onChange(isValidInputValue ? notFormattedValue : '');
102
- setInputValue(notFormattedValue);
103
- }, [country, currentCountriesPhoneRules, handleChangeCountry, onChange]);
116
+ onChange(isValidInputValue ? intermediateValue : '');
117
+ setInputValue(intermediateValue);
118
+ }, [country, getIsValidValue, moveCaretByValue, onChange]);
104
119
  var handleChange = (0, _react.useCallback)(function (event) {
105
- handleInputValueChange(event.target.value);
120
+ handleInputValueChange(event.target.value, event);
106
121
  }, [handleInputValueChange]);
107
122
  var handleBlur = (0, _react.useCallback)(function (event) {
108
123
  blurHandler(event);
@@ -152,6 +167,7 @@ var PhoneInput = exports.PhoneInput = function PhoneInput(_ref) {
152
167
  hasError: hasError || (isFirstBlur ? false : isHaveInputError),
153
168
  inFocus: inFocus,
154
169
  name: name,
155
- paddingLeft: isHaveSelectCountries ? 75 : 18
170
+ paddingLeft: isHaveSelectCountries ? 75 : 18,
171
+ autoComplete: 'off'
156
172
  }))));
157
173
  };
@@ -1,5 +1,6 @@
1
1
  import { TCountryRule, TCountrySelectOption, TSupportedCountries } from './types';
2
2
  declare class PhoneInputLogic {
3
+ private getCaretPositionByRusValue;
3
4
  getIsValidPhoneInput(value: string, country: TCountryRule): boolean;
4
5
  getCountriesSelectOptions(countries?: Array<{
5
6
  label: string;
@@ -15,6 +16,7 @@ declare class PhoneInputLogic {
15
16
  [k: string]: TCountryRule;
16
17
  };
17
18
  getIsHaveStartSequenceInString(value: string, subsequence: string): boolean;
19
+ formatRusPhoneToUnifiedView(value: string, moveCaret?: (positionCaret: number) => void): string;
18
20
  getOnlyNumbersFromString(value: string): string;
19
21
  }
20
22
  export declare const phoneInputLogic: PhoneInputLogic;
@@ -19,6 +19,22 @@ var PhoneInputLogic = /*#__PURE__*/function () {
19
19
  _classCallCheck(this, PhoneInputLogic);
20
20
  }
21
21
  _createClass(PhoneInputLogic, [{
22
+ key: "getCaretPositionByRusValue",
23
+ value: function getCaretPositionByRusValue(value) {
24
+ var mask = _constants.allCountriesPhoneRules.RUS.mask.split('');
25
+ var counter = 0;
26
+ var position = 0;
27
+ mask.forEach(function (maskItem, index) {
28
+ if (maskItem === _constants.MASK_DIGIT_ITEM) {
29
+ counter += 1;
30
+ if (counter === value.length) {
31
+ position = index + 1;
32
+ }
33
+ }
34
+ });
35
+ return position;
36
+ }
37
+ }, {
22
38
  key: "getIsValidPhoneInput",
23
39
  value: function getIsValidPhoneInput(value, country) {
24
40
  return !!value.match(country.regexp);
@@ -58,6 +74,25 @@ var PhoneInputLogic = /*#__PURE__*/function () {
58
74
  value: function getIsHaveStartSequenceInString(value, subsequence) {
59
75
  return value.startsWith(subsequence);
60
76
  }
77
+
78
+ // '8' => '+7', '9' => '+79'
79
+ }, {
80
+ key: "formatRusPhoneToUnifiedView",
81
+ value: function formatRusPhoneToUnifiedView(value, moveCaret) {
82
+ var result = value;
83
+ if (value[0] === '8') {
84
+ result = "7".concat(value.slice(1));
85
+ }
86
+ if (value[0] === '9') {
87
+ var newValue = "79".concat(value.slice(1));
88
+ result = newValue;
89
+ if (moveCaret) {
90
+ // когда пользователь вводит номер с "9", мы подставляем в инпут "79", но каретка не переносится после "9", нужно это делать вручную
91
+ moveCaret(this.getCaretPositionByRusValue(newValue));
92
+ }
93
+ }
94
+ return result;
95
+ }
61
96
  }, {
62
97
  key: "getOnlyNumbersFromString",
63
98
  value: function getOnlyNumbersFromString(value) {
@@ -0,0 +1,5 @@
1
+ import { FC, ReactNode } from 'react';
2
+ export type TProps = {
3
+ children: ReactNode;
4
+ };
5
+ export declare const MiniAppWrapper: FC<TProps>;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.MiniAppWrapper = void 0;
8
+ var _react = _interopRequireDefault(require("react"));
9
+ var S = _interopRequireWildcard(require("./styles"));
10
+ 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); }
11
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
+ var MiniAppWrapper = exports.MiniAppWrapper = function MiniAppWrapper(_ref) {
14
+ var children = _ref.children;
15
+ return /*#__PURE__*/_react["default"].createElement(S.Wrapper, null, children);
16
+ };
@@ -0,0 +1,6 @@
1
+ import type { Meta, StoryObj } from '@storybook/react';
2
+ import { MiniAppWrapper } from '.';
3
+ declare const meta: Meta<typeof MiniAppWrapper>;
4
+ export default meta;
5
+ export declare const ContainerHasNoHeigth: StoryObj;
6
+ export declare const ContainerHasHeigth: StoryObj;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports["default"] = exports.ContainerHasNoHeigth = exports.ContainerHasHeigth = void 0;
7
+ var _ = require(".");
8
+ var meta = {
9
+ title: 'miniApp/MiniAppWrapper',
10
+ component: _.MiniAppWrapper,
11
+ tags: ['autodocs']
12
+ };
13
+ var _default = exports["default"] = meta;
14
+ var children = /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
15
+ style: {
16
+ background: 'antiquewhite',
17
+ padding: 15
18
+ }
19
+ }, "form"), /*#__PURE__*/React.createElement("div", {
20
+ style: {
21
+ background: 'aquamarine',
22
+ padding: 15
23
+ }
24
+ }, "footer"));
25
+ var ContainerHasNoHeigth = exports.ContainerHasNoHeigth = {
26
+ render: function render() {
27
+ return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(_.MiniAppWrapper, null, children));
28
+ }
29
+ };
30
+ var ContainerHasHeigth = exports.ContainerHasHeigth = {
31
+ render: function render() {
32
+ return /*#__PURE__*/React.createElement("div", {
33
+ style: {
34
+ height: 200
35
+ }
36
+ }, /*#__PURE__*/React.createElement(_.MiniAppWrapper, null, children));
37
+ }
38
+ };
@@ -0,0 +1,2 @@
1
+ /// <reference types="react" />
2
+ export declare const Wrapper: import("styled-components").IStyledComponent<"web", import("styled-components/dist/types").FastOmit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>>;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.Wrapper = void 0;
7
+ var _styledComponents = _interopRequireDefault(require("styled-components"));
8
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
9
+ var Wrapper = exports.Wrapper = _styledComponents["default"].div.withConfig({
10
+ displayName: "styles__Wrapper",
11
+ componentId: "sc-6hgexf-0"
12
+ })(["height:100%;display:flex;flex-direction:column;*:first-child{flex-grow:1;}"]);
package/dist/index.d.ts CHANGED
@@ -29,6 +29,7 @@ export { CountrySelect } from './components/form/CountrySelect';
29
29
  export type { TProps as TCountrySelectProps } from './components/form/CountrySelect';
30
30
  export { PhoneInput } from './components/form/PhoneInput';
31
31
  export type { TProps as TPhoneInputProps } from './components/form/PhoneInput';
32
+ export type { MiniAppWrapper } from './components/miniApp/MiniAppWrapper';
32
33
  export { PaletteProvider } from './palette';
33
34
  export type { TAbstractPalette } from './palette';
34
35
  export { useOutsideClick } from './hooks/useOutsideClick';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@invoicebox/ui",
3
- "version": "0.1.0-alpha.63",
3
+ "version": "0.1.0-alpha.65",
4
4
  "description": "Invoicebox UI",
5
5
  "license": "Apache License 2.0",
6
6
  "scripts": {