-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
7 Самсонов Иван #38
base: master
Are you sure you want to change the base?
7 Самсонов Иван #38
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,146 @@ | ||
import './style.css'; | ||
import React, { FC, useState } from 'react'; | ||
import ReactDom from 'react-dom'; | ||
import { Button, Input, Select, Gapped, Modal } from '@skbkontur/react-ui'; | ||
|
||
const items = [ | ||
'Москва', | ||
'Екатеринбург', | ||
'Санкт-Петербург', | ||
'Новосибирск', | ||
'Казань', | ||
'Нижний Новгород', | ||
'Челябинск', | ||
'Самара', | ||
'Омск', | ||
'Ростов-на-Дону', | ||
'Уфа' | ||
]; | ||
|
||
type FormData = { | ||
name: string; | ||
surname: string; | ||
city: string; | ||
}; | ||
|
||
type DataState = { | ||
saved: FormData; | ||
current: FormData; | ||
}; | ||
|
||
const defaultForm = { | ||
name: '', | ||
surname: '', | ||
city: undefined | ||
}; | ||
|
||
const Form: React.FC = () => { | ||
const [data, setData] = useState({ | ||
saved: { ...defaultForm }, | ||
current: { ...defaultForm } | ||
}); | ||
const [saved, setSaved] = useState(false); | ||
const [opened, setOpened] = useState(false); | ||
const [panel, setPanel] = useState(false); | ||
function changeValue(value: string, field: string) { | ||
setData({ ...data, current: { ...data.current, [field]: value } }); | ||
} | ||
|
||
function renderModal() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если у тебя в компоненте появляется функция, которая называется |
||
const listItems = []; | ||
if (data.current.name !== data.saved.name) | ||
listItems.push( | ||
<p> | ||
Имя: было {data.saved.name} стало {data.current.name} | ||
</p> | ||
Comment on lines
+53
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это в целом тоже может быть мини-компонентом) |
||
); | ||
if (data.current.surname !== data.saved.surname) | ||
listItems.push( | ||
<p> | ||
Фамилия: была {data.saved.surname} стала {data.current.surname} | ||
</p> | ||
); | ||
if (data.saved.city !== data.current.city) | ||
listItems.push( | ||
<p> | ||
Город: был {data.saved.city} стал {data.current.city} | ||
</p> | ||
); | ||
return ( | ||
<Modal onClose={close}> | ||
<Modal.Header>Пользователь сохранен</Modal.Header> | ||
<Modal.Body>{listItems.length !== 0 && saved && <div>Измененные данные: {listItems}</div>}</Modal.Body> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Когда рендеришь массив, у каждого из детей должно быть свойство key. Сейчас его нет, и в консоли из-за этого ошибки при открытии модалки |
||
<Modal.Footer panel={panel}> | ||
<Button | ||
onClick={() => { | ||
close(); | ||
}} | ||
Comment on lines
+75
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А можно просто |
||
> | ||
Закрыть | ||
</Button> | ||
</Modal.Footer> | ||
</Modal> | ||
); | ||
} | ||
|
||
function open() { | ||
setOpened(true); | ||
} | ||
|
||
function close() { | ||
setOpened(false); | ||
setData({ ...data, saved: data.current }); | ||
setSaved(true); | ||
} | ||
Comment on lines
+90
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут внутри столько всего происходит, что это скорее не просто close, а saveAndClose какой-нибудь) Ну или абстрактный О, и кстати в принципе, почему сохранение данных происходит по закрытию модалки, а не по кнопке "Сохранить"?) |
||
|
||
return ( | ||
<> | ||
<form> | ||
<h1>Информация о пользователе</h1> | ||
<Gapped gap={14} vertical> | ||
<label> | ||
<div className="name">Имя</div> | ||
<Input | ||
placeholder="Введите имя пользователя" | ||
value={data.current.name} | ||
onValueChange={name => changeValue(name, 'name')} | ||
/> | ||
</label> | ||
<label> | ||
<div className="surname">Фамилия</div> | ||
<Input | ||
placeholder="Введите фамилию пользователя" | ||
value={data.current.surname} | ||
onValueChange={surname => changeValue(surname, 'surname')} | ||
/> | ||
</label> | ||
<label> | ||
<div className="city">Город</div> | ||
<Select | ||
placeholder="Выберите город" | ||
items={items} | ||
value={data.current.city} | ||
onValueChange={city => changeValue(city, 'city')} | ||
search | ||
/> | ||
</label> | ||
<Button | ||
use="primary" | ||
size="large" | ||
onClick={() => { | ||
open(); | ||
}} | ||
> | ||
Сохранить | ||
</Button> | ||
</Gapped> | ||
</form> | ||
{opened && renderModal()} | ||
</> | ||
); | ||
}; | ||
|
||
ReactDom.render(<Form />, document.getElementById('userForm')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Кажется, ты забыл закоммитить изменения в index.html, без него ничего не работает) |
||
/** | ||
* Итак, перед тобой пустой проект. Давай его чем-то заполним. Не стесняйся подсматривать в уже сделанные задачи, | ||
* чтобы оттуда что-то скопировать. | ||
|
@@ -55,5 +196,3 @@ import './style.css'; | |
* гражданство, национальность, номер телефона и адрес электронной почты. | ||
* Придумай, как избежать излишнего дублирования. | ||
*/ | ||
|
||
console.log('Hi from script!'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
body { | ||
font-family: "Segoe UI", Helvetica, sans-serif; | ||
} | ||
|
||
.name, .surname, .city { | ||
display: inline-block; | ||
width: 120px; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А это зачем?