UNPKG

@talend/react-components

Version:
153 lines 4.8 kB
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } import { Component } from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import { timeToStr, pad } from '../../Time/time-extraction'; import { Gesture } from '@talend/react-a11y'; import theme from './TimePicker.module.scss'; import { jsx as _jsx } from "react/jsx-runtime"; function isBefore(a, b) { if (a.hours > b.hours) { return false; } else if (a.hours === b.hours && a.minutes > b.minutes) { return false; } else if (a.hours === b.hours && a.minutes === b.minutes && a.seconds >= b.seconds) { return false; } return true; } function addInterval({ hours, minutes, ...seconds }, interval = 60) { let newMinutes = minutes + interval; let newHours = hours; if (Math.floor(newMinutes / 60) > 0) { newHours += Math.floor(newMinutes / 60); newMinutes %= 60; } return { hours: newHours, minutes: newMinutes, ...seconds }; } function getOptions(interval = 60, useSeconds) { const options = []; const start = { hours: 0, minutes: 0, seconds: 0 }; const end = { hours: 23, minutes: 59, seconds: 59 }; let current = start; while (isBefore(current, end)) { options.push({ label: timeToStr(current, useSeconds), value: current }); current = addInterval(current, interval); } return options; } export class TimePicker extends Component { constructor(props) { super(props); this.onSelect = this.onSelect.bind(this); this.updateHighlightIndex = this.updateHighlightIndex.bind(this); this.scrollItemIntoView = this.scrollItemIntoView.bind(this); this.options = getOptions(props.interval, props.useSeconds); this.state = { hightlightedItemIndex: this.options.findIndex(option => option.label.includes(props.textInput)) }; } componentDidMount() { if (this.props.textInput) { this.scrollItemIntoView(this.props.textInput); } } onSelect(event, option, index) { this.setState({ hightlightedItemIndex: index }, () => this.props.onChange(event, { textInput: option.label, time: { hours: pad(option.value.hours), minutes: pad(option.value.minutes), seconds: pad(option.value.seconds) } })); } scrollItemIntoView(textInput) { const found = this.options.findIndex(option => option.label.includes(textInput)); if (found) { const ref = this.containerRef.childNodes[found]; if (ref) { ref.scrollIntoView({ block: 'center' }); } if (found !== this.state.hightlightedItemIndex) { this.updateHighlightIndex(found); } } } updateHighlightIndex(index) { this.setState(({ hightlightedItemIndex }) => { if (hightlightedItemIndex !== index) { return { hightlightedItemIndex: index }; } return null; }); } render() { return /*#__PURE__*/_jsx("div", { className: theme.container, ref: ref => this.containerRef = ref, role: "list", children: this.options.map((option, index) => { const className = classNames('tc-time-picker-time', { highlight: index === this.state.hightlightedItemIndex }); const ariaProps = {}; if (index === this.state.hightlightedItemIndex) { ariaProps['aria-current'] = 'time'; } return /*#__PURE__*/_jsx("button", { tabIndex: -1, role: "listitem", type: "button", className: className, onClick: event => this.onSelect(event, option), onKeyDown: event => this.props.onKeyDown(event, this.containerRef.childNodes[index]), ...ariaProps, children: option.label }, index); }) }); } } _defineProperty(TimePicker, "propTypes", { interval: PropTypes.number, onChange: PropTypes.func.isRequired, onKeyDown: PropTypes.func.isRequired, textInput: PropTypes.string, useSeconds: PropTypes.bool }); _defineProperty(TimePicker, "defaultProps", { interval: 60, useSeconds: false }); export default Gesture.withListGesture(TimePicker, true); //# sourceMappingURL=TimePicker.component.js.map