React - 활성화 된 focus out 하기

2023. 6. 23. 15:05개발하는중/react

728x90
반응형

text 입력 후 엔터 입력시 포커스 아웃된다

 

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
import TextField from "@mui/material/TextField";
import React from "react";
import Container from "@mui/material/Container";
import {Card, CardContent, CardHeader, Divider, Grid} from "@mui/material";
import Box from "@mui/material/Box";
 
 
const Focus = () => {
 
    const enterKeyEventhandler = (e) => {
        if (e.key === 'Enter') {
            document.activeElement.blur();
        }
    }
 
 
    return (
        <>
            <Container maxWidth="lg">
                <Grid
                    container
                    direction="row"
                    justifyContent="center"
                    alignItems="stretch"
                    spacing={3}
                >
                    <Grid item xs={12}>
                        <Card>
                            <CardHeader title="FOCUS" />
                            <Divider />
                           <CardContent>
                               <Box
                                   sx={{
                                       '& .MuiTextField-root': { m: 1, width: '25ch' }
                                   }}
                                   noValidate
                                   autoComplete="off"
                               >
                                   <div>
                                       <TextField
                                           label="focus"
                                           id="focus"
                                           name="focus"
                                           InputLabelProps={{
                                               shrink: true
                                           }}
                                           onKeyPress={enterKeyEventhandler}
                                       />
                                       <TextField
                                           label="focus1"
                                           id="focus1"
                                           name="focus1"
                                           InputLabelProps={{
                                               shrink: true
                                           }}
                                           onKeyPress={enterKeyEventhandler}
                                       />
                                   </div>
                               </Box>
 
                           </CardContent>
 
                        </Card>
                    </Grid>
 
                </Grid>
            </Container>
        </>
 
    );
 
}
 
export default Focus;
cs
728x90