forked from twentyhq/twenty
-
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.
New Datetime field picker (twentyhq#4907)
### Description New Datetime field picker ### Refs twentyhq#4376 ### Demo https://github.com/twentyhq/twenty/assets/140154534/32656323-972c-413a-9986-a78efffae1b4 Fixes twentyhq#4376 --------- Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com> Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: Matheus <matheus_benini@hotmail.com> Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com> Co-authored-by: Charles Bochet <charles@twenty.com>
- Loading branch information
1 parent
464a2d5
commit efcb5dc
Showing
15 changed files
with
469 additions
and
99 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
5 changes: 3 additions & 2 deletions
5
packages/twenty-docs/src/ui/navigation/menu-item/menuItemToggleCode.js
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
109 changes: 109 additions & 0 deletions
109
...s/twenty-front/src/modules/ui/input/components/internal/date/components/DateTimeInput.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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useIMask } from 'react-imask'; | ||
import styled from '@emotion/styled'; | ||
import { DateTime } from 'luxon'; | ||
|
||
import { DATE_TIME_BLOCKS } from '@/ui/input/components/internal/date/constants/DateTimeBlocks'; | ||
import { DATE_TIME_MASK } from '@/ui/input/components/internal/date/constants/DateTimeMask'; | ||
|
||
const StyledInputContainer = styled.div` | ||
width: 100%; | ||
display: flex; | ||
border-bottom: 1px solid ${({ theme }) => theme.border.color.light}; | ||
height: ${({ theme }) => theme.spacing(8)}; | ||
`; | ||
|
||
const StyledInput = styled.input<{ hasError?: boolean }>` | ||
background: ${({ theme }) => theme.background.secondary}; | ||
border: none; | ||
color: ${({ theme }) => theme.font.color.primary}; | ||
outline: none; | ||
padding: 8px; | ||
font-weight: 500; | ||
font-size: ${({ theme }) => theme.font.size.md}; | ||
width: 100%; | ||
color: ${({ hasError, theme }) => (hasError ? theme.color.red : 'inherit')}; | ||
`; | ||
|
||
type DateTimeInputProps = { | ||
onChange?: (date: Date | null) => void; | ||
date: Date | null; | ||
isDateTimeInput?: boolean; | ||
onError?: (error: Error) => void; | ||
}; | ||
|
||
export const DateTimeInput = ({ | ||
date, | ||
onChange, | ||
isDateTimeInput, | ||
}: DateTimeInputProps) => { | ||
const [hasError, setHasError] = useState(false); | ||
|
||
const parseDateToString = (date: any) => { | ||
const dateParsed = DateTime.fromJSDate(date); | ||
|
||
const formattedDate = dateParsed.toFormat('MM/dd/yyyy HH:mm'); | ||
|
||
return formattedDate; | ||
}; | ||
|
||
const parseStringToDate = (str: string) => { | ||
setHasError(false); | ||
|
||
const parsedDate = DateTime.fromFormat(str, 'MM/dd/yyyy HH:mm'); | ||
|
||
const isValid = parsedDate.isValid; | ||
|
||
if (!isValid) { | ||
setHasError(true); | ||
|
||
return null; | ||
} | ||
|
||
const jsDate = parsedDate.toJSDate(); | ||
|
||
return jsDate; | ||
}; | ||
|
||
const { ref, setValue, value } = useIMask( | ||
{ | ||
mask: Date, | ||
pattern: DATE_TIME_MASK, | ||
blocks: DATE_TIME_BLOCKS, | ||
min: new Date(1970, 0, 1), | ||
max: new Date(2100, 0, 1), | ||
format: parseDateToString, | ||
parse: parseStringToDate, | ||
lazy: false, | ||
autofix: true, | ||
}, | ||
{ | ||
onComplete: (value) => { | ||
const parsedDate = parseStringToDate(value); | ||
|
||
onChange?.(parsedDate); | ||
}, | ||
onAccept: () => { | ||
setHasError(false); | ||
}, | ||
}, | ||
); | ||
|
||
useEffect(() => { | ||
setValue(parseDateToString(date)); | ||
}, [date, setValue]); | ||
|
||
return ( | ||
<StyledInputContainer> | ||
<StyledInput | ||
type="text" | ||
ref={ref as any} | ||
placeholder={`Type date${ | ||
isDateTimeInput ? ' and time' : ' (mm/dd/yyyy)' | ||
}`} | ||
value={value} | ||
hasError={hasError} | ||
/> | ||
</StyledInputContainer> | ||
); | ||
}; |
Oops, something went wrong.