|
| 1 | +import React, { MouseEventHandler, useCallback, useMemo } from 'react' |
| 2 | +import { Label, useField } from 'payload/components/forms' |
| 3 | +import Error from 'payload/dist/admin/components/forms/Error' |
| 4 | +import type { DateField, Option, OptionObject, SelectField } from 'payload/types' |
| 5 | +import type { Timezone } from '.' |
| 6 | +import DatePicker from 'payload/dist/admin/components/elements/DatePicker' |
| 7 | +import ReactSelect from 'payload/dist/admin/components/elements/ReactSelect' |
| 8 | +import { utcToZonedTime, zonedTimeToUtc } from 'date-fns-tz' |
| 9 | +import FieldDescription from 'payload/dist/admin/components/forms/FieldDescription' |
| 10 | +import '../../styles/date.scss' |
| 11 | + |
| 12 | +type Props = DateField & { |
| 13 | + path: string |
| 14 | + readOnly?: boolean |
| 15 | + placeholder?: string |
| 16 | + className?: string |
| 17 | + custom: { |
| 18 | + timezone?: Timezone |
| 19 | + timezoneField: SelectField |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +const DateComponent: React.FC<Props> = ({ |
| 24 | + readOnly, |
| 25 | + className, |
| 26 | + required, |
| 27 | + path, |
| 28 | + label, |
| 29 | + admin, |
| 30 | + custom, |
| 31 | + type, |
| 32 | + ...others |
| 33 | +}) => { |
| 34 | + const { timezone, timezoneField: timezoneFieldProps } = custom |
| 35 | + const { value, setValue, showError, errorMessage } = useField<Props>({ path }) |
| 36 | + const placeholder = admin?.placeholder ?? '' |
| 37 | + |
| 38 | + const beforeInput = admin?.components?.beforeInput |
| 39 | + const afterInput = admin?.components?.afterInput |
| 40 | + |
| 41 | + // @todo: figure it out later |
| 42 | + //const saveWithTimezone = timezone?.saveWithTimezone |
| 43 | + const datePickerProps = admin?.date |
| 44 | + |
| 45 | + const timezonePath = path.includes('.') |
| 46 | + ? path.slice(0, path.lastIndexOf('.')) + '.' + timezoneFieldProps.name |
| 47 | + : timezoneFieldProps.name |
| 48 | + |
| 49 | + const timezoneField = useField<Props>({ path: timezonePath }) |
| 50 | + |
| 51 | + const timezoneValue = timezoneField.value |
| 52 | + |
| 53 | + const renderedValue = useMemo(() => { |
| 54 | + // @ts-expect-error |
| 55 | + const modifiedValue = utcToZonedTime(value, timezoneValue) |
| 56 | + |
| 57 | + return modifiedValue |
| 58 | + }, [value, timezoneValue]) |
| 59 | + |
| 60 | + const visibleTimezone = useMemo(() => { |
| 61 | + const options = timezoneFieldProps.options as OptionObject[] |
| 62 | + |
| 63 | + const item = options.find(value => { |
| 64 | + // @ts-expect-error |
| 65 | + return value.value === timezoneValue |
| 66 | + }) |
| 67 | + |
| 68 | + return item |
| 69 | + }, [timezoneValue, timezoneFieldProps.options]) |
| 70 | + |
| 71 | + let dateFormat = datePickerProps?.displayFormat |
| 72 | + |
| 73 | + if (!datePickerProps?.displayFormat) { |
| 74 | + // when no displayFormat is provided, determine format based on the picker appearance |
| 75 | + if (datePickerProps?.pickerAppearance === 'default') dateFormat = 'MM/dd/yyyy' |
| 76 | + else if (datePickerProps?.pickerAppearance === 'dayAndTime') dateFormat = 'MMM d, yyy h:mm a' |
| 77 | + else if (datePickerProps?.pickerAppearance === 'timeOnly') dateFormat = 'h:mm a' |
| 78 | + else if (datePickerProps?.pickerAppearance === 'dayOnly') dateFormat = 'MMM dd' |
| 79 | + else if (datePickerProps?.pickerAppearance === 'monthOnly') dateFormat = 'MMMM' |
| 80 | + } |
| 81 | + |
| 82 | + const onChange = useCallback( |
| 83 | + (incomingDate: Date) => { |
| 84 | + if (!readOnly) { |
| 85 | + // @ts-expect-error |
| 86 | + const zonedTime = zonedTimeToUtc(incomingDate, timezoneValue) |
| 87 | + |
| 88 | + setValue(zonedTime?.toISOString() || null) |
| 89 | + } |
| 90 | + }, |
| 91 | + [timezoneValue], |
| 92 | + ) |
| 93 | + |
| 94 | + const classes = [ |
| 95 | + '', |
| 96 | + 'text', |
| 97 | + className, |
| 98 | + showError && 'error', |
| 99 | + readOnly && 'read-only', |
| 100 | + 'container', |
| 101 | + ] |
| 102 | + .filter(Boolean) |
| 103 | + .join(' ') |
| 104 | + |
| 105 | + const isRequired = required |
| 106 | + const isReadonly = readOnly || admin?.readOnly |
| 107 | + |
| 108 | + return ( |
| 109 | + <div className={`bfDateFieldWrapper field-type`}> |
| 110 | + <Error showError={showError} message={errorMessage ?? ''} /> |
| 111 | + <div> |
| 112 | + <div className={classes}> |
| 113 | + {Array.isArray(beforeInput) && beforeInput.map((Component, i) => <Component key={i} />)} |
| 114 | + <div className="fieldsWrapper"> |
| 115 | + <div className="dateField"> |
| 116 | + <Label |
| 117 | + htmlFor={`field-${path.replace(/\./gi, '__')}`} |
| 118 | + label={label} |
| 119 | + required={isRequired} |
| 120 | + /> |
| 121 | + |
| 122 | + <DatePicker |
| 123 | + {...datePickerProps} |
| 124 | + onChange={onChange} |
| 125 | + readOnly={isReadonly} |
| 126 | + value={renderedValue} |
| 127 | + overrides={{ id: `field-${path.replace(/\./g, '__')}`, locale: undefined }} |
| 128 | + /> |
| 129 | + </div> |
| 130 | + <div className="timezoneField"> |
| 131 | + <div> |
| 132 | + <Label |
| 133 | + htmlFor={`field-${timezonePath.replaceAll('.', '-')}`} |
| 134 | + label={timezoneFieldProps?.label ?? ''} |
| 135 | + /> |
| 136 | + </div> |
| 137 | + <ReactSelect |
| 138 | + inputId={`field-${timezonePath.replaceAll('.', '-')}`} |
| 139 | + value={visibleTimezone} |
| 140 | + isMulti={false} |
| 141 | + onChange={selected => { |
| 142 | + console.log('select', selected.value) |
| 143 | + timezoneField.setValue(selected.value) |
| 144 | + }} |
| 145 | + disabled={isReadonly} |
| 146 | + // @ts-expect-error |
| 147 | + options={timezoneFieldProps.options} |
| 148 | + /> |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + {Array.isArray(afterInput) && afterInput.map((Component, i) => <Component key={i} />)} |
| 152 | + </div> |
| 153 | + </div> |
| 154 | + <FieldDescription |
| 155 | + className={`field-description-${path.replace(/\./g, '__')}`} |
| 156 | + description={admin?.description} |
| 157 | + value={value} |
| 158 | + /> |
| 159 | + </div> |
| 160 | + ) |
| 161 | +} |
| 162 | + |
| 163 | +export default DateComponent |
0 commit comments