@@ -20,7 +20,6 @@ import TextareaAutosize from 'react-textarea-autosize'
20
20
import { useToast } from './toast'
21
21
import { numWithUnits } from '@/lib/format'
22
22
import textAreaCaret from 'textarea-caret'
23
- import ReactDatePicker from 'react-datepicker'
24
23
import 'react-datepicker/dist/react-datepicker.css'
25
24
import useDebounceCallback , { debounce } from './use-debounce-callback'
26
25
import { FileUpload } from './file-upload'
@@ -38,9 +37,10 @@ import QrIcon from '@/svgs/qr-code-line.svg'
38
37
import QrScanIcon from '@/svgs/qr-scan-line.svg'
39
38
import { useShowModal } from './modal'
40
39
import { QRCodeSVG } from 'qrcode.react'
41
- import { Scanner } from '@yudiel/react-qr-scanner '
40
+ import dynamic from 'next/dynamic '
42
41
import { qrImageSettings } from './qr'
43
42
import { useIsClient } from './use-client'
43
+ import PageLoading from './page-loading'
44
44
45
45
export class SessionRequiredError extends Error {
46
46
constructor ( ) {
@@ -971,6 +971,19 @@ export function Select ({ label, items, info, groupClassName, onChange, noForm,
971
971
)
972
972
}
973
973
974
+ function DatePickerSkeleton ( ) {
975
+ return (
976
+ < div className = 'react-datepicker-wrapper' >
977
+ < input className = 'form-control clouds fade-out p-0 px-2 mb-0' />
978
+ </ div >
979
+ )
980
+ }
981
+
982
+ const ReactDatePicker = dynamic ( ( ) => import ( 'react-datepicker' ) . then ( mod => mod . default ) , {
983
+ ssr : false ,
984
+ loading : ( ) => < DatePickerSkeleton />
985
+ } )
986
+
974
987
export function DatePicker ( { fromName, toName, noForm, onChange, when, from, to, className, ...props } ) {
975
988
const formik = noForm ? null : useFormikContext ( )
976
989
const [ , , fromHelpers ] = noForm ? [ { } , { } , { } ] : useField ( { ...props , name : fromName } )
@@ -1038,19 +1051,23 @@ export function DatePicker ({ fromName, toName, noForm, onChange, when, from, to
1038
1051
}
1039
1052
1040
1053
return (
1041
- < ReactDatePicker
1042
- className = { `form-control text-center ${ className } ` }
1043
- selectsRange
1044
- maxDate = { new Date ( ) }
1045
- minDate = { new Date ( '2021-05-01' ) }
1046
- { ...props }
1047
- selected = { new Date ( innerFrom ) }
1048
- startDate = { new Date ( innerFrom ) }
1049
- endDate = { innerTo ? new Date ( innerTo ) : undefined }
1050
- dateFormat = { dateFormat }
1051
- onChangeRaw = { onChangeRawHandler }
1052
- onChange = { innerOnChange }
1053
- />
1054
+ < >
1055
+ { ReactDatePicker && (
1056
+ < ReactDatePicker
1057
+ className = { `form-control text-center ${ className } ` }
1058
+ selectsRange
1059
+ maxDate = { new Date ( ) }
1060
+ minDate = { new Date ( '2021-05-01' ) }
1061
+ { ...props }
1062
+ selected = { new Date ( innerFrom ) }
1063
+ startDate = { new Date ( innerFrom ) }
1064
+ endDate = { innerTo ? new Date ( innerTo ) : undefined }
1065
+ dateFormat = { dateFormat }
1066
+ onChangeRaw = { onChangeRawHandler }
1067
+ onChange = { innerOnChange }
1068
+ />
1069
+ ) }
1070
+ </ >
1054
1071
)
1055
1072
}
1056
1073
@@ -1070,19 +1087,27 @@ export function DateTimeInput ({ label, groupClassName, name, ...props }) {
1070
1087
1071
1088
function DateTimePicker ( { name, className, ...props } ) {
1072
1089
const [ field , , helpers ] = useField ( { ...props , name } )
1090
+ const ReactDatePicker = dynamic ( ( ) => import ( 'react-datepicker' ) . then ( mod => mod . default ) , {
1091
+ ssr : false ,
1092
+ loading : ( ) => < span > loading date picker</ span >
1093
+ } )
1073
1094
return (
1074
- < ReactDatePicker
1075
- { ...field }
1076
- { ...props }
1077
- showTimeSelect
1078
- dateFormat = 'Pp'
1079
- className = { `form-control ${ className } ` }
1080
- selected = { ( field . value && new Date ( field . value ) ) || null }
1081
- value = { ( field . value && new Date ( field . value ) ) || null }
1082
- onChange = { ( val ) => {
1083
- helpers . setValue ( val )
1084
- } }
1085
- />
1095
+ < >
1096
+ { ReactDatePicker && (
1097
+ < ReactDatePicker
1098
+ { ...field }
1099
+ { ...props }
1100
+ showTimeSelect
1101
+ dateFormat = 'Pp'
1102
+ className = { `form-control ${ className } ` }
1103
+ selected = { ( field . value && new Date ( field . value ) ) || null }
1104
+ value = { ( field . value && new Date ( field . value ) ) || null }
1105
+ onChange = { ( val ) => {
1106
+ helpers . setValue ( val )
1107
+ } }
1108
+ />
1109
+ ) }
1110
+ </ >
1086
1111
)
1087
1112
}
1088
1113
@@ -1149,6 +1174,10 @@ function QrPassword ({ value }) {
1149
1174
function PasswordScanner ( { onScan, text } ) {
1150
1175
const showModal = useShowModal ( )
1151
1176
const toaster = useToast ( )
1177
+ const Scanner = dynamic ( ( ) => import ( '@yudiel/react-qr-scanner' ) . then ( mod => mod . Scanner ) , {
1178
+ ssr : false ,
1179
+ loading : ( ) => < PageLoading />
1180
+ } )
1152
1181
1153
1182
return (
1154
1183
< InputGroup . Text
@@ -1158,26 +1187,28 @@ function PasswordScanner ({ onScan, text }) {
1158
1187
return (
1159
1188
< div >
1160
1189
{ text && < h5 className = 'line-height-md mb-4 text-center' > { text } </ h5 > }
1161
- < Scanner
1162
- formats = { [ 'qr_code' ] }
1163
- onScan = { ( [ { rawValue : result } ] ) => {
1164
- onScan ( result )
1165
- onClose ( )
1166
- } }
1167
- styles = { {
1168
- video : {
1169
- aspectRatio : '1 / 1'
1170
- }
1171
- } }
1172
- onError = { ( error ) => {
1173
- if ( error instanceof DOMException ) {
1174
- console . log ( error )
1175
- } else {
1176
- toaster . danger ( 'qr scan: ' + error ?. message || error ?. toString ?. ( ) )
1177
- }
1178
- onClose ( )
1179
- } }
1180
- />
1190
+ { Scanner && (
1191
+ < Scanner
1192
+ formats = { [ 'qr_code' ] }
1193
+ onScan = { ( [ { rawValue : result } ] ) => {
1194
+ onScan ( result )
1195
+ onClose ( )
1196
+ } }
1197
+ styles = { {
1198
+ video : {
1199
+ aspectRatio : '1 / 1'
1200
+ }
1201
+ } }
1202
+ onError = { ( error ) => {
1203
+ if ( error instanceof DOMException ) {
1204
+ console . log ( error )
1205
+ } else {
1206
+ toaster . danger ( 'qr scan: ' + error ?. message || error ?. toString ?. ( ) )
1207
+ }
1208
+ onClose ( )
1209
+ } }
1210
+ />
1211
+ ) }
1181
1212
</ div >
1182
1213
)
1183
1214
} )
0 commit comments