-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextField.tsx
More file actions
139 lines (123 loc) · 2.95 KB
/
Copy pathTextField.tsx
File metadata and controls
139 lines (123 loc) · 2.95 KB
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
import React, {ChangeEvent, HTMLAttributes, useRef} from 'react'
import MuiTextField from '@mui/material/TextField'
import styled from 'styled-components'
import Flex from './Flex'
type Props = {
className?: string
text: string
onTextChanged: (e: ChangeEvent<HTMLInputElement>) => void
label?: string
maxLength?: number
itemBefore?: React.ReactNode
errorText?: string
disabled?: boolean
borderColor?: Color
inputBackgroundColor?: Color
maxRows?: number
disableMultiline?: boolean
} & HTMLAttributes<HTMLElement>
type Color = 'primary' | 'secondary' | 'accent'
const TextField = ({
className,
text,
onTextChanged,
label,
maxLength,
itemBefore,
errorText,
disabled,
borderColor,
inputBackgroundColor,
maxRows,
disableMultiline,
...props
}: Props) => {
const inputRef = useRef<HTMLDivElement>()
return (
<Flex
direction="column"
alignItems="flex-start"
gap="4px"
alignSelf="stretch"
>
<InputBox
className={className}
itemBefore={itemBefore}
borderColor={borderColor || 'accent'}
inputBackgroundColor={inputBackgroundColor}
onClick={() => inputRef.current?.focus()}
{...props}
>
{itemBefore}
<StyledTextField
placeholder={label}
inputRef={inputRef}
value={text}
onChange={onTextChanged}
multiline={!disableMultiline}
minRows={1}
maxRows={maxRows || 6}
fullWidth
disabled={disabled}
inputProps={{
maxLength,
}}
helperText={maxLength && `${text.length}/${maxLength}`}
spellCheck={false}
/>
</InputBox>
{errorText && <ErrorMessage>{errorText}</ErrorMessage>}
</Flex>
)
}
const InputBox = styled.div<{
itemBefore?: React.ReactNode
borderColor: Color
inputBackgroundColor?: Color
}>`
width: 100%;
padding: 12px;
padding-top: ${(props) => (props.itemBefore ? '12px' : '4px')};
padding-bottom: 4px;
border-radius: 12px;
border: ${(props) => `1px solid var(--color-${props.borderColor})`};
background-color: ${(props) => {
if (!props.inputBackgroundColor) {
return 'unset'
}
return `var(--color-${props.inputBackgroundColor})`
}};
`
const ErrorMessage = styled.span`
color: var(--color-danger);
font-size: 14px;
`
const StyledTextField = styled(MuiTextField)<{
disabled?: boolean
multiline?: boolean
}>`
.MuiInputBase-root {
padding: 0;
font-family: soleil, sans-serif;
color: var(--color-secondary);
margin: 4px 0;
.MuiOutlinedInput-notchedOutline {
border: none;
}
}
.MuiInputBase-input {
padding: ${(props) => !props.multiline && '0'};
}
.MuiFormHelperText-root {
font-family: soleil, sans-serif;
color: var(--color-grey);
text-align: right;
}
.MuiInput-underline::before {
all: unset;
}
.MuiInput-underline::after {
all: unset;
}
`
export default TextField