UNPKG

@zohodesk/dot

Version:

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

160 lines (149 loc) 4.76 kB
/**** Libraries ****/ import React, { memo, useRef, useState } from 'react'; import { defaultProps } from "./props/defaultProps"; import { propTypes } from "./props/propTypes"; /**** Components ****/ import Label from '@zohodesk/components/es/v1/Label/Label'; import TextBox from '@zohodesk/components/es/v1/TextBox/TextBox'; import ValidationMessage from "../ValidationMessage/ValidationMessage"; import { useUniqueId } from '@zohodesk/components/es/Provider/IdProvider'; import FieldContainer from "../FieldContainer/FieldContainer"; /**** CSS ****/ import style from "../../../../form/fields/Fields.module.css"; function CurrencyField(props) { let { labelName, id, textBoxSize, textBoxVariant, textBoxType, isMandatory, validationMessage, validationPalette, maxLength, isReadOnly, value, errorType, isDisabled, title, dataId, validationRuleMessage, validationRulePalette, onKeyDown, placeHolder, infoText, borderColor, fieldSize, labelPalette, labelCustomClass, htmlId, dataSelectorId, lockedInfoText, isLocked, needReadOnlyStyle, isClickable, userCurrencyType, customProps, formatCurrency, renderLabelProps, onBlur, onFocus, getRef, onChange } = props; const { LabelProps = {}, TextBoxProps = {}, ValidationMessageProps1 = {}, ValidationMessageProps2 = {} } = customProps; const getNextId = useUniqueId(); const [isActive, setActive] = useState(false); const textBox = useRef(null); let formatValue = value; if (!isActive) { formatValue = formatCurrency(value, userCurrencyType); } else { formatValue = value; } let uniqueId = htmlId ? htmlId : getNextId(); const isFocusable = !needReadOnlyStyle || !isReadOnly && needReadOnlyStyle; function handleChange(value) { onChange && onChange(id, value); } function handleGetRef(el) { textBox.current = el; getRef && getRef(el, id); } function handleonFocus() { setActive(true); onFocus && onFocus(); } function handleonBlur() { setActive(false); onBlur && onBlur(); } 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, size: fieldSize === 'small' ? 'small' : 'medium', palette: isMandatory ? 'mandatory' : isDisabled ? 'primary' : labelPalette, customClass: `${style.fieldLabel} ${labelCustomClass} ${isMandatory ? style.labelMandatory : ''}`, htmlFor: uniqueId, dataId: isDisabled ? `${dataId}_label_disabled` : isMandatory ? `${dataId}_label_mandatory` : `${dataId}_label`, ...LabelProps })), /*#__PURE__*/React.createElement("div", { className: `${style.fieldContainer} ${labelName ? style[`fieldMargin_${fieldSize}`] : ''}` }, /*#__PURE__*/React.createElement(TextBox, { htmlId: uniqueId, id: id, type: textBoxType, variant: textBoxVariant, size: fieldSize === 'medium' ? textBoxSize : 'xsmall', maxLength: maxLength, isReadOnly: isReadOnly, inputRef: handleGetRef, value: formatValue, onChange: handleChange, dataId: dataId, onBlur: handleonBlur, onFocus: isFocusable && handleonFocus, onKeyDown: onKeyDown, placeHolder: placeHolder, borderColor: borderColor, isDisabled: isDisabled, needEffect: isReadOnly || isDisabled ? false : true, isClickable: isClickable, needReadOnlyStyle: needReadOnlyStyle, ...TextBoxProps }), validationMessage && /*#__PURE__*/React.createElement(ValidationMessage, { text: validationMessage, palette: validationPalette, type: errorType, dataId: `${dataId}_ValidationMessage`, htmlFor: uniqueId, ...ValidationMessageProps1 }), validationRuleMessage && /*#__PURE__*/React.createElement(ValidationMessage, { text: validationRuleMessage, palette: validationRulePalette, type: errorType, htmlFor: uniqueId, dataId: `${dataId}_ValidationRuleMessage`, ...ValidationMessageProps2 }))); } CurrencyField.propTypes = propTypes; CurrencyField.defaultProps = defaultProps; const MemoizedCurrencyField = /*#__PURE__*/memo(CurrencyField); MemoizedCurrencyField.propTypes = propTypes; MemoizedCurrencyField.defaultProps = defaultProps; MemoizedCurrencyField.displayName = 'CurrencyField'; export default MemoizedCurrencyField;