Skip to content

Commit 72a7b94

Browse files
committed
Add StatefullModalForm
1 parent cbc9244 commit 72a7b94

File tree

8 files changed

+90
-6
lines changed

8 files changed

+90
-6
lines changed

src/shared/button/Button.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import React, { ReactNode } from 'react';
1+
import React, { ReactNode, MouseEvent } from 'react';
22
import cn from 'clsx';
33
import s from './Button.modules.scss';
44

55
type ButtonProps = {
66
children: ReactNode;
77
disabled?: boolean;
8+
onClick?: (event: MouseEvent<HTMLButtonElement>) => void;
89
};
910

10-
export const Button = ({ children, disabled = false }: ButtonProps) => (
11-
<button className={cn(s.button, { [s.disabled]: disabled })}>{children}</button>
11+
export const Button = ({ children, onClick, disabled = false }: ButtonProps) => (
12+
<button className={cn(s.button, { [s.disabled]: disabled })} onClick={onClick}>
13+
{children}
14+
</button>
1215
);

src/shared/modal-form/ModalForm.module.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.modal {
22
display: flex;
3+
position: fixed;
34
align-items: center;
45
z-index: 1;
56
left: 0;

src/shared/modal-form/ModalForm.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
import React, { ReactNode } from 'react';
1+
import React, { ReactNode, MouseEvent } from 'react';
22
import cn from 'clsx';
33
import s from './ModalForm.module.scss';
44

55
type ModalFormProps = {
66
visible: boolean;
77
children: ReactNode;
8+
onClose?: (event: MouseEvent<HTMLButtonElement>) => void;
89
};
910

10-
export const ModalForm = ({ visible = true, children }: ModalFormProps) => {
11+
export const ModalForm = ({ visible = true, onClose, children }: ModalFormProps) => {
1112
return (
1213
<div id="myModal" className={cn(s.modal, { [s['modal-hide']]: !visible })}>
1314
<div className={s['modal-content']}>
14-
<span className={s.close}>&times;</span>
15+
<span className={s.close} onClick={onClose}>
16+
&times;
17+
</span>
1518
{children}
1619
</div>
1720
</div>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.statefull-modal {
2+
display: flex;
3+
flex-direction: column;
4+
gap: 10px;
5+
align-items: center;
6+
7+
input {
8+
align-self: stretch;
9+
}
10+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React, { useState, FormEvent } from 'react';
2+
import { Button } from '../button/Button';
3+
import { TextInput } from '../text-input/TextInput';
4+
import { ModalForm } from '../modal-form/ModalForm';
5+
import s from './StatefullModalForm.module.scss';
6+
7+
export const StatefullModalForm = () => {
8+
const [inputValue, setInputValue] = useState('');
9+
const [isModalFormVisible, setIsModalFormVisible] = useState(false);
10+
11+
const openModalForm = () => setIsModalFormVisible(true);
12+
const closeModalForm = () => setIsModalFormVisible(false);
13+
const saveInputValue = (event: FormEvent<HTMLInputElement>) => setInputValue(event.currentTarget.value);
14+
15+
return (
16+
<div className={s['statefull-modal']}>
17+
<TextInput value={inputValue} onInput={saveInputValue} />
18+
<Button onClick={openModalForm}>Открыть модальное окно</Button>
19+
<ModalForm visible={isModalFormVisible} onClose={closeModalForm}>
20+
<p>{inputValue}</p>
21+
</ModalForm>
22+
</div>
23+
);
24+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.text-input {
2+
padding: 8px;
3+
font-size: 1em;
4+
border: 2px solid #ccc;
5+
border-radius: 5px;
6+
-webkit-transition: 0.5s;
7+
transition: 0.5s;
8+
outline: none;
9+
&:focus {
10+
border: 2px solid #555;
11+
}
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React, { FormEvent } from 'react';
2+
import s from './TextInput.module.scss';
3+
4+
type TextInputProps = {
5+
value: string;
6+
onInput?: (event: FormEvent<HTMLInputElement>) => void;
7+
};
8+
9+
export const TextInput = ({ value, onInput }: TextInputProps) => (
10+
<input className={s['text-input']} type="text" value={value} onInput={onInput} />
11+
);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { Meta } from '@storybook/react';
2+
3+
import { StatefullModalForm } from '../shared/statefull-modal-form/StatefullModalForm';
4+
5+
const meta: Meta<typeof StatefullModalForm> = {
6+
component: StatefullModalForm,
7+
title: 'Общее задание/Состояние модального окна',
8+
tags: ['autodocs'],
9+
parameters: {
10+
docs: {
11+
story: {
12+
height: '500px',
13+
},
14+
},
15+
},
16+
};
17+
18+
export default meta;
19+
20+
export const Test = {};

0 commit comments

Comments
 (0)