UNPKG

@guardian/threads

Version:
43 lines 1.84 kB
import { __extends } from "tslib"; import React from 'react'; import _debounce from 'lodash.debounce'; import { parseDate } from '../../../../utils/parseDate'; import { InputChip } from './abstract/InputChip'; import AutosizeInput from 'react-input-autosize'; import styles from './DateChip.module.css'; var DateChip = /** @class */ (function (_super) { __extends(DateChip, _super); function DateChip() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.state = { isValidDate: true, }; _this.debounceCheckValidity = _debounce(function (text) { if (text === '') { _this.setState({ isValidDate: true }); // cheeky hack to stop the instant pop-in of the warning } else { var date = parseDate(text, _this.props.dateMode); _this.setState({ isValidDate: !!date, }); } }, 500); _this.onChange = function (e) { _this.props.onUpdate(_this.props.index, e.target.value); _this.debounceCheckValidity(e.target.value); }; return _this; } DateChip.prototype.componentWillUnmount = function () { this.debounceCheckValidity.cancel(); }; DateChip.prototype.render = function () { var isValidDate = this.state.isValidDate; return (React.createElement("div", { className: styles.inlineDate, "data-invalid": !isValidDate ? true : null }, React.createElement(AutosizeInput, { inputRef: this.setTextInputRef, type: "text", inputClassName: styles.inlineInputChip, value: this.props.value, onChange: this.onChange, onKeyDown: this.onKeyDown }))); }; return DateChip; }(InputChip)); export { DateChip }; //# sourceMappingURL=DateChip.js.map