UNPKG

@arche-mc2/arche-controls

Version:

We know that there are a ton of react UI library projects to choose from. Our hope with this one is to provide the next generation of react components that you can use to bootstrap your next project, or as a reference for building a UIKit. Read on to get

153 lines 8.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); require("normalize.css/normalize.css"); require("react-dates/initialize"); var React = require("react"); var BaseControl_1 = require("../_Common/BaseControl/BaseControl"); var theming_1 = require("../../../Common/theming"); var withTheme_1 = require("../../../Common/theming/withTheme"); var eventListener_1 = require("../../../Common/utils/eventListener"); var SvgIcon_1 = require("../../Display/SvgIcon"); var react_dates_1 = require("react-dates"); var helpers_1 = require("../../../Common/utils/helpers"); var fr_1 = require("./i18n/fr"); var classnames = require("classnames"); var utils_1 = require("../../../Common/utils"); var moment = require("moment"); var styles_1 = require("./styles"); var typestyle_1 = require("typestyle"); var Select_1 = require("../Select"); var helpers_2 = require("../../../Common/utils/helpers"); var types_1 = require("../../../Common/utils/types"); moment.locale('fr'); var MIN_DATE = new Date(-8640000000000); var MAX_DATE = new Date(+8640000000000); var UpDate = (function (_super) { tslib_1.__extends(UpDate, _super); function UpDate(p, c) { var _this = _super.call(this, p, c) || this; _this.onChange = function (startDate, endDate) { _this.handleChangeEvent(eventListener_1.eventFactory(_this.props.name, startDate), startDate); }; _this.setInput = function (input) { if (_this.dateInput == undefined) { _this.dateInput = input; if (_this.props.tabIndex) { _this.dateInput.setAttribute('tavbindex', _this.props.tabIndex); } } }; _this.onFocusChange = function (_a) { var focused = _a.focused; if (focused) { _this.onFocus(eventListener_1.eventFactory('focus', true)); } else { _this.onBlur(eventListener_1.eventFactory('blur', true)); } }; _this.returnYears = function () { var years = []; for (var i = moment().year() - 100; i <= moment().year(); i++) { years.push({ id: i, text: i }); } years = years.sort(function (a, b) { return b.id - a.id; }); return years; }; _this.formatMonth = function (month) { return (month + 1) < 10 ? "0" + (month + 1) : "" + (month + 1); }; _this.onCloseCalendar = function (_a) { var date = _a.date; if (date == null) { var dateInput = document.getElementById(_this.id); dateInput.value = ''; } ; }; _this.renderMonthElement = function (_a) { var month = _a.month, onMonthSelect = _a.onMonthSelect, onYearSelect = _a.onYearSelect; return (React.createElement("div", { style: { width: "100%", display: "flex", justifyContent: "center", flexShrink: 2 } }, React.createElement("div", { style: { width: "30%" } }, React.createElement(Select_1.default, { value: { id: month.month(), text: _this.formatMonth(month.month()) }, menuPlacement: "bottom", data: moment.months().map(function (label, value) { return ({ id: value, text: label }); }), tooltip: moment.months().find(function (label, value) { return value == month.month(); }), onChange: function (e) { onMonthSelect(month, e.target.value.id); } })), React.createElement("div", { className: typestyle_1.style({ width: "40%" }) }, React.createElement(Select_1.default, { data: _this.returnYears(), menuPlacement: "bottom", value: { id: month.year(), text: month.year() }, isSearchable: true, onChange: function (e) { return onYearSelect(month, e.target.value.id); } })))); }; _this.defaultIsOutsideRange = function (day) { return (_this.props.maxDate && day.toDate().setHours(0, 0, 0, 0) > _this.props.maxDate.setHours(0, 0, 0, 0)) || (_this.props.minDate && day.toDate().setHours(0, 0, 0, 0) < _this.props.minDate.setHours(0, 0, 0, 0)); }; _this.state = { value: _this.props.value }; _this.id = helpers_1.generateUniqueId(); return _this; } ; UpDate.prototype.showError = function () { return this.props.showError !== undefined ? this.props.showError === true : this.hasError; }; ; UpDate.prototype.showSuccess = function () { return this.props.showSuccess; }; ; UpDate.prototype.checkDate = function (e) { var event = e.target; if (event && event.value) { var value = event.value; var stringsToNumerics = value.split('/').map(function (stringValue) { return helpers_2.isNumeric(stringValue); }); var unwantedCharacters = stringsToNumerics.some(function (booleanValue) { return !booleanValue; }); if (unwantedCharacters) event.value = event.value.replace(/[a-zA-Z\u00C0-\u024F\u1E00-\u1EFF!''$%^&*()_+|~=`{}\[\]:\\;<>?,.@#§µ²°]/, ''); } }; ; UpDate.prototype.componentDidMount = function () { this.datePicker = document.getElementById(this.id); if (this.datePicker) { this.datePicker.addEventListener('input', this.checkDate); } if (this.props['dataFor'] && this.datePicker) { this.datePicker.setAttribute('data-tip', 'tooltip'); this.datePicker.setAttribute('data-for', this.props['dataFor']); } }; ; UpDate.prototype.componentWillUnmount = function () { if (this.datePicker) this.datePicker.removeEventListener('input', this.checkDate); }; ; UpDate.prototype.getValue = function (newDate) { return newDate; }; ; UpDate.prototype.renderControl = function () { var _a = this.props, disabled = _a.disabled, minDate = _a.minDate, maxDate = _a.maxDate, floatingLabel = _a.floatingLabel, placeholder = _a.placeholder, iconPosition = _a.iconPosition; return (React.createElement("div", tslib_1.__assign({ className: classnames(styles_1.default(tslib_1.__assign({ focused: this.isFocused }, this.props)), 'up-date', this.isFocused ? 'up-input-focused' : null, !utils_1.isEmpty(this.currentValue) ? 'up-input-valued' : null) }, types_1.getTestableComponentProps(this.props)), floatingLabel && React.createElement("label", { htmlFor: this.id }, floatingLabel), React.createElement(react_dates_1.SingleDatePicker, { customInputIcon: React.createElement(SvgIcon_1.default, { iconName: "calendar", width: "15px", height: "15px" }), enableOutsideDays: this.props.enableOutsideDays, renderMonthElement: this.props.numberOfMonths == 1 ? this.renderMonthElement : null, numberOfMonths: this.props.numberOfMonths, focused: this.isFocused, onFocusChange: this.onFocusChange, placeholder: floatingLabel ? '' : placeholder, date: this.currentValue, onDateChange: this.onChange, id: this.id, disabled: disabled || this.props.readonly, showClearDate: !this.props.readonly, showDefaultInputIcon: true, noBorder: false, screenReaderInputMessage: floatingLabel ? floatingLabel : placeholder ? placeholder : "Date", ref: this.setInput, keepOpenOnDateSelect: false, hideKeyboardShortcutsPanel: true, phrases: fr_1.default, isDayBlocked: function (day) { return false; }, inputIconPosition: iconPosition == "right" ? "after" : "before", isOutsideRange: this.props.isOutsideRange || this.defaultIsOutsideRange, daySize: this.props.daySize, onClose: this.onCloseCalendar }))); }; UpDate.defaultProps = { format: 'DD/MM/YYYY', showError: true, theme: theming_1.default, minDate: MIN_DATE, maxDate: MAX_DATE, iconPosition: 'right', numberOfMonths: 2, enableOutsideDays: true, daySize: 30 }; return UpDate; }(BaseControl_1.BaseControlComponent)); exports.UpDate = UpDate; ; exports.default = withTheme_1.default(UpDate); //# sourceMappingURL=UpDate.js.map