UNPKG

7.53 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.TimePicker = void 0;
4var tslib_1 = require("tslib");
5var React = require("react");
6var react_hooks_1 = require("@fluentui/react-hooks");
7var Utilities_1 = require("../../Utilities");
8var date_time_utilities_1 = require("@fluentui/date-time-utilities");
9var ComboBox_1 = require("../../ComboBox");
10var REGEX_SHOW_SECONDS_HOUR_12 = /^((1[0-2]|0?[1-9]):([0-5][0-9]):([0-5][0-9])\s([AaPp][Mm]))$/;
11var REGEX_HIDE_SECONDS_HOUR_12 = /^((1[0-2]|0?[1-9]):[0-5][0-9]\s([AaPp][Mm]))$/;
12var REGEX_SHOW_SECONDS_HOUR_24 = /^([0-1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/;
13var REGEX_HIDE_SECONDS_HOUR_24 = /^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$/;
14var TIME_LOWER_BOUND = 0;
15var TIME_UPPER_BOUND = 23;
16var getDefaultStrings = function (useHour12, showSeconds) {
17 var hourUnits = useHour12 ? '12-hour' : '24-hour';
18 var timeFormat = "hh:mm" + (showSeconds ? ':ss' : '') + (useHour12 ? ' AP' : '');
19 var errorMessageToDisplay = "Enter a valid time in the " + hourUnits + " format: " + timeFormat;
20 return {
21 invalidInputErrorMessage: errorMessageToDisplay,
22 };
23};
24var TimePicker = function (_a) {
25 var label = _a.label, _b = _a.increments, increments = _b === void 0 ? 30 : _b, _c = _a.showSeconds, showSeconds = _c === void 0 ? false : _c, _d = _a.allowFreeform, allowFreeform = _d === void 0 ? true : _d, _e = _a.useHour12, useHour12 = _e === void 0 ? false : _e, timeRange = _a.timeRange, _f = _a.strings, strings = _f === void 0 ? getDefaultStrings(useHour12, showSeconds) : _f, defaultValue = _a.defaultValue, onChange = _a.onChange, onFormatDate = _a.onFormatDate, onValidateUserInput = _a.onValidateUserInput, rest = tslib_1.__rest(_a, ["label", "increments", "showSeconds", "allowFreeform", "useHour12", "timeRange", "strings", "defaultValue", "onChange", "onFormatDate", "onValidateUserInput"]);
26 var _g = React.useState(''), userText = _g[0], setUserText = _g[1];
27 var _h = React.useState(''), errorMessage = _h[0], setErrorMessage = _h[1];
28 var optionsCount = getDropdownOptionsCount(increments, timeRange);
29 var initialValue = react_hooks_1.useConst(defaultValue || new Date());
30 var baseDate = React.useMemo(function () { return generateBaseDate(increments, timeRange, initialValue); }, [
31 increments,
32 timeRange,
33 initialValue,
34 ]);
35 var timePickerOptions = React.useMemo(function () {
36 var optionsList = Array(optionsCount);
37 for (var i = 0; i < optionsCount; i++) {
38 optionsList[i] = 0;
39 }
40 return optionsList.map(function (_, index) {
41 var option = date_time_utilities_1.addMinutes(baseDate, increments * index);
42 option.setSeconds(0);
43 var optionText = onFormatDate ? onFormatDate(option) : date_time_utilities_1.formatTimeString(option, showSeconds, useHour12);
44 return {
45 key: optionText,
46 text: optionText,
47 };
48 });
49 }, [baseDate, increments, optionsCount, showSeconds, onFormatDate, useHour12]);
50 var _j = React.useState(timePickerOptions[0].key), selectedKey = _j[0], setSelectedKey = _j[1];
51 var onInputChange = React.useCallback(function (event, option, index, value) {
52 var validateUserInput = function (userInput) {
53 var errorMessageToDisplay = '';
54 var regex;
55 if (useHour12) {
56 regex = showSeconds ? REGEX_SHOW_SECONDS_HOUR_12 : REGEX_HIDE_SECONDS_HOUR_12;
57 }
58 else {
59 regex = showSeconds ? REGEX_SHOW_SECONDS_HOUR_24 : REGEX_HIDE_SECONDS_HOUR_24;
60 }
61 if (!regex.test(userInput)) {
62 errorMessageToDisplay = strings.invalidInputErrorMessage;
63 }
64 return errorMessageToDisplay;
65 };
66 var key = option === null || option === void 0 ? void 0 : option.key;
67 var updatedUserText = '';
68 var errorMessageToDisplay = '';
69 if (value) {
70 if (allowFreeform && !option) {
71 if (!onFormatDate) {
72 // Validate only if user did not add onFormatDate
73 errorMessageToDisplay = validateUserInput(value);
74 }
75 else {
76 // Use user provided validation if onFormatDate is provided
77 if (onValidateUserInput) {
78 errorMessageToDisplay = onValidateUserInput(value);
79 }
80 }
81 }
82 updatedUserText = value;
83 }
84 else if (option) {
85 updatedUserText = option.text;
86 }
87 if (onChange && !errorMessageToDisplay) {
88 var selectedTime = value || (option === null || option === void 0 ? void 0 : option.text) || '';
89 var date = date_time_utilities_1.getDateFromTimeSelection(useHour12, baseDate, selectedTime);
90 onChange(event, date);
91 }
92 setErrorMessage(errorMessageToDisplay);
93 setUserText(updatedUserText);
94 setSelectedKey(key);
95 }, [
96 baseDate,
97 allowFreeform,
98 onChange,
99 onFormatDate,
100 onValidateUserInput,
101 showSeconds,
102 useHour12,
103 strings.invalidInputErrorMessage,
104 ]);
105 var evaluatePressedKey = function (event) {
106 // eslint-disable-next-line deprecation/deprecation
107 var charCode = event.charCode;
108 if (!onFormatDate &&
109 // Only permit input of digits, space, colon, A/P/M characters
110 !((charCode >= Utilities_1.KeyCodes.zero && charCode <= Utilities_1.KeyCodes.colon) ||
111 charCode === Utilities_1.KeyCodes.space ||
112 charCode === Utilities_1.KeyCodes.a ||
113 charCode === Utilities_1.KeyCodes.m ||
114 charCode === Utilities_1.KeyCodes.p)) {
115 event.preventDefault();
116 }
117 };
118 return (React.createElement(ComboBox_1.ComboBox, tslib_1.__assign({}, rest, { allowFreeform: allowFreeform, selectedKey: selectedKey, label: label, errorMessage: errorMessage, options: timePickerOptions, onChange: onInputChange, text: userText,
119 //eslint-disable-next-line
120 onKeyPress: evaluatePressedKey })));
121};
122exports.TimePicker = TimePicker;
123exports.TimePicker.displayName = 'TimePicker';
124var clampTimeRange = function (timeRange) {
125 return {
126 start: Math.min(Math.max(timeRange.start, TIME_LOWER_BOUND), TIME_UPPER_BOUND),
127 end: Math.min(Math.max(timeRange.end, TIME_LOWER_BOUND), TIME_UPPER_BOUND),
128 };
129};
130var generateBaseDate = function (increments, timeRange, baseDate) {
131 if (timeRange) {
132 var clampedTimeRange = clampTimeRange(timeRange);
133 baseDate.setHours(clampedTimeRange.start);
134 }
135 return date_time_utilities_1.ceilMinuteToIncrement(baseDate, increments);
136};
137var getDropdownOptionsCount = function (increments, timeRange) {
138 var hoursInRange = date_time_utilities_1.TimeConstants.HoursInOneDay;
139 if (timeRange) {
140 var clampedTimeRange = clampTimeRange(timeRange);
141 if (clampedTimeRange.start > clampedTimeRange.end) {
142 hoursInRange = date_time_utilities_1.TimeConstants.HoursInOneDay - timeRange.start - timeRange.end;
143 }
144 else if (timeRange.end > timeRange.start) {
145 hoursInRange = timeRange.end - timeRange.start;
146 }
147 }
148 return Math.floor((date_time_utilities_1.TimeConstants.MinutesInOneHour * hoursInRange) / increments);
149};
150//# sourceMappingURL=TimePicker.js.map
\No newline at end of file