-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConditionTextField.tsx
155 lines (145 loc) · 4.44 KB
/
ConditionTextField.tsx
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
import React, { useState, useRef, useEffect } from 'react';
import Box from '@mui/material/Box';
import StyledTextField from '../metadataKeyValue/StyledTextField';
import '../metadataKeyValue/style.scss';
import { ThemeProvider, Typography } from '@mui/material';
import theme from '../../../../theme';
import {useDebounce} from "react-use";
export function ConditionTextField({
disabled,
value,
onChange,
placeholder,
helperText,
autoFocus,
isErrored,
}: {
disabled: boolean;
value?: string;
onChange: (newVal?: string) => void;
placeholder?: string;
helperText?: string;
shouldHighlightIfEmpty?: boolean;
autoFocus?: boolean;
isErrored?: boolean;
}) {
const [currentValue, setCurrentValue] = useState(value);
const textFieldRef = useRef<HTMLDivElement | null>(null);
const textFieldWrapperContainerRef = useRef<HTMLDivElement | null>(null);
const [focused, setFocused] = useState(false);
const copyTextFieldRef = useRef<HTMLDivElement | null>(null);
const [inputWidth, setInputWidth] = useState<number>(copyTextFieldRef.current?.scrollWidth ?? 0);
useEffect(() => {
setInputWidth(copyTextFieldRef.current?.scrollWidth ?? 0);
}, [currentValue, placeholder, focused]);
useEffect(() => {
setCurrentValue(value);
}, [value]);
useEffect(() => {
function handleClickOutside(event: any) {
if (
textFieldWrapperContainerRef.current &&
!textFieldWrapperContainerRef.current.contains(event.target)
) {
textFieldRef.current?.blur();
}
}
document.addEventListener('mousedown', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [textFieldWrapperContainerRef]);
const handleFocus = () => {
setFocused(true);
};
const handleBlur = () => {
setFocused(false);
};
const handleKeyDown = (event: any) => {
if (event.key === 'Enter' || event.key === 'Escape') {
textFieldRef.current?.blur();
}
};
useDebounce(() => {
onChange(currentValue);
},200, [currentValue]);
return (
<ThemeProvider theme={theme}>
<Box
sx={{
display: 'flex',
height: '100%',
flexDirection: 'column',
width: !!inputWidth ? inputWidth : 'min-content',
minWidth: '40px',
'.MuiFormHelperText-root': {
width: 'max-content',
},
}}
onMouseDown={(e) => {
if (disabled) {
e.preventDefault();
} //When disabled, make text field not focused on a regular click
}}
ref={textFieldWrapperContainerRef}
>
<Typography
variant={'medium'}
ref={copyTextFieldRef}
style={{
display: 'flex',
zIndex: -1,
color: 'transparent',
borderRadius: '8px',
padding: '0px 10px',
flexWrap: 'nowrap',
width: 'max-content',
height: '0px',
boxSizing: 'border-box',
}}
>
{currentValue ? currentValue : focused ? undefined : placeholder}
</Typography>
{/*This is a hidden div that is used to calculate the width of the input field*/}
<StyledTextField
helperTextPaddingBottom={'0px'}
isErrored={isErrored}
setBorder={!disabled}
backgroundColor={'rgba(255, 255, 255, 1)'}
backgroundColorHover={'rgba(248, 250, 252, 1)'}
backgroundColorFocus={'rgba(248, 250, 252, 1)'}
helperTextPaddingLeft={'0px'}
disabled={disabled}
borderRadius={'8px'}
changeColorOnHover={!disabled}
inputRef={textFieldRef}
helperText={helperText}
autoFocus={autoFocus}
onFocus={handleFocus}
onBlur={handleBlur}
InputProps={{
autoComplete: 'off',
readOnly: disabled,
sx: {
input: {
'&::placeholder': {
color: 'rgba(23, 45, 50, 1)!important',
opacity: 1,
},
height: '28px',
boxSizing: 'border-box',
},
},
}}
onChange={(e: any) => {
setCurrentValue(e.target.value);
}}
onKeyDown={handleKeyDown}
value={currentValue}
placeholder={focused ? undefined : placeholder}
/>
</Box>
</ThemeProvider>
);
}
export default ConditionTextField;