-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
/
InputAdornments.tsx
134 lines (127 loc) · 3.98 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
import React from 'react';
import clsx from 'clsx';
import { makeStyles, Theme, createStyles } from '@material-ui/core/styles';
import IconButton from '@material-ui/core/IconButton';
import Input from '@material-ui/core/Input';
import InputLabel from '@material-ui/core/InputLabel';
import InputAdornment from '@material-ui/core/InputAdornment';
import FormHelperText from '@material-ui/core/FormHelperText';
import FormControl from '@material-ui/core/FormControl';
import TextField from '@material-ui/core/TextField';
import MenuItem from '@material-ui/core/MenuItem';
import Visibility from '@material-ui/icons/Visibility';
import VisibilityOff from '@material-ui/icons/VisibilityOff';
const ranges = [
{
value: '0-20',
label: '0 to 20',
},
{
value: '21-50',
label: '21 to 50',
},
{
value: '51-100',
label: '51 to 100',
},
];
const useStyles = makeStyles((theme: Theme) =>
createStyles({
root: {
display: 'flex',
flexWrap: 'wrap',
},
margin: {
margin: theme.spacing(1),
},
withoutLabel: {
marginTop: theme.spacing(3),
},
textField: {
flexBasis: 200,
},
}),
);
function InputAdornments() {
const classes = useStyles();
const [values, setValues] = React.useState({
amount: '',
password: '',
weight: '',
weightRange: '',
showPassword: false,
});
const handleChange = (prop: 'amount' | 'password' | 'weight' | 'weightRange') => (
event: React.ChangeEvent<HTMLInputElement>,
) => {
setValues({ ...values, [prop]: event.target.value });
};
const handleClickShowPassword = () => {
setValues({ ...values, showPassword: !values.showPassword });
};
return (
<div className={classes.root}>
<TextField
label="With normal TextField"
id="simple-start-adornment"
className={clsx(classes.margin, classes.textField)}
InputProps={{
startAdornment: <InputAdornment position="start">Kg</InputAdornment>,
}}
/>
<TextField
select
label="With Select"
className={clsx(classes.margin, classes.textField)}
value={values.weightRange}
onChange={handleChange('weightRange')}
InputProps={{
startAdornment: <InputAdornment position="start">Kg</InputAdornment>,
}}
>
{ranges.map(option => (
<MenuItem key={option.value} value={option.value}>
{option.label}
</MenuItem>
))}
</TextField>
<FormControl fullWidth className={classes.margin}>
<InputLabel htmlFor="adornment-amount">Amount</InputLabel>
<Input
id="adornment-amount"
value={values.amount}
onChange={handleChange('amount')}
startAdornment={<InputAdornment position="start">$</InputAdornment>}
/>
</FormControl>
<FormControl className={clsx(classes.margin, classes.withoutLabel, classes.textField)}>
<Input
id="adornment-weight"
value={values.weight}
onChange={handleChange('weight')}
endAdornment={<InputAdornment position="end">Kg</InputAdornment>}
aria-describedby="weight-helper-text"
aria-label="Weight"
/>
<FormHelperText id="weight-helper-text">Weight</FormHelperText>
</FormControl>
<FormControl className={clsx(classes.margin, classes.textField)}>
<InputLabel htmlFor="adornment-password">Password</InputLabel>
<Input
id="adornment-password"
type={values.showPassword ? 'text' : 'password'}
value={values.password}
onChange={handleChange('password')}
endAdornment={
<InputAdornment position="end">
<IconButton aria-label="Toggle password visibility" onClick={handleClickShowPassword}>
{values.showPassword ? <Visibility /> : <VisibilityOff />}
</IconButton>
</InputAdornment>
}
/>
</FormControl>
</div>
);
}
export default InputAdornments;