forked from samsam-ahmadi/react-trip-date
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: can change year when you want to change the month
- Loading branch information
Samsam Ahmadi
committed
May 7, 2022
1 parent
6d20869
commit ee53c38
Showing
6 changed files
with
127 additions
and
14 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
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,58 @@ | ||
import styled from "styled-components"; | ||
import dayjs, { Dayjs } from "dayjs"; | ||
import { Dispatch, SetStateAction } from "react"; | ||
import { useOnClickOutside } from "libs/useClickOutside"; | ||
|
||
type Props = { | ||
setSource: Dispatch<SetStateAction<Dayjs>>; | ||
setDisplayYears: Dispatch<SetStateAction<boolean>>; | ||
}; | ||
|
||
export const DisplayYears = ({ setDisplayYears, setSource }: Props) => { | ||
const years = []; | ||
const { ref } = useOnClickOutside(() => setDisplayYears(prev => !prev)); | ||
for (let i = 0; i < 120; i++) { | ||
years.push( | ||
<p | ||
key={i} | ||
onClick={() => { | ||
setSource(pre => | ||
pre.set("year", Number(dayjs().subtract(i, "year").format("YYYY"))), | ||
); | ||
setDisplayYears(prev => !prev); | ||
}} | ||
> | ||
{dayjs().subtract(i, "year").format("YYYY")} | ||
</p>, | ||
); | ||
} | ||
|
||
return <Wrapper ref={ref}> {years} </Wrapper>; | ||
}; | ||
|
||
const Wrapper = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
position: absolute; | ||
top: 20px; | ||
left: 0; | ||
right: 0; | ||
margin: auto; | ||
width: 80px; | ||
height: 40vh; | ||
background: #fff; | ||
overflow: auto; | ||
padding: 10px 0px; | ||
border-radius: ${({ theme }) => theme.shape.borderRadius}px; | ||
> p { | ||
display: block; | ||
margin: 0; | ||
padding: 6px 0; | ||
text-align: center; | ||
width: 100% !important; | ||
color: ${({ theme }) => theme.grey[900]} !important; | ||
:hover { | ||
background-color: ${({ theme }) => theme.primary.light}; | ||
} | ||
} | ||
`; |
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,28 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
type Event = MouseEvent | TouchEvent; | ||
|
||
export const useOnClickOutside = (handler: (event: Event) => void) => { | ||
const ref = useRef<any>(); | ||
|
||
useEffect(() => { | ||
const listener = (event: Event) => { | ||
const el = ref?.current; | ||
if (!el || el.contains((event?.target as Node) || null)) { | ||
return; | ||
} | ||
|
||
handler(event); | ||
}; | ||
|
||
document.addEventListener("mousedown", listener); | ||
document.addEventListener("touchstart", listener); | ||
|
||
return () => { | ||
document.removeEventListener("mousedown", listener); | ||
document.removeEventListener("touchstart", listener); | ||
}; | ||
}, [ref, handler]); | ||
|
||
return { ref }; | ||
}; |