UNPKG

@parkassist/pa-ui-library

Version:
217 lines 5.93 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { cloneDeep } from 'lodash'; import { checkIfTruthyOrZero } from '../../utils'; import Palette from '../../../constants/Palette'; import FontStyles from '../../../constants/FontStyles'; import { EndAdornment, StartAdornment } from '../Adornments/Adornments'; import React from 'react'; const getHelperText = (helperText, errorHelperText, showError) => { if (errorHelperText) { return showError ? errorHelperText : helperText; } return helperText; }; export const renderHelperText = (helperText, errorHelperText, showError, hideEmptySpace) => { return getHelperText(helperText, errorHelperText, showError) || (hideEmptySpace ? null : ' '); }; export const adjustNumberValue = ({ event, min, max, disabledEmptyNumericValue }) => { const newEvent = cloneDeep(event); const { value } = event.target; if (value === '') { if (!disabledEmptyNumericValue) return event; newEvent.target.value = 0; return newEvent; } if (min || max) { let newValue = +value; newValue = Boolean(checkIfTruthyOrZero(min) && newValue < min) ? min : newValue; newValue = Boolean(checkIfTruthyOrZero(max) && newValue > max) ? max : newValue; newEvent.target.value = newValue; return newEvent; } return event; }; export const getValue = (value, type, disabledEmptyNumericValue) => { if (!value) { if (disabledEmptyNumericValue && type === 'number') { return '0'; } return ''; } return value; }; export const getType = (type, showPassword) => { return type === 'password' && showPassword ? 'text' : type; }; export const handleCopy = (value, onCopy) => { navigator.clipboard.writeText(value); onCopy(true); setTimeout(() => { onCopy(false); }, 2000); }; export const handleFocus = (event, onFocus, changeFocusState) => { onFocus && onFocus(event); changeFocusState(true); }; export const handleBlur = (event, onBlur, changeFocusState) => { onBlur && onBlur(event); changeFocusState(false); }; export const getSXStyles = ({ style, topSpace }) => { return Object.assign(Object.assign(Object.assign({}, style), topSpace && { marginTop: '5px' }), { '& input[type=number]': { 'MozApperance': 'textfield' }, '& input[type=number]::-webkit-outer-spin-button': { 'WebkitAppearance': 'none', margin: 0 }, '& input[type=number]::-webkit-inner-spin-button': { 'WebkitAppearance': 'none', margin: 0 } }); }; export const getInputLabelProps = ({ InputLabelProps, type }) => { return Object.assign(Object.assign(Object.assign({}, InputLabelProps), (type === 'file' || type === 'color') && { shrink: true }), { sx: { color: Palette.DIM_GREY, font: FontStyles.INPUT_FONT, '&.MuiInputLabel-shrink': { font: FontStyles.INPUT_SHRINK_LABEL, lineHeight: '25px' }, '&.Mui-error': { color: Palette.DIM_GREY }, '&.MuiInputLabel-shrink.Mui-focused': { color: Palette.LIGHT_BLACK }, '&.MuiInputLabel-shrink.Mui-error': { color: Palette.ERROR_RED } } }); }; export const getinputProps = ({ inputProps, readOnly, maxLength, acceptedFiles, type, multiple }) => { return Object.assign(Object.assign(Object.assign({}, inputProps), { readOnly, maxLength, accept: acceptedFiles }), type === 'files' && multiple && { multiple: true }); }; export const getInputProps = ({ InputProps, iconStart, iconEnd, prefixText, suffixText, allowCopying, allowShowingPassword, disabled, setCopied, onShowPassword, type, copied, value, showPassword }) => { return Object.assign(Object.assign(Object.assign(Object.assign({}, InputProps), (prefixText || iconStart) && { startAdornment: _jsx(StartAdornment, { text: prefixText, icon: iconStart }) }), (suffixText || iconEnd || allowCopying || allowShowingPassword) && { endAdornment: _jsx(EndAdornment, { text: suffixText, icon: iconEnd, copyAdornment: type === 'text' && allowCopying, copied: copied, onCopy: () => handleCopy(value, setCopied), passwordAdornment: type === 'password' && allowShowingPassword, showPassword: showPassword, onShowPassword: onShowPassword }) }), { sx: { color: Palette.LIGHT_BLACK, font: FontStyles.INPUT_FONT, borderWidth: '1px', '&.Mui-error .MuiOutlinedInput-notchedOutline': { borderColor: Palette.ERROR_RED, borderWidth: '1px' }, '&:hover .MuiOutlinedInput-notchedOutline': { borderColor: !disabled && Palette.LIGHT_BLACK }, '&.Mui-error:hover .MuiOutlinedInput-notchedOutline': { borderColor: !disabled && Palette.ERROR_RED }, '&.Mui-focused .MuiOutlinedInput-notchedOutline': { borderColor: Palette.LIGHT_BLACK, borderWidth: '1px' }, '&.Mui-focused.Mui-error .MuiOutlinedInput-notchedOutline': { borderColor: Palette.ERROR_RED, borderWidth: '2px' }, '& .MuiOutlinedInput-notchedOutline': { borderColor: Palette.DIM_GREY }, '& legend': { fontSize: '11px' }, '& .MuiInputAdornment-root img': { filter: disabled ? Palette.FILTER_VERY_LIGHT_GREY : Palette.FILTER_DIM_GREY }, '&:hover .MuiInputAdornment-root img, &.Mui-focused .MuiInputAdornment-root img': { filter: !disabled && Palette.FILTER_BLACK }, '&.MuiInputBase-adornedStart': { paddingLeft: '8px' }, '&.MuiInputBase-adornedEnd': { paddingRight: '8px' } } }); }; export const getFormHelperTextProps = () => { return { sx: { font: FontStyles.BODY2_FONT, color: Palette.DIM_GREY, letterSpacing: 0, '&.Mui-error': { color: Palette.ERROR_RED } } }; };