Move date-fns in async lazy component

pull/2824/head
Johan Girod 2023-09-22 14:00:46 +02:00
parent 7643e7954b
commit 09ccdbb4c7
2 changed files with 38 additions and 21 deletions

View File

@ -1,13 +1,16 @@
import { useCallback } from 'react'
import { lazy, Suspense, useCallback } from 'react'
import styled from 'styled-components'
import { InputProps } from '@/components/conversation/RuleInput'
import { DateField } from '@/design-system/field'
import { DateFieldProps } from '@/design-system/field/DateField'
import { Spacing } from '@/design-system/layout'
import Skeleton from '../ui/Skeleton'
import { useEngine } from '../utils/EngineContext'
import InputSuggestions from './InputSuggestions'
const DateField = lazy(() => import('@/design-system/field/DateField'))
export default function DateInput({
suggestions,
onChange,
@ -62,20 +65,31 @@ export default function DateInput({
}}
/>
)}
<DateField
defaultSelected={
(missing && hideDefaultValue) || !dateValue
? undefined
: new Date(dateValue)
}
isRequired={required}
onChange={handleDateChange}
aria-label={title}
label={title}
type={type}
/>
<Suspense fallback={<DateFieldFallback />}>
<DateField
defaultSelected={
(missing && hideDefaultValue) || !dateValue
? undefined
: new Date(dateValue)
}
isRequired={required}
onChange={handleDateChange}
aria-label={title}
label={title}
type={type}
/>
</Suspense>
<Spacing md />
</div>
</div>
)
}
function DateFieldFallback() {
return <Wrapper />
}
const Wrapper = styled(Skeleton)`
width: 218px;
height: 3.5rem;
`

View File

@ -1,8 +1,11 @@
import { CSSProperties } from 'react'
import { keyframes, styled } from 'styled-components'
type SkeletonProps = {
width?: number
height?: number
className?: string
style?: CSSProperties
}
const skeletonKeyframes = keyframes`
@ -14,14 +17,14 @@ const skeletonKeyframes = keyframes`
}
` as unknown as string // keyframes type are outdated, see https://github.com/DefinitelyTyped/DefinitelyTyped/issues/48907
export default function Skeleton({ width, height }: SkeletonProps) {
export default function Skeleton({
style,
className,
height,
width,
}: SkeletonProps) {
return (
<StyledSpan
style={{
width,
height,
}}
>
<StyledSpan className={className} style={{ ...style, height, width }}>
&zwnj;
</StyledSpan>
)