UNPKG

@zohodesk/dot

Version:

In this Library, we Provide Some Basic Components to Build Your Application

195 lines (182 loc) 5.64 kB
/**** Libraries ****/ import React, { PureComponent } from 'react'; import { defaultProps } from "./props/defaultProps"; import { propTypes } from "./props/propTypes"; /**** Components ****/ import Label from '@zohodesk/components/lib/Label/Label'; import DateWidget from '@zohodesk/components/lib/DateTime/DateWidget'; import ValidationMessage from "../ValidationMessage/ValidationMessage"; import { getUniqueId } from '@zohodesk/components/lib/Provider/IdProvider'; import FieldContainer from "../FieldContainer/FieldContainer"; /**** CSS ****/ import style from "../Fields.module.css"; export default class DateField extends PureComponent { constructor(props) { super(props); this.handleChange = this.handleChange.bind(this); this.handleGetRef = this.handleGetRef.bind(this); this.handleLabelClick = this.handleLabelClick.bind(this); this.getNextId = getUniqueId(this); this.openPopup = this.openPopup.bind(this); } handleChange(value) { let { id, onChange } = this.props; onChange && onChange(id, value); } handleGetRef(el) { let { getRef, id } = this.props; this.dateField = el; getRef && getRef(el, id); } componentDidMount() { this.openPopup(); } openPopup() { let { openPopupOnMount } = this.props; if (openPopupOnMount) { setTimeout(() => { this.dateField && this.dateField.click && this.dateField.click(); this.handleLabelClick(); }, 100); } } componentDidUpdate(prevProps) { let { openPopupOnMount: prevOpenPopupOnMount } = prevProps; let { openPopupOnMount } = this.props; if (openPopupOnMount !== prevOpenPopupOnMount) { this.openPopup(); } } handleLabelClick() { let { isFocusOnLabelClick } = this.props; if (isFocusOnLabelClick && this.dateField && this.dateField.focus) { this.dateField.focus(); } } render() { let { labelName, isMandatory, isDateTime, validationMessage, validationPalette, textBoxSize, textBoxVariant, isReadOnly, value, errorType, isDisabled, title, popupGroup, dataId, dataSelectorId, validationRuleMessage, validationRulePalette, onKeyDown, infoText, borderColor, fieldSize, datePattern, isLocked, lockedInfoText, lockedValueText, labelPalette, labelCustomClass, htmlId, timeZone, i18nKeys, iconOnHover, customProps, renderLabelProps } = this.props; const { LabelProps = {}, DateWidgetProps = {}, ValidationMessageProps1 = {}, ValidationMessageProps2 = {} } = customProps; let getAriaId = htmlId ? htmlId : this.getNextId(); return /*#__PURE__*/React.createElement("div", { className: `${style.container} ${isDisabled ? style.disabled : isReadOnly ? style.readonly : ''}`, "data-title": isDisabled ? title : null, "data-selector-id": dataSelectorId }, labelName && /*#__PURE__*/React.createElement(FieldContainer, { infoText: infoText, isLocked: isLocked, lockedInfoText: lockedInfoText, renderProps: renderLabelProps }, /*#__PURE__*/React.createElement(Label, { text: labelName, htmlFor: getAriaId, size: fieldSize === 'small' ? 'small' : 'medium', palette: isMandatory ? 'mandatory' : isDisabled ? 'primary' : labelPalette, onClick: isDisabled || isReadOnly ? null : this.handleLabelClick, customClass: `${style.fieldLabel} ${labelCustomClass} ${isMandatory ? style.labelMandatory : ''}`, dataId: isDisabled ? `${dataId}_label_disabled` : isMandatory ? `${dataId}_label_mandatory` : `${dataId}_label`, ...LabelProps, id: labelName })), /*#__PURE__*/React.createElement("div", { className: `${style.fieldContainer} ${labelName ? style[`fieldMargin_${fieldSize}`] : ''}` }, isLocked && lockedValueText ? /*#__PURE__*/React.createElement("div", { className: style.lockText }, lockedValueText) : /*#__PURE__*/React.createElement(DateWidget, { textBoxSize: fieldSize === 'medium' ? textBoxSize : 'xsmall', textBoxVariant: textBoxVariant, isDateTime: isDateTime, onSelect: this.handleChange, timeZone: timeZone, getRef: this.handleGetRef, isReadOnly: isReadOnly, value: value, popupGroup: popupGroup, dataId: `${dataId}(formatted_${labelName})`, onKeyDown: onKeyDown, borderColor: borderColor, isDisabled: isDisabled, dateFormat: datePattern, isEditable: true, htmlId: getAriaId, i18nKeys: i18nKeys, iconOnHover: iconOnHover, ...DateWidgetProps, a11y: { ariaLabelledby: labelName } }), validationMessage && /*#__PURE__*/React.createElement(ValidationMessage, { text: validationMessage, palette: validationPalette, type: errorType, htmlFor: getAriaId, dataId: `${dataId}_ValidationMessage`, ...ValidationMessageProps1 }), validationRuleMessage && /*#__PURE__*/React.createElement(ValidationMessage, { text: validationRuleMessage, palette: validationRulePalette, type: errorType, htmlFor: getAriaId, dataId: `${dataId}_ValidationRuleMessage`, ...ValidationMessageProps2 }))); } } DateField.propTypes = propTypes; DateField.defaultProps = defaultProps; // if (__DOCS__) { // DateField.docs = { // componentGroup: 'Form Fields', // folderName: 'General' // }; // }