функционал загрузки фотографии вынесен в отдельный блок

This commit is contained in:
admin 2024-12-10 19:45:51 +03:00
parent 0a1d4067a8
commit ca360b2451
3 changed files with 71 additions and 35 deletions

View File

@ -1,12 +1,8 @@
import * as React from 'react'; import * as React from 'react';
import Avatar from '@mui/material/Avatar'; import {Button, TextField, Grid, Box,
import Button from '@mui/material/Button'; Typography, Container
import CssBaseline from '@mui/material/CssBaseline'; } from '@mui/material';
import TextField from '@mui/material/TextField'; import Photo from './photo/photo';
import Grid from '@mui/material/Grid';
import Box from '@mui/material/Box';
import Typography from '@mui/material/Typography';
import Container from '@mui/material/Container';
import { createTheme, ThemeProvider } from '@mui/material/styles'; import { createTheme, ThemeProvider } from '@mui/material/styles';
import axios from 'axios'; import axios from 'axios';
import student_icon from './student-icon.png'; import student_icon from './student-icon.png';
@ -22,7 +18,7 @@ const theme = createTheme();
const SingUpPage = (): React.ReactElement => { const SingUpPage = (): React.ReactElement => {
const [avatarUrl, setAvatarUrl] = useState<string | null>(null); const [selectedPhoto, setSelectedPhoto] = useState(null);
const [selectedOption, setSelectedOption] = useState<string | null>(null); const [selectedOption, setSelectedOption] = useState<string | null>(null);
const handleChange = (selectedOption) => { const handleChange = (selectedOption) => {
setSelectedOption(selectedOption); setSelectedOption(selectedOption);
@ -48,28 +44,14 @@ const SingUpPage = (): React.ReactElement => {
onClose(); onClose();
}; };
*/ */
const handleAvatarClick = () => {
const fileInput = document.getElementById('avatar-input') as HTMLInputElement; const handlePhotoChange = (photo) => {
fileInput.click(); setSelectedPhoto(photo); // Сохраняем выбранное изображение
}; };
const handleAvatarChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = () => {
setAvatarUrl(reader.result as string);
};
reader.readAsDataURL(file);
};
return ( return (
<ThemeProvider theme={theme}> <ThemeProvider theme={theme}>
<Container component="main" maxWidth="xs"> <Container component="main" maxWidth="xs">
<CssBaseline />
<Box <Box
sx={{ sx={{
marginTop: 8, marginTop: 8,
@ -124,15 +106,7 @@ const SingUpPage = (): React.ReactElement => {
justifyContent="center" justifyContent="center"
sx={{ paddingTop: 7, paddingLeft: 3 }} sx={{ paddingTop: 7, paddingLeft: 3 }}
> >
<Avatar src={avatarUrl || student_icon} onClick={handleAvatarClick} sx={{ width: 200, height: 200 }}/> <Photo defaultPhoto={student_icon} onPhotoChange={handlePhotoChange} />
<input
type="file"
name="image"
accept="image/*"
onChange={handleAvatarChange}
style={{ display: 'none' }}
id="avatar-input"
/>
</Grid> </Grid>
<Grid <Grid
container container

View File

@ -0,0 +1,62 @@
import React, { useState } from 'react';
import styled from '@emotion/styled';
import Avatar from "@mui/material/Avatar";
const PhotoStyled = styled.div`
position: relative;
display: inline-block;
cursor: pointer;
`;
const AvatarStyled = styled(Avatar)`
width: 200px;
height: 200px;
`;
const InputStyled = styled.input`
display: none;
`;
function Photo({ defaultPhoto, onPhotoChange }) {
const [photo, setPhoto] = useState(defaultPhoto);
const handleFileChange = (event) => {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
const newPhoto = e.target.result;
setPhoto(newPhoto);
if (onPhotoChange) onPhotoChange(newPhoto);
};
reader.readAsDataURL(file);
}
};
const handleAvatarClick = () => {
const fileInput = document.getElementById('fileInput') as HTMLInputElement;
fileInput.click();
}
return (
<PhotoStyled>
<AvatarStyled
src={photo}
alt="Выберите фотографию"
onClick={handleAvatarClick}
/>
<InputStyled
id="fileInput"
type="file"
accept="image/*"
onChange={handleFileChange}
/>
</PhotoStyled>
);
}
export default Photo;

View File