-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from ktvtk/module6-task2
- Loading branch information
Showing
4 changed files
with
81 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import {JSX, useState} from 'react'; | ||
import {sortOptions} from '../../const.ts'; | ||
import {SortOption} from '../../types/sort-option.ts'; | ||
|
||
type SortingProps = { | ||
onSortChange: (option: SortOption) => void; | ||
}; | ||
|
||
export function Sorting({onSortChange} : SortingProps) : JSX.Element { | ||
const [activeSortOption, setActiveSortOption] = useState<SortOption>('Popular'); | ||
const [isOptionsVisible, setIsOptionsVisible] = useState<boolean>(false); | ||
|
||
const handleOptionClick = (option: SortOption) => { | ||
setActiveSortOption(option); | ||
onSortChange(option); | ||
setIsOptionsVisible(false); | ||
}; | ||
|
||
const handleListOptionClick = () => { | ||
setIsOptionsVisible(!isOptionsVisible); | ||
}; | ||
|
||
return ( | ||
<form className="places__sorting" action="#" method="get"> | ||
<span className="places__sorting-caption">Sort by</span> | ||
<span className="places__sorting-type" tabIndex={0} onClick={handleListOptionClick}> | ||
{activeSortOption} | ||
<svg className="places__sorting-arrow" width="7" height="4"> | ||
<use xlinkHref="#icon-arrow-select"></use> | ||
</svg> | ||
</span> | ||
<ul className={`places__options places__options--custom ${isOptionsVisible ? 'places__options--opened' : ''}`}> | ||
{sortOptions.map((option) => ( | ||
<li | ||
key={option} | ||
className={`places__option ${option === activeSortOption ? 'places__option--active' : ''}`} | ||
tabIndex={0} | ||
onClick={() => handleOptionClick(option)} | ||
> | ||
{option} | ||
</li> | ||
))} | ||
</ul> | ||
</form> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type SortOption = 'Popular' | ||
| 'Price: low to high' | ||
| 'Price: high to low' | ||
| 'Top rated first'; |