forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
InputAdornments.tsx
169 lines (153 loc) · 3.96 KB
/
InputAdornments.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import * as React from 'react';
import Box from '@mui/material/Box';
import ButtonUnstyled from '@mui/base/ButtonUnstyled';
import InputUnstyled, {
InputUnstyledProps,
inputUnstyledClasses,
} from '@mui/base/InputUnstyled';
import Visibility from '@mui/icons-material/Visibility';
import VisibilityOff from '@mui/icons-material/VisibilityOff';
import { styled } from '@mui/system';
const blue = {
100: '#DAECFF',
200: '#80BFFF',
400: '#3399FF',
500: '#007FFF',
600: '#0072E5',
};
const grey = {
50: '#F3F6F9',
100: '#E7EBF0',
200: '#E0E3E7',
300: '#CDD2D7',
400: '#B2BAC2',
500: '#A0AAB4',
600: '#6F7E8C',
700: '#3E5060',
800: '#2D3843',
900: '#1A2027',
};
const StyledInputRoot = styled('div')(
({ theme }) => `
font-family: IBM Plex Sans, sans-serif;
font-weight: 400;
border-radius: 12px;
color: ${theme.palette.mode === 'dark' ? grey[300] : grey[500]};
background: ${theme.palette.mode === 'dark' ? grey[900] : '#fff'};
border: 1px solid ${theme.palette.mode === 'dark' ? grey[700] : grey[200]};
box-shadow: 0px 2px 2px ${theme.palette.mode === 'dark' ? grey[900] : grey[50]};
display: flex;
align-items: center;
justify-content: center;
&.${inputUnstyledClasses.focused} {
border-color: ${blue[400]};
outline: 3px solid ${theme.palette.mode === 'dark' ? blue[500] : blue[200]};
}
&:hover {
border-color: ${blue[400]};
}
`,
);
const StyledInputElement = styled('input')(
({ theme }) => `
font-size: 0.875rem;
font-family: inherit;
font-weight: 400;
line-height: 1.5;
flex-grow: 1;
color: ${theme.palette.mode === 'dark' ? grey[300] : grey[900]};
background: inherit;
border: none;
border-radius: inherit;
padding: 12px 12px;
outline: 0;
`,
);
const IconButton = styled(ButtonUnstyled)(
({ theme }) => `
display: inline-flex;
align-items: center;
justify-content: center;
border: none;
background: inherit;
cursor: pointer;
color: ${theme.palette.mode === 'dark' ? grey[300] : grey[700]};
`,
);
const InputAdornment = styled('div')`
margin: 8px;
display: inline-flex;
align-items: center;
justify-content: center;
`;
const CustomInput = React.forwardRef(function CustomInput(
props: InputUnstyledProps,
ref: React.ForwardedRef<HTMLDivElement>,
) {
const { slots, ...other } = props;
return (
<InputUnstyled
slots={{
root: StyledInputRoot,
input: StyledInputElement,
...slots,
}}
{...other}
ref={ref}
/>
);
});
interface State {
amount: string;
password: string;
weight: string;
weightRange: string;
showPassword: boolean;
}
export default function InputAdornments() {
const [values, setValues] = React.useState<State>({
amount: '',
password: '',
weight: '',
weightRange: '',
showPassword: false,
});
const handleChange =
(prop: keyof State) => (event: React.ChangeEvent<HTMLInputElement>) => {
setValues({ ...values, [prop]: event.target.value });
};
const handleClickShowPassword = () => {
setValues({
...values,
showPassword: !values.showPassword,
});
};
const handleMouseDownPassword = (event: React.MouseEvent<HTMLButtonElement>) => {
event.preventDefault();
};
return (
<Box sx={{ display: 'flex', '& > * + *': { ml: 1 } }}>
<CustomInput
id="outlined-start-adornment"
startAdornment={<InputAdornment>kg</InputAdornment>}
/>
<CustomInput
id="outlined-adornment-password"
type={values.showPassword ? 'text' : 'password'}
value={values.password}
onChange={handleChange('password')}
endAdornment={
<InputAdornment>
<IconButton
aria-label="toggle password visibility"
onClick={handleClickShowPassword}
onMouseDown={handleMouseDownPassword}
>
{values.showPassword ? <VisibilityOff /> : <Visibility />}
</IconButton>
</InputAdornment>
}
/>
</Box>
);
}