-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daria Dmitrienko
committed
Oct 10, 2024
1 parent
11deff0
commit d34adac
Showing
9 changed files
with
171 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 121 additions & 6 deletions
127
src/components/article-params-form/ArticleParamsForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,135 @@ | ||
import { ArrowButton } from 'components/arrow-button'; | ||
import { Button } from 'components/button'; | ||
import { useRef, useState } from 'react'; | ||
|
||
import styles from './ArticleParamsForm.module.scss'; | ||
import { Select } from '../select'; | ||
import { RadioGroup } from '../radio-group' | ||
import { fontFamilyOptions, fontColors, backgroundColors, contentWidthArr, fontSizeOptions, defaultArticleState, ArticleStateType } from 'src/constants/articleProps'; | ||
import { useOutsideClickClose } from '../select/hooks/useOutsideClickClose'; | ||
import { Separator } from '../separator'; | ||
import { Spacer } from '../spacer'; | ||
|
||
|
||
interface propsArticleParamsForm { | ||
onChange?: (data: ArticleStateType) => void; | ||
} | ||
|
||
export const ArticleParamsForm = ({ onChange }: propsArticleParamsForm) => { | ||
const [sidebarIsOpen, setIsSidebarOpen] = useState(false); | ||
const [selectedFont, setSelectedFont] = useState(defaultArticleState.fontFamilyOption); | ||
const [selectedFontSize, setSelectedFontSize] = useState(defaultArticleState.fontSizeOption); | ||
const [selectedFontColor, setSelectedFontColor] = useState(defaultArticleState.fontColor); | ||
const [selectedBackgroundColor, setSelectedBackgroundColor] = useState(defaultArticleState.backgroundColor); | ||
const [selectedWidthContent, setSelectedWidthContent] = useState(defaultArticleState.contentWidth); | ||
|
||
const rootRef = useRef<HTMLDivElement>(null); | ||
let lastStyles = useRef(defaultArticleState); | ||
|
||
useOutsideClickClose({ | ||
isOpen: sidebarIsOpen, | ||
rootRef: rootRef, | ||
onClose: () => { | ||
setSelectedFont(lastStyles.current.fontFamilyOption) | ||
setSelectedFontSize(lastStyles.current.fontSizeOption) | ||
setSelectedFontColor(lastStyles.current.fontColor) | ||
setSelectedBackgroundColor(lastStyles.current.backgroundColor) | ||
setSelectedWidthContent(lastStyles.current.contentWidth) | ||
}, | ||
onChange: setIsSidebarOpen, | ||
}); | ||
|
||
const toggleSidebar = () => { | ||
// Вызов функции для изменения состояния | ||
setIsSidebarOpen(!sidebarIsOpen); | ||
} | ||
|
||
const submitHandler = (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
|
||
lastStyles.current = { | ||
fontFamilyOption: selectedFont, | ||
fontColor: selectedFontColor, | ||
backgroundColor: selectedBackgroundColor, | ||
contentWidth: selectedWidthContent, | ||
fontSizeOption: selectedFontSize, | ||
}; | ||
|
||
if (onChange) { | ||
onChange(lastStyles.current) | ||
} | ||
} | ||
|
||
const resetHandler = (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
setSelectedFontColor(defaultArticleState.fontColor) | ||
setSelectedFont(defaultArticleState.fontFamilyOption) | ||
setSelectedFontSize(defaultArticleState.fontSizeOption) | ||
setSelectedBackgroundColor(defaultArticleState.backgroundColor) | ||
setSelectedWidthContent(defaultArticleState.contentWidth) | ||
if (onChange) { | ||
onChange(defaultArticleState) | ||
} | ||
} | ||
|
||
export const ArticleParamsForm = () => { | ||
return ( | ||
<> | ||
<ArrowButton /> | ||
<aside className={styles.container}> | ||
<form className={styles.form}> | ||
<div ref={rootRef}> | ||
<ArrowButton onClick={toggleSidebar} /> | ||
<aside className={`${styles.container} ${sidebarIsOpen ? styles.container_open : ''}`}> | ||
|
||
<form className={styles.form} onSubmit={submitHandler} onReset={resetHandler} > | ||
|
||
<Select | ||
selected={selectedFont} | ||
onChange={setSelectedFont} | ||
options={fontFamilyOptions} | ||
title='шрифт' | ||
/> | ||
|
||
<Spacer /> | ||
|
||
<RadioGroup | ||
selected={selectedFontSize} | ||
name='radio' | ||
onChange={setSelectedFontSize} | ||
options={fontSizeOptions} | ||
title='размер шрифта' | ||
/> | ||
|
||
<Spacer /> | ||
|
||
<Select | ||
selected={selectedFontColor} | ||
onChange={setSelectedFontColor} | ||
options={fontColors} | ||
title='цвет шрифта' | ||
/> | ||
<Spacer /> | ||
|
||
<Separator /> | ||
<Spacer /> | ||
|
||
<Select | ||
selected={selectedBackgroundColor} | ||
onChange={setSelectedBackgroundColor} | ||
options={backgroundColors} | ||
title='цвет фона' | ||
/> | ||
<Spacer /> | ||
|
||
<Select | ||
selected={selectedWidthContent} | ||
onChange={setSelectedWidthContent} | ||
options={contentWidthArr} | ||
title='ширина контента' | ||
/> | ||
|
||
<div className={styles.bottomContainer}> | ||
<Button title='Сбросить' type='reset' /> | ||
<Button title='Применить' type='submit' /> | ||
</div> | ||
</form> | ||
</aside> | ||
</> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
.separator { | ||
width: 100%; | ||
height: 1px; | ||
background: #000000; | ||
background: #D7D7D7; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import { Spacer } from './Spacer'; | ||
|
||
const meta: Meta<typeof Spacer> = { | ||
component: Spacer, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof Spacer>; | ||
|
||
export const SelectStory: Story = { | ||
render: () => { | ||
return <Spacer />; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import styles from './index.module.scss'; | ||
|
||
export const Spacer = () => { | ||
return <div className={styles.spacer}></div>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.spacer { | ||
width: 100%; | ||
height: 50px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Spacer } from './Spacer'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters