@talend/react-components
Version:
Set of react components.
115 lines (114 loc) • 4.52 kB
JavaScript
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 { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { ButtonIcon } from '@talend/design-system';
import getDefaultT from '../../../translate';
import DatePicker from '../../pickers/DatePicker';
import HeaderTitle from '../HeaderTitle';
import ViewLayout from '../ViewLayout';
import theme from './DateView.module.scss';
/**
* Get the positive euclidean modulo number from a dividend and a divisor
* @param {number} dividend Dividend
* @param {number} divisor Divisor
* @return {number} The positive euclidean modulo
*/
import { jsx as _jsx } from "react/jsx-runtime";
function euclideanModulo(dividend, divisor) {
const modulo = (dividend % divisor + divisor) % divisor;
return modulo < 0 ? modulo + Math.abs(divisor) : modulo;
}
class DateView extends PureComponent {
constructor(props) {
super(props);
this.goToPreviousMonth = this.incrementMonthIndex.bind(this, -1);
this.goToNextMonth = this.incrementMonthIndex.bind(this, 1);
}
incrementMonthIndex(monthIncrement, callback) {
const monthIndexIncremented = this.props.calendar.monthIndex + monthIncrement;
const newMonthIndex = euclideanModulo(monthIndexIncremented, 12);
const yearIncrement = Math.floor(monthIndexIncremented / 12);
const newYear = this.props.calendar.year + yearIncrement;
this.props.onSelectMonthYear({
monthIndex: newMonthIndex,
year: newYear
}, callback);
}
render() {
const {
t
} = this.props;
const header = {
leftElement: /*#__PURE__*/_jsx(ButtonIcon, {
size: "S",
icon: "arrow-left",
onClick: () => this.goToPreviousMonth(),
tabIndex: "-1",
children: t('DATEPICKER_MONTH_PREVIOUS', 'Go to previous month')
}),
middleElement: /*#__PURE__*/_jsx(HeaderTitle, {
monthIndex: this.props.calendar.monthIndex,
year: this.props.calendar.year,
button: {
'aria-label': t('DATEPICKER_TO_MONTH_YEAR', {
defaultValue: 'Switch to month view'
}),
onClick: this.props.onTitleClick,
tabIndex: this.props.allowFocus ? 0 : -1
},
onSelectYear: this.props.onSelectYear
}),
rightElement: /*#__PURE__*/_jsx(ButtonIcon, {
size: "S",
icon: "arrow-right",
onClick: () => this.goToNextMonth(),
tabIndex: "-1",
children: t('DATEPICKER_MONTH_NEXT', 'Go to next month')
})
};
const bodyElement = /*#__PURE__*/_jsx("div", {
className: theme.body,
children: /*#__PURE__*/_jsx("div", {
className: theme.date,
children: /*#__PURE__*/_jsx(DatePicker, {
allowFocus: this.props.allowFocus,
calendar: this.props.calendar,
selectedDate: this.props.selectedDate,
startDate: this.props.startDate,
endDate: this.props.endDate,
onSelect: this.props.onSelectDate,
goToPreviousMonth: this.goToPreviousMonth,
goToNextMonth: this.goToNextMonth,
isDisabledChecker: this.props.isDisabledChecker
})
}, "date")
});
return /*#__PURE__*/_jsx(ViewLayout, {
header: header,
bodyElement: bodyElement
});
}
}
_defineProperty(DateView, "propTypes", {
allowFocus: PropTypes.bool,
calendar: PropTypes.shape({
monthIndex: PropTypes.number.isRequired,
year: PropTypes.number.isRequired
}).isRequired,
onTitleClick: PropTypes.func.isRequired,
onSelectMonthYear: PropTypes.func.isRequired,
onSelectDate: PropTypes.func.isRequired,
onSelectYear: PropTypes.func.isRequired,
selectedDate: PropTypes.instanceOf(Date),
startDate: PropTypes.instanceOf(Date),
endDate: PropTypes.instanceOf(Date),
isDisabledChecker: PropTypes.func,
t: PropTypes.func
});
_defineProperty(DateView, "defaultProps", {
t: getDefaultT()
});
export default DateView;
//# sourceMappingURL=DateView.component.js.map