feat(discount-codes): update DiscountCodeFormPage to include formatted number handling
This commit is contained in:
parent
9544517fc9
commit
cc12101dbe
|
|
@ -17,7 +17,12 @@ const schema = yup.object({
|
||||||
name: yup.string().min(1, 'نام الزامی است').max(100, 'نام نباید بیشتر از ۱۰۰ کاراکتر باشد').required('نام الزامی است'),
|
name: yup.string().min(1, 'نام الزامی است').max(100, 'نام نباید بیشتر از ۱۰۰ کاراکتر باشد').required('نام الزامی است'),
|
||||||
description: yup.string().max(500, 'توضیحات نباید بیشتر از ۵۰۰ کاراکتر باشد').nullable(),
|
description: yup.string().max(500, 'توضیحات نباید بیشتر از ۵۰۰ کاراکتر باشد').nullable(),
|
||||||
type: yup.mixed<'percentage' | 'fixed' | 'fee_percentage'>().oneOf(['percentage', 'fixed', 'fee_percentage']).required('نوع الزامی است'),
|
type: yup.mixed<'percentage' | 'fixed' | 'fee_percentage'>().oneOf(['percentage', 'fixed', 'fee_percentage']).required('نوع الزامی است'),
|
||||||
value: yup.number().typeError('مقدار نامعتبر است').required('مقدار الزامی است').min(0.01, 'مقدار باید بیشتر از صفر باشد'),
|
value: yup
|
||||||
|
.number()
|
||||||
|
.transform((val, original) => parseFormattedNumber(original) as any)
|
||||||
|
.typeError('مقدار نامعتبر است')
|
||||||
|
.required('مقدار الزامی است')
|
||||||
|
.min(0.01, 'مقدار باید بیشتر از صفر باشد'),
|
||||||
status: yup.mixed<'active' | 'inactive'>().oneOf(['active', 'inactive']).required('وضعیت الزامی است'),
|
status: yup.mixed<'active' | 'inactive'>().oneOf(['active', 'inactive']).required('وضعیت الزامی است'),
|
||||||
application_level: yup.mixed<'invoice' | 'category' | 'product' | 'shipping' | 'product_fee'>().oneOf(['invoice', 'category', 'product', 'shipping', 'product_fee']).required('سطح اعمال الزامی است'),
|
application_level: yup.mixed<'invoice' | 'category' | 'product' | 'shipping' | 'product_fee'>().oneOf(['invoice', 'category', 'product', 'shipping', 'product_fee']).required('سطح اعمال الزامی است'),
|
||||||
min_purchase_amount: yup
|
min_purchase_amount: yup
|
||||||
|
|
@ -196,6 +201,8 @@ const DiscountCodeFormPage = () => {
|
||||||
step="0.01"
|
step="0.01"
|
||||||
placeholder="20"
|
placeholder="20"
|
||||||
error={errors.value?.message as string}
|
error={errors.value?.message as string}
|
||||||
|
thousandSeparator
|
||||||
|
numeric
|
||||||
{...register('value')}
|
{...register('value')}
|
||||||
data-testid="discount-value-input"
|
data-testid="discount-value-input"
|
||||||
/>
|
/>
|
||||||
|
|
|
||||||
|
|
@ -86,13 +86,16 @@ export const createOptionalNumberTransform = () => {
|
||||||
|
|
||||||
export const formatWithThousands = (value: string | number): string => {
|
export const formatWithThousands = (value: string | number): string => {
|
||||||
if (value === null || value === undefined) return "";
|
if (value === null || value === undefined) return "";
|
||||||
const str = persianToEnglish(value.toString());
|
const raw = persianToEnglish(value.toString());
|
||||||
if (str === "") return "";
|
if (raw === "") return "";
|
||||||
const parts = str.replace(/[^\d.]/g, "").split(".");
|
const hasTrailingDot = /\.$/.test(raw);
|
||||||
|
const sanitized = raw.replace(/[^\d.]/g, "");
|
||||||
|
const parts = sanitized.split(".");
|
||||||
const integerPart = parts[0];
|
const integerPart = parts[0];
|
||||||
const decimalPart = parts.length > 1 ? parts[1] : "";
|
const decimalPart = parts.length > 1 ? parts[1] : "";
|
||||||
const withCommas = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
const withCommas = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
||||||
return decimalPart ? `${withCommas}.${decimalPart}` : withCommas;
|
if (decimalPart) return `${withCommas}.${decimalPart}`;
|
||||||
|
return hasTrailingDot ? `${withCommas}.` : withCommas;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const parseFormattedNumber = (value: any): number | undefined => {
|
export const parseFormattedNumber = (value: any): number | undefined => {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue