|
| 1 | +import { ActionValue } from "mendix"; |
| 2 | +import { action, computed, makeObservable, observable } from "mobx"; |
| 3 | +import { ClassAttributes, createRef, KeyboardEvent, KeyboardEventHandler, MouseEvent, MouseEventHandler } from "react"; |
| 4 | +import ReactDatePicker, { ReactDatePickerProps } from "react-datepicker"; |
| 5 | + |
| 6 | +interface DatePickerBackendProps extends ReactDatePickerProps, ClassAttributes<ReactDatePicker> {} |
| 7 | + |
| 8 | +interface PickerState { |
| 9 | + startDate: Date | undefined; |
| 10 | + endDate: Date | undefined; |
| 11 | + expanded: boolean; |
| 12 | + selectsRange: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +type Params = { |
| 16 | + defaultStart?: Date; |
| 17 | + defaultEnd?: Date; |
| 18 | + onChange?: ActionValue<"none">; |
| 19 | + type: "date" | "time" | "datetime" | "range"; |
| 20 | +}; |
| 21 | + |
| 22 | +export class DatePickerController { |
| 23 | + private _dates: Array<Date | undefined>; |
| 24 | + private _timer = -1; |
| 25 | + private _defaultState: Array<Date | undefined>; |
| 26 | + private _type: "date" | "time" | "datetime" | "range"; |
| 27 | + expanded = false; |
| 28 | + pickerRef = createRef<ReactDatePicker<undefined, undefined>>(); |
| 29 | + |
| 30 | + constructor(params: Params) { |
| 31 | + this._dates = this.getDefaults(params); |
| 32 | + this._defaultState = this.getDefaults(params); |
| 33 | + this._type = params.type; |
| 34 | + |
| 35 | + // implement onChange action |
| 36 | + |
| 37 | + makeObservable(this, { |
| 38 | + pickerState: computed, |
| 39 | + expanded: observable, |
| 40 | + handlePickerChange: action, |
| 41 | + handleCalendarOpen: action, |
| 42 | + handleCalendarClose: action, |
| 43 | + handleKeyDown: action |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + get pickerState(): PickerState { |
| 48 | + const isRange = this._type === "range"; |
| 49 | + return { |
| 50 | + endDate: this._selectsRange ? this._dates[1] : undefined, |
| 51 | + expanded: this.expanded, |
| 52 | + selectsRange: isRange, |
| 53 | + startDate: this._dates[0] |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + private get _selectsRange(): boolean { |
| 58 | + return this._type === "range"; |
| 59 | + } |
| 60 | + |
| 61 | + handlePickerChange: DatePickerBackendProps["onChange"] = (value: Date | [Date | null, Date | null] | null) => { |
| 62 | + if (this._selectsRange) { |
| 63 | + const [start, end] = value as [Date | null, Date | null]; |
| 64 | + this._dates[0] = start ?? undefined; |
| 65 | + this._dates[1] = end ?? undefined; |
| 66 | + return; |
| 67 | + } else { |
| 68 | + this._dates[0] = value as Date; |
| 69 | + return; |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + handleCalendarOpen: DatePickerBackendProps["onCalendarOpen"] = () => { |
| 74 | + this.expanded = true; |
| 75 | + }; |
| 76 | + |
| 77 | + handleCalendarClose: DatePickerBackendProps["onCalendarOpen"] = () => { |
| 78 | + this.expanded = false; |
| 79 | + }; |
| 80 | + |
| 81 | + /** We use mouse down to avoid race condition with calendar "outside click" event. */ |
| 82 | + handleButtonMouseDown: MouseEventHandler = () => { |
| 83 | + if (this.expanded === false) { |
| 84 | + this._setActive(); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + handleButtonKeyDown: KeyboardEventHandler = e => { |
| 89 | + if (e.code === "Enter" || e.code === "Space") { |
| 90 | + e.preventDefault(); |
| 91 | + e.stopPropagation(); |
| 92 | + this._setActive(); |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + handleKeyDown: KeyboardEventHandler = event => { |
| 97 | + // Clear value on "Backspace" in range mode. Works only when focused on input. |
| 98 | + if ( |
| 99 | + this._selectsRange && |
| 100 | + (event.target as HTMLInputElement).nodeName === "INPUT" && |
| 101 | + event.code === "Backspace" |
| 102 | + ) { |
| 103 | + this._dates = [undefined, undefined]; |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + /** |
| 108 | + * Prevent any input changes in range selection mode. |
| 109 | + * @remark |
| 110 | + * Don't change this method unless there no other way to solve your problem. |
| 111 | + * This method is just UX tweak that should prevent user confusion and have very low |
| 112 | + * value in widget behavior. Feel free to remove this method if you refactoring code. |
| 113 | + */ |
| 114 | + UNSAFE_handleChangeRaw = (event?: MouseEvent<HTMLElement> | KeyboardEvent<HTMLElement> | undefined): void => { |
| 115 | + if (event?.type === "change" && this._selectsRange) { |
| 116 | + event.preventDefault(); |
| 117 | + } |
| 118 | + }; |
| 119 | + |
| 120 | + private _setActive(): void { |
| 121 | + const picker = this.pickerRef.current; |
| 122 | + clearTimeout(this._timer); |
| 123 | + // Run setFocus on next tick to prevent calling focus on disabled element. |
| 124 | + this._timer = window.setTimeout(() => { |
| 125 | + picker?.setFocus(); |
| 126 | + this._timer = -1; |
| 127 | + }) as number; |
| 128 | + } |
| 129 | + |
| 130 | + setup(): (() => void) | void { |
| 131 | + this._dates = this._defaultState; |
| 132 | + } |
| 133 | + |
| 134 | + private getDefaults(params: Params): Array<Date | undefined> { |
| 135 | + return params.type === "range" ? [params.defaultStart, params.defaultEnd] : [params.defaultStart, undefined]; |
| 136 | + } |
| 137 | +} |
0 commit comments