@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
277 lines • 15.6 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TimePicker = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
/**
*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const react_1 = require("@floating-ui/react");
const base_1 = require("@neo4j-ndl/base");
const classnames_1 = __importDefault(require("classnames"));
const react_2 = require("react");
const dialog_1 = require("../dialog");
const icons_1 = require("../icons");
const theme_1 = require("../theme");
const typography_1 = require("../typography");
const generate_time_options_1 = require("./generate-time-options");
const needle_time_1 = require("./needle-time");
const time_picker_hooks_1 = require("./time-picker-hooks");
exports.TimePicker = (0, react_2.forwardRef)(function TimePicker(props, ref) {
var _a, _b;
const { format = 'hh:mm', isDisabled, isFluid, isReadOnly, isRequired, selected, timeInterval = 15, label, onChange, className, onError, style, size = 'medium', errorText, htmlAttributes, floatingStrategy, } = props;
const [editedInput, setEditedInput] = (0, react_2.useState)(selected);
const is24Hour = format === 'hh:mm';
const [inputValue, setInputValue] = (0, react_2.useState)((selected === null || selected === void 0 ? void 0 : selected.toString(is24Hour)) || '');
const listRef = (0, react_2.useRef)(null);
const inputRef = (0, react_2.useRef)(null);
(0, react_2.useImperativeHandle)(ref, () => inputRef.current, []);
const [lastAction, setLastAction] = (0, react_2.useState)('type');
const [error, setError] = (0, react_2.useState)(undefined);
const timeOptions = (0, react_2.useMemo)(() => (0, generate_time_options_1.generateTimeOptions)(is24Hour, timeInterval), [is24Hour, timeInterval]);
const { themeClassName } = (0, theme_1.useNeedleTheme)();
const { isPopoverOpen, setIsPopoverOpen } = (0, time_picker_hooks_1.useTimePickerPopover)(false);
const { focusedIndex, setFocusedIndex, handleArrowNavigation } = (0, time_picker_hooks_1.useKeyboardNavigation)(listRef, timeOptions.length);
const classes = {
'ndl-small': size === 'small',
'ndl-medium': size === 'medium',
'ndl-large': size === 'large',
'ndl-error': error !== undefined,
'ndl-fluid': isFluid,
'ndl-disabled': isDisabled,
'ndl-read-only': isReadOnly,
};
const inputWidth = isFluid === true ? '100%' : is24Hour === true ? '6rem' : '9rem';
const errorToShow = errorText !== undefined && errorText !== '' ? errorText : error;
const isValidTime = (0, react_2.useCallback)((hours, minutes) => {
if (hours > 23 || hours < 0) {
return false;
}
if (minutes > 59 || minutes < 0) {
return false;
}
return true;
}, []);
(0, react_2.useEffect)(() => {
if (error !== undefined) {
onError === null || onError === void 0 ? void 0 : onError(error, needle_time_1.NeedleTime.fromString(inputValue));
}
}, [error, inputValue, onError]);
(0, react_2.useEffect)(() => {
var _a;
setEditedInput(selected);
setInputValue((_a = selected === null || selected === void 0 ? void 0 : selected.toString(is24Hour)) !== null && _a !== void 0 ? _a : '');
}, [is24Hour, selected]);
(0, react_2.useEffect)(() => {
if (isPopoverOpen) {
setTimeout(() => {
var _a, _b;
(_b = (_a = listRef.current) === null || _a === void 0 ? void 0 : _a.children[focusedIndex]) === null || _b === void 0 ? void 0 : _b.scrollIntoView({
block: 'center',
inline: 'nearest',
});
}, 0);
}
else {
setTimeout(() => {
var _a, _b, _c;
(_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.blur();
const selectedIndex = (_c = timeOptions.indexOf((_b = selected === null || selected === void 0 ? void 0 : selected.toString(is24Hour)) !== null && _b !== void 0 ? _b : '')) !== null && _c !== void 0 ? _c : 0;
setFocusedIndex(selectedIndex);
}, 0);
}
}, [
focusedIndex,
is24Hour,
isPopoverOpen,
selected,
setFocusedIndex,
timeOptions,
]);
const handleOnClick = (timeText) => {
if (onChange) {
onChange(needle_time_1.NeedleTime.fromString(timeText));
}
setError(undefined);
setInputValue(timeText);
setEditedInput(needle_time_1.NeedleTime.fromString(timeText));
setIsPopoverOpen(false);
};
const newHandleInputChange = (e) => {
scrollToTime(e.target.value);
setInputValue(e.target.value);
setLastAction('type');
const time = needle_time_1.NeedleTime.fromString(e.target.value);
if (!isValidTime(time.hour, time.minute)) {
setError('Invalid time');
}
else {
setError(undefined);
}
};
const handleKeyDown = (e) => {
if (isReadOnly === true || isDisabled === true) {
e.preventDefault();
return;
}
if (e.key === 'Escape') {
setIsPopoverOpen(false);
}
setIsPopoverOpen(true);
if (e.key === 'Enter') {
if (lastAction === 'type' && inputValue.length >= (is24Hour ? 5 : 6)) {
const time = needle_time_1.NeedleTime.fromString(inputValue);
handleOnClick(time.toString(is24Hour));
}
else if (lastAction === 'type' && !inputValue.includes(':')) {
const time = needle_time_1.NeedleTime.fromString(inputValue);
if (isValidTime(time.hour, time.minute)) {
handleOnClick(time.toString(is24Hour));
}
setIsPopoverOpen(false);
}
else {
handleOnClick(timeOptions[focusedIndex]);
}
}
else if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
e.preventDefault();
setLastAction('arrow');
handleArrowNavigation(e.key);
}
};
const scrollToTime = (startOfTime) => {
const parsedTime = startOfTime.toLowerCase().replace(/[^0-9amp]/g, '');
let index = -1;
// Handle AM/PM input
const isPM = parsedTime.includes('p');
const isAM = parsedTime.includes('a');
const timeDigits = parsedTime.replace(/[amp]/g, '');
if (timeDigits.length === 1) {
// Single digit - assume it's an hour
const hour = `0${timeDigits}`;
const searchTime = is24Hour
? `${hour}:00`
: `${hour}:00 ${isPM ? 'PM' : 'AM'}`;
index = timeOptions.indexOf(searchTime);
}
else {
// Find closest matching time
const closestTime = timeOptions.find((time) => {
const timeWithoutSpecialChars = time
.toLowerCase()
.replace(/[^0-9amp]/g, '');
if (isPM || isAM) {
// If AM/PM specified, match that specifically
return timeWithoutSpecialChars.startsWith(parsedTime);
}
else {
// Otherwise just match the numbers
return timeWithoutSpecialChars
.replace(/[amp]/g, '')
.startsWith(timeDigits);
}
});
index = closestTime !== undefined ? timeOptions.indexOf(closestTime) : -1;
}
if (index !== -1) {
setFocusedIndex(index);
}
return index;
};
/** Custom floating ui solution */
const isInsideDialog = (0, dialog_1.useIsInsideDialog)();
const containerRef = (0, react_2.useRef)(null);
const { refs, floatingStyles, context } = (0, react_1.useFloating)({
elements: {
reference: containerRef.current,
},
middleware: [
(0, react_1.offset)(10),
(0, react_1.flip)({
fallbackPlacements: ['top'],
fallbackStrategy: 'bestFit',
}),
(0, react_1.shift)({ padding: 5 }),
],
onOpenChange: (open) => {
setIsPopoverOpen(open);
},
open: isDisabled !== true && isReadOnly !== true && isPopoverOpen,
placement: 'bottom',
strategy: floatingStrategy !== null && floatingStrategy !== void 0 ? floatingStrategy : (isInsideDialog ? 'fixed' : 'absolute'),
whileElementsMounted: react_1.autoUpdate,
});
const dismiss = (0, react_1.useDismiss)(context);
const { getReferenceProps, getFloatingProps } = (0, react_1.useInteractions)([dismiss]);
const { styles: transitionStyles } = (0, react_1.useTransitionStyles)(context, {
duration: (_a = Number.parseInt(base_1.tokens.transitions.values.duration.quick, 10)) !== null && _a !== void 0 ? _a : 0,
});
const mergedRef = (0, react_1.useMergeRefs)([refs.setReference, containerRef]);
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)("div", Object.assign({ className: (0, classnames_1.default)('ndl-time-picker', className, Object.assign({}, classes)), ref: mergedRef }, getReferenceProps(), { style: {
width: isFluid === true ? '100%' : 'fit-content',
}, children: [(0, jsx_runtime_1.jsxs)("label", { className: "ndl-time-picker-label", children: [label !== undefined && ((0, jsx_runtime_1.jsx)(typography_1.Typography, { variant: size === 'large' ? 'body-large' : 'body-medium', children: label })), (0, jsx_runtime_1.jsxs)("div", { className: "ndl-time-picker-input-wrapper", style: Object.assign({ width: inputWidth }, style), children: [(0, jsx_runtime_1.jsx)("input", Object.assign({ "aria-label": label, className: "ndl-time-picker-input", type: "text", ref: inputRef, value: inputValue, disabled: isDisabled, readOnly: isReadOnly, required: isRequired, onChange: newHandleInputChange, onKeyDown: handleKeyDown, onClick: () => {
if (isReadOnly === true || isDisabled === true)
return;
setIsPopoverOpen(true);
}, onBlur: (e) => {
var _a;
if (isReadOnly === true || isDisabled === true)
return;
if (((_a = e.relatedTarget) === null || _a === void 0 ? void 0 : _a.classList.contains('ndl-time-picker-popover-item')) === true) {
return;
}
if (inputValue.length < 4) {
setError('Invalid time');
return;
}
const time = needle_time_1.NeedleTime.fromString(inputValue);
if (time === undefined || time === null) {
setError('Invalid time');
return;
}
if (!isValidTime(time.hour, time.minute)) {
setError('Invalid time');
}
else {
setError(undefined);
setInputValue(time.toString(is24Hour));
}
}, placeholder: format, maxLength: is24Hour ? 5 : 8 }, htmlAttributes)), (0, jsx_runtime_1.jsx)(icons_1.ClockIconOutline, { className: "ndl-icon-svg ndl-time-picker-icon" })] })] }), errorToShow !== undefined && ((0, jsx_runtime_1.jsxs)("div", { className: "ndl-time-picker-error-wrapper", children: [(0, jsx_runtime_1.jsx)(icons_1.ExclamationCircleIconSolid, { className: "ndl-time-picker-error-icon" }), (0, jsx_runtime_1.jsx)(typography_1.Typography, { variant: size === 'large' ? 'body-medium' : 'body-small', className: "ndl-time-picker-error-text", children: errorToShow })] }))] })), context.open && ((0, jsx_runtime_1.jsx)(react_1.FloatingPortal, { root: context.refs.reference.current, children: (0, jsx_runtime_1.jsx)(react_1.FloatingFocusManager, { context: context, modal: false, initialFocus: -1, children: (0, jsx_runtime_1.jsx)("div", Object.assign({ ref: refs.setFloating, className: (0, classnames_1.default)(themeClassName, 'ndl-time-picker-popover', Object.assign({}, classes)), style: Object.assign(Object.assign(Object.assign({}, transitionStyles), floatingStyles), { width: isFluid === true
? (_b = containerRef.current) === null || _b === void 0 ? void 0 : _b.clientWidth
: undefined }) }, getFloatingProps(), { children: (0, jsx_runtime_1.jsx)("ul", { ref: listRef, tabIndex: -1, children: timeOptions.map((time, i) => ((0, jsx_runtime_1.jsx)("li", { role: "option", "aria-selected": (selected === null || selected === void 0 ? void 0 : selected.toString(is24Hour)) === time ||
(editedInput === null || editedInput === void 0 ? void 0 : editedInput.toString(is24Hour)) === time, value: time, onClick: (e) => {
e.stopPropagation();
e.preventDefault();
handleOnClick(time);
setIsPopoverOpen(false);
}, onMouseDown: (e) => {
e.stopPropagation();
e.preventDefault();
}, tabIndex: -1, className: (0, classnames_1.default)(focusedIndex === i ? 'focused' : '', 'ndl-time-picker-popover-item'), onKeyDown: (e) => {
if (e.key === 'Enter') {
e.stopPropagation();
handleOnClick(time);
}
}, children: time }, i))) }) })) }) }))] }));
});
//# sourceMappingURL=TimePicker.js.map