pds-dev-kit-web 2.2.319-alpha.0 → 2.2.319-alpha.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.
|
@@ -19,7 +19,6 @@ var types_1 = require("../../../common/types");
|
|
|
19
19
|
var input_utils_1 = require("../input-utils");
|
|
20
20
|
var policy_context_1 = require("../policy-context");
|
|
21
21
|
var service_1 = require("../service");
|
|
22
|
-
var validator_1 = require("../validator");
|
|
23
22
|
function useNewPriceTextField(_a) {
|
|
24
23
|
var _b;
|
|
25
24
|
var name = _a.name, hintText = _a.hintText, defaultAmount = _a.defaultAmount, validation = _a.validation, _c = _a.state, state = _c === void 0 ? 'normal' : _c, _d = _a.useRequirementMode, useRequirementMode = _d === void 0 ? 'use' : _d, onChange = _a.onChange, onBlur = _a.onBlur, onFocus = _a.onFocus, onKeyDown = _a.onKeyDown, onKeyUp = _a.onKeyUp, onTarget = _a.onTarget, onCurrencyChange = _a.onCurrencyChange, onValueChange = _a.onValueChange;
|
|
@@ -111,24 +110,12 @@ function useNewPriceTextField(_a) {
|
|
|
111
110
|
else if (useRequirementMode === 'use') {
|
|
112
111
|
required = String(t('str_please_enter_price'));
|
|
113
112
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
});
|
|
119
|
-
}, max: function (value) {
|
|
120
|
-
return validator_1.PricingValidator.max((0, input_utils_1.toRawValue)(value), {
|
|
121
|
-
maxAmount: basePolicy.maxAmount,
|
|
122
|
-
currency: baseCurrency
|
|
123
|
-
});
|
|
124
|
-
}, format: function (value) {
|
|
125
|
-
return validator_1.PricingValidator.format((0, input_utils_1.toRawValue)(value), {
|
|
126
|
-
paymentUnit: basePolicy.paymentUnit,
|
|
127
|
-
currency: baseCurrency
|
|
128
|
-
});
|
|
129
|
-
} }, ((_a = validation === null || validation === void 0 ? void 0 : validation.validate) !== null && _a !== void 0 ? _a : {}));
|
|
113
|
+
// 문구는 react-i18next의 t로 만든다. bare i18next 싱글턴은 소비 앱에서 중첩 설치되면
|
|
114
|
+
// 초기화되지 않아 undefined를 돌려주므로(검증이 통과로 처리됨) 쓰지 않는다.
|
|
115
|
+
var translate = function (key, options) { return String(t(key, options)); };
|
|
116
|
+
var validate = __assign({ price: function (value) { return (0, input_utils_1.validatePriceAmount)(value, basePolicy, translate); } }, ((_a = validation === null || validation === void 0 ? void 0 : validation.validate) !== null && _a !== void 0 ? _a : {}));
|
|
130
117
|
return { required: required, validate: validate };
|
|
131
|
-
}, [validation, useRequirementMode, basePolicy,
|
|
118
|
+
}, [validation, useRequirementMode, basePolicy, t]);
|
|
132
119
|
var errorMessage = (_b = (0, types_1.getErrorByName)(errors, name)) === null || _b === void 0 ? void 0 : _b.message;
|
|
133
120
|
/** 값이 비어 있지 않을 때만 통화·환산값을 알린다 */
|
|
134
121
|
(0, react_1.useEffect)(function () {
|
|
@@ -15,3 +15,18 @@ export declare function sanitizePriceInput(input: string, decimalDigits: number)
|
|
|
15
15
|
export declare function normalizePriceOnBlur(raw: string, decimalDigits: number): string;
|
|
16
16
|
/** 표시용 포맷: 자릿수 고정 + 천 단위 콤마 */
|
|
17
17
|
export declare function formatAmount(amount: number | string, decimalDigits: number): string;
|
|
18
|
+
/** 검증에 필요한 통화 정책 부분집합 (policy-model의 CurrencyPolicy와 호환) */
|
|
19
|
+
export type PriceAmountPolicy = {
|
|
20
|
+
code: string;
|
|
21
|
+
decimalDigits: number;
|
|
22
|
+
minAmount: number;
|
|
23
|
+
maxAmount: number;
|
|
24
|
+
paymentUnit: number;
|
|
25
|
+
};
|
|
26
|
+
/** react-i18next의 t 와 같은 모양. bare i18next 싱글턴에 의존하지 않기 위해 주입받는다 */
|
|
27
|
+
export type PriceMessageTranslator = (key: string, options?: Record<string, unknown>) => string;
|
|
28
|
+
/**
|
|
29
|
+
* 가격 입력값을 정책(최소·최대·단위·소수 자릿수)으로 검증한다.
|
|
30
|
+
* @returns 오류 문구(i18n 적용) 또는 통과 시 undefined. 빈 값은 통과(required는 별도 처리)
|
|
31
|
+
*/
|
|
32
|
+
export declare function validatePriceAmount(value: string, policy: PriceAmountPolicy, t: PriceMessageTranslator): string | undefined;
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* 통화 정책(소수 자릿수)만 인자로 받고 React/RHF에 의존하지 않는다.
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.formatAmount = exports.normalizePriceOnBlur = exports.sanitizePriceInput = exports.groupDigits = exports.toRawValue = void 0;
|
|
7
|
+
exports.validatePriceAmount = exports.formatAmount = exports.normalizePriceOnBlur = exports.sanitizePriceInput = exports.groupDigits = exports.toRawValue = void 0;
|
|
8
8
|
/** 폼에 들어 있는 값을 콤마·공백 없는 숫자 문자열로 바꾼다. */
|
|
9
9
|
function toRawValue(formValue) {
|
|
10
10
|
if (formValue === null || formValue === undefined) {
|
|
@@ -65,3 +65,55 @@ function formatAmount(amount, decimalDigits) {
|
|
|
65
65
|
: fixed.replace(/(\d)(?=(\d{3})+$)/g, '$1,');
|
|
66
66
|
}
|
|
67
67
|
exports.formatAmount = formatAmount;
|
|
68
|
+
function getDecimalPlaces(value) {
|
|
69
|
+
var decimalStr = value.toString().split('.')[1];
|
|
70
|
+
return decimalStr ? decimalStr.length : 0;
|
|
71
|
+
}
|
|
72
|
+
function isDivisible(amount, unit) {
|
|
73
|
+
var precision = Math.pow(10, Math.max(getDecimalPlaces(amount), getDecimalPlaces(unit)));
|
|
74
|
+
var intAmount = Math.round(amount * precision);
|
|
75
|
+
var intUnit = Math.round(unit * precision);
|
|
76
|
+
return intAmount % intUnit === 0;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 가격 입력값을 정책(최소·최대·단위·소수 자릿수)으로 검증한다.
|
|
80
|
+
* @returns 오류 문구(i18n 적용) 또는 통과 시 undefined. 빈 값은 통과(required는 별도 처리)
|
|
81
|
+
*/
|
|
82
|
+
function validatePriceAmount(value, policy, t) {
|
|
83
|
+
var raw = toRawValue(value);
|
|
84
|
+
if (raw === '') {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
var amount = Number(raw);
|
|
88
|
+
if (Number.isNaN(amount)) {
|
|
89
|
+
return t('str_please_enter_price');
|
|
90
|
+
}
|
|
91
|
+
var currency = policy.code;
|
|
92
|
+
if (amount < policy.minAmount) {
|
|
93
|
+
return t('str_min_price', {
|
|
94
|
+
minPrice: formatAmount(policy.minAmount, policy.decimalDigits),
|
|
95
|
+
currency: currency
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
if (amount > policy.maxAmount) {
|
|
99
|
+
return t('str_max_price', {
|
|
100
|
+
maxPrice: formatAmount(policy.maxAmount, policy.decimalDigits),
|
|
101
|
+
currency: currency
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
var unitMessage = function () {
|
|
105
|
+
return t('str_price_unit', {
|
|
106
|
+
priceUnit: formatAmount(policy.paymentUnit, policy.decimalDigits),
|
|
107
|
+
currency: currency
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
var _a = raw.split('.'), fraction = _a[1];
|
|
111
|
+
if (fraction !== undefined && fraction.length > policy.decimalDigits) {
|
|
112
|
+
return unitMessage();
|
|
113
|
+
}
|
|
114
|
+
if (!isDivisible(amount, policy.paymentUnit)) {
|
|
115
|
+
return unitMessage();
|
|
116
|
+
}
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
exports.validatePriceAmount = validatePriceAmount;
|
|
@@ -35,18 +35,20 @@ function buildCurrencyPolicy(code, input) {
|
|
|
35
35
|
*/
|
|
36
36
|
function normalizeExchangeRates(baseCurrency, altCurrencies, input) {
|
|
37
37
|
var _a;
|
|
38
|
-
var _b;
|
|
39
38
|
var rates = input !== null && input !== void 0 ? input : {};
|
|
40
|
-
|
|
39
|
+
// API가 Decimal을 문자열("1500.0000000000")로 내려주는 경우가 있어 숫자로 강제 변환한다
|
|
40
|
+
var baseRateRaw = rates[baseCurrency];
|
|
41
|
+
var baseRate = baseRateRaw === undefined ? 1 : Number(baseRateRaw);
|
|
41
42
|
if (!Number.isFinite(baseRate) || baseRate <= 0) {
|
|
42
43
|
throw new Error("createPricingPolicy: invalid exchange rate for ".concat(baseCurrency));
|
|
43
44
|
}
|
|
44
45
|
var normalized = (_a = {}, _a[baseCurrency] = 1, _a);
|
|
45
46
|
altCurrencies.forEach(function (code) {
|
|
46
|
-
var
|
|
47
|
-
if (
|
|
47
|
+
var rateRaw = rates[code];
|
|
48
|
+
if (rateRaw === undefined || rateRaw === null) {
|
|
48
49
|
throw new Error("createPricingPolicy: missing exchange rate for ".concat(code));
|
|
49
50
|
}
|
|
51
|
+
var rate = Number(rateRaw);
|
|
50
52
|
if (!Number.isFinite(rate) || rate <= 0) {
|
|
51
53
|
throw new Error("createPricingPolicy: invalid exchange rate for ".concat(code));
|
|
52
54
|
}
|