@cloud-ru/uikit-product-fields-predefined 0.16.0 → 0.17.0
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 +11 -0
- package/dist/cjs/components/FieldPhone/FieldPhone.js +31 -0
- package/dist/cjs/components/FieldPhone/__tests__/constants.d.ts +29 -0
- package/dist/cjs/components/FieldPhone/__tests__/constants.js +175 -1
- package/dist/cjs/components/FieldPhone/__tests__/handleAutoInsert.spec.d.ts +1 -0
- package/dist/cjs/components/FieldPhone/__tests__/handleAutoInsert.spec.js +63 -0
- package/dist/cjs/components/FieldPhone/utils.d.ts +7 -0
- package/dist/cjs/components/FieldPhone/utils.js +30 -1
- package/dist/esm/components/FieldPhone/FieldPhone.js +32 -1
- package/dist/esm/components/FieldPhone/__tests__/constants.d.ts +29 -0
- package/dist/esm/components/FieldPhone/__tests__/constants.js +174 -0
- package/dist/esm/components/FieldPhone/__tests__/handleAutoInsert.spec.d.ts +1 -0
- package/dist/esm/components/FieldPhone/__tests__/handleAutoInsert.spec.js +61 -0
- package/dist/esm/components/FieldPhone/utils.d.ts +7 -0
- package/dist/esm/components/FieldPhone/utils.js +28 -0
- package/dist/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/components/FieldPhone/FieldPhone.tsx +35 -1
- package/src/components/FieldPhone/__tests__/constants.ts +175 -0
- package/src/components/FieldPhone/__tests__/handleAutoInsert.spec.ts +87 -0
- package/src/components/FieldPhone/utils.ts +41 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { ALL_COUNTRY_CODES } from '../countries';
|
|
3
|
+
import { handleAutoInsert } from '../utils';
|
|
4
|
+
import { handleAutoInsertCases } from './constants';
|
|
5
|
+
const OPTIONS = ALL_COUNTRY_CODES.map(c => ({
|
|
6
|
+
id: c.value,
|
|
7
|
+
iso2: c.iso2,
|
|
8
|
+
mask: c.mask,
|
|
9
|
+
content: {
|
|
10
|
+
caption: c.caption,
|
|
11
|
+
option: c.value,
|
|
12
|
+
},
|
|
13
|
+
beforeContent: null,
|
|
14
|
+
}));
|
|
15
|
+
const byId = (id) => {
|
|
16
|
+
const found = OPTIONS.find(c => c.id === id);
|
|
17
|
+
if (!found)
|
|
18
|
+
throw new Error(`Country not found in OPTIONS: ${id}`);
|
|
19
|
+
return found;
|
|
20
|
+
};
|
|
21
|
+
const casesNoCalls = handleAutoInsertCases.filter((c) => !('expectedValue' in c) && !('expectedLengthFromCountryId' in c));
|
|
22
|
+
const casesExact = handleAutoInsertCases.filter((c) => 'expectedValue' in c);
|
|
23
|
+
describe('handleAutoInsert — no calls', () => {
|
|
24
|
+
casesNoCalls.forEach(tc => {
|
|
25
|
+
it(`${tc.name}`, () => {
|
|
26
|
+
const onValueChange = vi.fn();
|
|
27
|
+
const onCountryChange = vi.fn();
|
|
28
|
+
handleAutoInsert({
|
|
29
|
+
raw: tc.raw,
|
|
30
|
+
onValueChange,
|
|
31
|
+
onCountryChange,
|
|
32
|
+
country: byId(tc.currentId),
|
|
33
|
+
options: OPTIONS,
|
|
34
|
+
});
|
|
35
|
+
expect(onCountryChange).not.toHaveBeenCalled();
|
|
36
|
+
expect(onValueChange).not.toHaveBeenCalled();
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
describe('handleAutoInsert — exact value', () => {
|
|
41
|
+
casesExact.forEach(tc => {
|
|
42
|
+
it(`${tc.name}`, () => {
|
|
43
|
+
var _a, _b;
|
|
44
|
+
const onValueChange = vi.fn();
|
|
45
|
+
const onCountryChange = vi.fn();
|
|
46
|
+
handleAutoInsert({
|
|
47
|
+
raw: tc.raw,
|
|
48
|
+
onValueChange,
|
|
49
|
+
onCountryChange,
|
|
50
|
+
country: byId(tc.currentId),
|
|
51
|
+
options: OPTIONS,
|
|
52
|
+
});
|
|
53
|
+
expect(onValueChange).toHaveBeenCalledTimes(1);
|
|
54
|
+
expect(onValueChange).toHaveBeenCalledWith(tc.expectedValue);
|
|
55
|
+
const expectedCountryCalls = tc.expectedCountryId ? 1 : 0;
|
|
56
|
+
expect(onCountryChange).toHaveBeenCalledTimes(expectedCountryCalls);
|
|
57
|
+
const actualCountryId = (_b = (_a = onCountryChange.mock.calls[0]) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.id;
|
|
58
|
+
expect(actualCountryId).toBe(tc.expectedCountryId);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -5,3 +5,10 @@ export declare const formatPhoneNumber: <T extends {
|
|
|
5
5
|
mask: string;
|
|
6
6
|
}>(phone: string, countries: readonly T[]) => string;
|
|
7
7
|
export declare function detectCountryByPhone(text: string, options: FieldPhoneOptionsProps[]): FieldPhoneOptionsProps | undefined;
|
|
8
|
+
export declare const handleAutoInsert: ({ raw, onValueChange, onCountryChange, country, options, }: {
|
|
9
|
+
raw: string;
|
|
10
|
+
onValueChange: (str: string) => void;
|
|
11
|
+
onCountryChange: (country: FieldPhoneOptionsProps) => void;
|
|
12
|
+
country?: FieldPhoneOptionsProps;
|
|
13
|
+
options: FieldPhoneOptionsProps[];
|
|
14
|
+
}) => void;
|
|
@@ -49,3 +49,31 @@ export function detectCountryByPhone(text, options) {
|
|
|
49
49
|
}
|
|
50
50
|
return options.find(opt => opt.iso2 === regionCode);
|
|
51
51
|
}
|
|
52
|
+
export const handleAutoInsert = ({ raw, onValueChange, onCountryChange, country, options, }) => {
|
|
53
|
+
var _a, _b, _c, _d, _e, _f;
|
|
54
|
+
if (!raw)
|
|
55
|
+
return;
|
|
56
|
+
const parsed = parsePhoneNumber(raw);
|
|
57
|
+
const ok = parsed.valid || parsed.possible;
|
|
58
|
+
if (!ok)
|
|
59
|
+
return;
|
|
60
|
+
const detected = detectCountryByPhone(raw, options);
|
|
61
|
+
if (!detected)
|
|
62
|
+
return;
|
|
63
|
+
let national = (_a = parsed.number) === null || _a === void 0 ? void 0 : _a.significant.replace(/\D/g, '');
|
|
64
|
+
if (!national)
|
|
65
|
+
return;
|
|
66
|
+
const nextNationalLength = ((_c = (_b = detected.mask) === null || _b === void 0 ? void 0 : _b.match(/X/g)) !== null && _c !== void 0 ? _c : []).length;
|
|
67
|
+
if (nextNationalLength && national.length > nextNationalLength) {
|
|
68
|
+
national = national.slice(-nextNationalLength);
|
|
69
|
+
}
|
|
70
|
+
const fullDigits = ((_e = (_d = parsed.number) === null || _d === void 0 ? void 0 : _d.e164) !== null && _e !== void 0 ? _e : raw).replace(/\D/g, '');
|
|
71
|
+
const countryCodeDigits = String((_f = parsed.countryCode) !== null && _f !== void 0 ? _f : '');
|
|
72
|
+
const expected = countryCodeDigits.length + nextNationalLength;
|
|
73
|
+
if (fullDigits.length < expected)
|
|
74
|
+
return;
|
|
75
|
+
if (detected.id !== (country === null || country === void 0 ? void 0 : country.id)) {
|
|
76
|
+
onCountryChange(detected);
|
|
77
|
+
}
|
|
78
|
+
onValueChange(national);
|
|
79
|
+
};
|