@chatui/core
Version:
The React library for Chatbot UI
92 lines (91 loc) • 2.83 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["className", "price", "currency", "locale", "original", "autoFit"];
/* eslint-disable react/no-array-index-key */
import React from 'react';
import clsx from 'clsx';
var canFormat = 'Intl' in window && typeof Intl.NumberFormat.prototype.formatToParts === 'function';
/**
* xl - 60
* lg - 48
* md - 36
* sm - 28
*/
function getSize(parts) {
var getPartLength = function getPartLength(type) {
var _parts$find;
return ((_parts$find = parts.find(function (p) {
return p.type === type;
})) === null || _parts$find === void 0 || (_parts$find = _parts$find.value) === null || _parts$find === void 0 ? void 0 : _parts$find.length) || 0;
};
var len1 = getPartLength('integer');
var len2 = getPartLength('fraction');
if (len1 < 2) {
return 'xl';
}
if (len1 === 2) {
return len2 ? 'lg' : 'xl';
}
if (len1 === 3) {
return len2 ? len2 > 1 ? 'md' : 'lg' : 'xl';
}
if (len1 === 4) {
return len2 ? len2 > 1 ? 'sm' : 'md' : 'lg';
}
return len2 ? 'sm' : 'md';
}
export var Price = /*#__PURE__*/React.forwardRef(function (props, ref) {
var className = props.className,
price = props.price,
currency = props.currency,
locale = props.locale,
original = props.original,
autoFit = props.autoFit,
other = _objectWithoutProperties(props, _excluded);
var parts = [];
if (locale && currency && canFormat) {
parts = new Intl.NumberFormat(locale, {
style: 'currency',
currency: currency,
useGrouping: false,
minimumFractionDigits: 0
}).formatToParts(price);
} else {
parts = undefined;
}
// 部分 Android 机只返回 `[{ type: 'literal', value: '¥5.88' }]`
if (!parts || parts.length < 2) {
var decimal = '.';
var _split = "".concat(price).split(decimal),
_split2 = _slicedToArray(_split, 2),
integer = _split2[0],
fraction = _split2[1];
parts = [{
type: 'currency',
value: currency
}, {
type: 'integer',
value: integer
}, {
type: 'decimal',
value: fraction && decimal
}, {
type: 'fraction',
value: fraction
}];
}
return /*#__PURE__*/React.createElement("div", _extends({
className: clsx('Price', {
'Price--original': original
}, className),
"data-size": autoFit ? getSize(parts) : undefined,
ref: ref,
"aria-label": "\u4EF7\u683C\uFF1A".concat(price)
}, other), parts.map(function (t, i) {
return t.value ? /*#__PURE__*/React.createElement("span", {
className: "Price-".concat(t.type),
key: i
}, t.value) : null;
}));
});