UNPKG

3.58 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.createFormattedComponent = exports.createFormattedDateTimePartsComponent = exports.FormattedListParts = exports.FormattedNumberParts = void 0;
4var tslib_1 = require("tslib");
5var React = (0, tslib_1.__importStar)(require("react"));
6var useIntl_1 = (0, tslib_1.__importDefault)(require("./useIntl"));
7var DisplayName;
8(function (DisplayName) {
9 DisplayName["formatDate"] = "FormattedDate";
10 DisplayName["formatTime"] = "FormattedTime";
11 DisplayName["formatNumber"] = "FormattedNumber";
12 DisplayName["formatList"] = "FormattedList";
13 // Note that this DisplayName is the locale display name, not to be confused with
14 // the name of the enum, which is for React component display name in dev tools.
15 DisplayName["formatDisplayName"] = "FormattedDisplayName";
16})(DisplayName || (DisplayName = {}));
17var DisplayNameParts;
18(function (DisplayNameParts) {
19 DisplayNameParts["formatDate"] = "FormattedDateParts";
20 DisplayNameParts["formatTime"] = "FormattedTimeParts";
21 DisplayNameParts["formatNumber"] = "FormattedNumberParts";
22 DisplayNameParts["formatList"] = "FormattedListParts";
23})(DisplayNameParts || (DisplayNameParts = {}));
24var FormattedNumberParts = function (props) {
25 var intl = (0, useIntl_1.default)();
26 var value = props.value, children = props.children, formatProps = (0, tslib_1.__rest)(props, ["value", "children"]);
27 return children(intl.formatNumberToParts(value, formatProps));
28};
29exports.FormattedNumberParts = FormattedNumberParts;
30exports.FormattedNumberParts.displayName = 'FormattedNumberParts';
31var FormattedListParts = function (props) {
32 var intl = (0, useIntl_1.default)();
33 var value = props.value, children = props.children, formatProps = (0, tslib_1.__rest)(props, ["value", "children"]);
34 return children(intl.formatListToParts(value, formatProps));
35};
36exports.FormattedListParts = FormattedListParts;
37exports.FormattedNumberParts.displayName = 'FormattedNumberParts';
38function createFormattedDateTimePartsComponent(name) {
39 var ComponentParts = function (props) {
40 var intl = (0, useIntl_1.default)();
41 var value = props.value, children = props.children, formatProps = (0, tslib_1.__rest)(props, ["value", "children"]);
42 var date = typeof value === 'string' ? new Date(value || 0) : value;
43 var formattedParts = name === 'formatDate'
44 ? intl.formatDateToParts(date, formatProps)
45 : intl.formatTimeToParts(date, formatProps);
46 return children(formattedParts);
47 };
48 ComponentParts.displayName = DisplayNameParts[name];
49 return ComponentParts;
50}
51exports.createFormattedDateTimePartsComponent = createFormattedDateTimePartsComponent;
52function createFormattedComponent(name) {
53 var Component = function (props) {
54 var intl = (0, useIntl_1.default)();
55 var value = props.value, children = props.children, formatProps = (0, tslib_1.__rest)(props
56 // TODO: fix TS type definition for localeMatcher upstream
57 , ["value", "children"]);
58 // TODO: fix TS type definition for localeMatcher upstream
59 var formattedValue = intl[name](value, formatProps);
60 if (typeof children === 'function') {
61 return children(formattedValue);
62 }
63 var Text = intl.textComponent || React.Fragment;
64 return React.createElement(Text, null, formattedValue);
65 };
66 Component.displayName = DisplayName[name];
67 return Component;
68}
69exports.createFormattedComponent = createFormattedComponent;