-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
5 changed files
with
249 additions
and
5 deletions.
There are no files selected for viewing
139 changes: 139 additions & 0 deletions
139
src/components/commons/inputs/PostFormDropdown.module.scss
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,139 @@ | ||
.dropdown { | ||
@include column-flexbox(start, start, 0.8rem); | ||
|
||
.label { | ||
@include text-style(14, $white); | ||
} | ||
|
||
.select-group { | ||
position: relative; | ||
width: 100%; | ||
|
||
&-select { | ||
display: none; | ||
} | ||
|
||
&-input-group { | ||
position: relative; | ||
|
||
.input { | ||
@include text-style(16, $gray10); | ||
|
||
cursor: pointer; | ||
|
||
width: 100%; | ||
height: 4.8rem; | ||
padding: 0 5.2rem 0 1.6rem; | ||
|
||
background: $input-background; | ||
border: 0.1rem solid $input-stroke; | ||
border-radius: 0.8rem; | ||
|
||
transition: $base-transition; | ||
|
||
&::placeholder { | ||
@include text-style(16, $gray50); | ||
} | ||
|
||
&.sm { | ||
@include text-style-14; | ||
|
||
height: 4rem; | ||
padding-right: 4.4rem; | ||
} | ||
|
||
&.opened { | ||
border-color: $purple; | ||
} | ||
|
||
&:disabled { | ||
cursor: not-allowed; | ||
background: $gray60; | ||
} | ||
} | ||
|
||
.arrow-btn { | ||
@include pos-center-y; | ||
|
||
right: 1.2rem; | ||
transition: $transition-6s; | ||
|
||
&:disabled { | ||
cursor: not-allowed; | ||
} | ||
|
||
&.rotate { | ||
@include pos-center-y; | ||
|
||
transform: translateY(-62%) rotate(180deg); | ||
transition: $transition-6s; | ||
} | ||
} | ||
} | ||
|
||
&-container { | ||
cursor: pointer; | ||
|
||
position: absolute; | ||
z-index: $dropdown-level; | ||
top: 5.6rem; | ||
|
||
display: none; | ||
|
||
width: 100%; | ||
max-height: 24.8rem; | ||
padding: 0.8rem 0.4rem 0.8rem 0.8rem; | ||
|
||
background: $input-background; | ||
border: 0.1rem solid $input-stroke; | ||
border-radius: 0.6rem; | ||
box-shadow: $dropdown-shadow; | ||
|
||
&.sm { | ||
top: 4.8rem; | ||
} | ||
|
||
&.show { | ||
display: block; | ||
} | ||
|
||
&-list { | ||
@include column-flexbox(start, start, 0.4rem); | ||
@include thin-scrollbar; | ||
|
||
overflow-y: auto; | ||
width: 100%; | ||
max-height: 23rem; | ||
padding-right: 0.4rem; | ||
|
||
.item { | ||
width: 100%; | ||
|
||
.btn { | ||
@include text-style(16, $gray50); | ||
|
||
@include responsive(T) { | ||
color: $gray50; | ||
background: inherit; | ||
border-radius: inherit; | ||
} | ||
|
||
width: 100%; | ||
padding: 0.8rem; | ||
text-align: left; | ||
|
||
&:hover { | ||
color: $gray10; | ||
background: $black50; | ||
border-radius: 0.4rem; | ||
} | ||
|
||
&.sm { | ||
@include text-style(14); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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,104 @@ | ||
import Image from 'next/image'; | ||
|
||
import { useState } from 'react'; | ||
|
||
import classNames from 'classnames/bind'; | ||
import { useFormContext } from 'react-hook-form'; | ||
|
||
import { SVGS } from '@/constants'; | ||
|
||
import useTogglePopup from '@/hooks/useTogglePopup'; | ||
|
||
import styles from './PostFormDropdown.module.scss'; | ||
|
||
const cx = classNames.bind(styles); | ||
|
||
const { url: defaultUrl, alt: defaultAlt } = SVGS.arrow.down.default; | ||
const { url: activeUrl, alt: activeAlt } = SVGS.arrow.down.active; | ||
|
||
type PostFormDropdownProps = { | ||
name: string; | ||
options: { title: string; value: number | string }[]; | ||
label?: string; | ||
isSmall?: boolean; | ||
isDisabled?: boolean; | ||
}; | ||
|
||
export const PostFormDropdown = ({ | ||
name, | ||
label = '', | ||
options, | ||
isSmall = false, | ||
isDisabled = false, | ||
}: PostFormDropdownProps) => { | ||
const { register, setValue } = useFormContext(); | ||
const { isOpen, popupRef, buttonRef, togglePopup } = useTogglePopup(); | ||
const [currentOptionTitle, setCurrentOptionTitle] = useState(options[0].title); | ||
|
||
const handleOptionChange = (title: string, value: number | string) => { | ||
setValue(name, value); | ||
setCurrentOptionTitle(title); | ||
togglePopup(); | ||
}; | ||
|
||
return ( | ||
<div className={cx('dropdown')} ref={popupRef}> | ||
<span className={cx('label')}>{label}</span> | ||
<div className={cx('select-group')}> | ||
<select | ||
className={cx('select-group-select')} | ||
{...register(name, { | ||
value: options[0].value, | ||
})} | ||
> | ||
{options.map((option, index) => ( | ||
<option key={`dropdown-${index}`} value={option.value}> | ||
{option.title} | ||
</option> | ||
))} | ||
</select> | ||
|
||
<div className={cx('select-group-input-group')}> | ||
<input | ||
className={cx('input', { sm: isSmall }, { opened: isOpen })} | ||
type='text' | ||
value={currentOptionTitle} | ||
disabled={isDisabled} | ||
onClick={togglePopup} | ||
readOnly | ||
/> | ||
<button | ||
className={cx('arrow-btn', { rotate: isOpen })} | ||
ref={buttonRef} | ||
type='button' | ||
disabled={isDisabled} | ||
onClick={togglePopup} | ||
> | ||
{isOpen ? ( | ||
<Image src={activeUrl} alt={activeAlt} width={24} height={24} /> | ||
) : ( | ||
<Image src={defaultUrl} alt={defaultAlt} width={24} height={24} /> | ||
)} | ||
</button> | ||
</div> | ||
|
||
<div className={cx('select-group-container', { sm: isSmall }, { show: isOpen })}> | ||
{isOpen && ( | ||
<ul className={cx('select-group-container-list')}> | ||
{options.map((option, index) => ( | ||
<li className={cx('item')} key={`dropdown-item-${index}`}> | ||
<button | ||
className={cx('btn', { sm: isSmall })} | ||
onClick={() => handleOptionChange(option.title, option.value)} | ||
> | ||
<label>{option.title}</label> | ||
</button> | ||
</li> | ||
))} | ||
</ul> | ||
)} | ||
</div> | ||
</div> | ||
</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
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