Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | import classNames from 'classnames'; import React, { useMemo } from 'react'; import { Box, SxProps, TooltipProps } from '@mui/material'; import Icon from '../Icon/Icon'; import { IconName } from '../Icon/icons'; import './styles.scss'; import { isWhitespaces } from '../../utils'; import IconTooltip from '../IconTooltip'; import TruncatedTextArea from './TruncatedTextArea'; type TextfieldType = 'text' | 'textarea' | 'number'; type Handler = (value: unknown) => void; export interface TruncateConfig { maxHeight: number; showMoreText: string; showLessText: string; showTextStyle?: Record<string, any>; valueTextStyle?: Record<string, any>; } export interface Props { autoFocus?: boolean; className?: string; disabled?: boolean; disabledCharacters?: string[]; display?: boolean; errorText?: string; helperText?: string; iconLeft?: IconName; iconRight?: IconName; id: string; label: string; min?: number; max?: number; onBlur?: () => void; onChange?: Handler; onClickRightIcon?: () => void; onFocus?: () => void; onKeyDown?: (key: string) => void; placeholder?: string; preserveNewlines?: boolean; readonly?: boolean; required?: boolean; // Since useStyles is deprecated, we can start passing in element specific styles in here // For now only the `textarea` has styles passed into it styles?: Record<string, Record<string, unknown>>; sx?: SxProps; tooltipTitle?: TooltipProps['title']; truncateConfig?: TruncateConfig; type: TextfieldType; warningText?: string; value?: string | number; } export default function TextField(props: Props): JSX.Element { const { autoFocus, label, className, disabled, disabledCharacters, display, errorText, helperText, iconLeft, iconRight, id, min, max, onBlur, onChange, onClickRightIcon, onFocus, onKeyDown, placeholder, preserveNewlines, readonly, required, styles, sx, tooltipTitle, truncateConfig, type, value, warningText, } = props; const textfieldClass = classNames({ 'textfield-value': true, 'textfield-value--disabled': disabled, 'textfield-value--error': !!errorText, 'textfield-value--warning': !!warningText, 'textfield-value--readonly': readonly, }); const textfieldOnChange = (event: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>) => { const textValue = event.target.value; if (isWhitespaces(textValue)) { return; } if (onChange) { onChange(textValue); } }; const onKeyDownHandler = (e: React.KeyboardEvent<HTMLInputElement>) => { if (disabledCharacters && disabledCharacters.includes(e.key)) { e?.preventDefault(); return; } if (onKeyDown) { onKeyDown(e.key); } }; const renderRightIcon = () => { if (iconRight === 'cancel' && !value) { return null; } return ( <button onClick={onClickRightIcon} className='textfield-value--icon-container'> <Icon name={iconRight!} className={`textfield-value--icon-right${iconRight === 'cancel' ? '--cancel' : ''}`} /> </button> ); }; const typeProps: { [key: string]: unknown } = { type, }; if (type === 'number') { if (min !== undefined) { typeProps.min = min; } if (max !== undefined) { typeProps.max = max; } } const displayComponent = useMemo(() => { if (!display) { return null; } if (type === 'textarea' && truncateConfig) { return <TruncatedTextArea preserveNewlines={preserveNewlines} truncateConfig={truncateConfig} value={value} />; } return <p className={`textfield-value--display${preserveNewlines ? ' preserve-newlines' : ''}`}>{value}</p>; }, [display, preserveNewlines, truncateConfig, type, value]); return ( <Box className={`textfield ${className}`} sx={sx}> <label htmlFor={id} className='textfield-label'> {required && label ? `${label} *` : label} {tooltipTitle && <IconTooltip placement='top' title={tooltipTitle} />} </label> {!display && (type === 'text' || type === 'number' ? ( <div id={id} className={textfieldClass}> {iconLeft && <Icon name={iconLeft} className='textfield-value--icon-left' />} <input autoFocus={autoFocus} value={type === 'number' ? value : value || ''} disabled={readonly || disabled} placeholder={placeholder} onChange={textfieldOnChange} onBlur={onBlur} onFocus={onFocus} onKeyDown={onKeyDownHandler} onWheel={(e) => e.currentTarget.blur()} required={required} {...typeProps} /> {iconRight ? renderRightIcon() : null} </div> ) : ( <textarea autoFocus={autoFocus} className={textfieldClass} value={value} disabled={readonly || disabled} placeholder={placeholder} onChange={textfieldOnChange} onBlur={onBlur} onFocus={onFocus} required={required} style={(styles || {}).textarea} /> ))} {displayComponent} {errorText && ( <div className='textfield-label-container'> <Icon name='error' className='textfield-error-text--icon' /> <label htmlFor={id} className='textfield-error-text'> {errorText} </label> </div> )} {warningText && ( <div className='textfield-label-container'> <Icon name='warning' className='textfield-warning-text--icon' /> <label htmlFor={id} className='textfield-warning-text'> {warningText} </label> </div> )} {helperText && ( <label htmlFor={id} className='textfield-help-text'> {helperText} </label> )} </Box> ); } |