| 128 | } |
| 129 | |
| 130 | export default class DatePrompt extends Prompt<Date> { |
| 131 | #segments: SegmentConfig[]; |
| 132 | #separator: string; |
| 133 | #segmentValues: DateParts; |
| 134 | #minDate: Date | undefined; |
| 135 | #maxDate: Date | undefined; |
| 136 | #cursor = { segmentIndex: 0, positionInSegment: 0 }; |
| 137 | #segmentSelected = true; |
| 138 | #pendingTensDigit: string | null = null; |
| 139 | |
| 140 | inlineError = ''; |
| 141 | |
| 142 | get segmentCursor() { |
| 143 | return { ...this.#cursor }; |
| 144 | } |
| 145 | |
| 146 | get segmentValues(): DateParts { |
| 147 | return { ...this.#segmentValues }; |
| 148 | } |
| 149 | |
| 150 | get segments(): readonly SegmentConfig[] { |
| 151 | return this.#segments; |
| 152 | } |
| 153 | |
| 154 | get separator(): string { |
| 155 | return this.#separator; |
| 156 | } |
| 157 | |
| 158 | get formattedValue(): string { |
| 159 | return this.#format(this.#segmentValues); |
| 160 | } |
| 161 | |
| 162 | #format(parts: DateParts): string { |
| 163 | return this.#segments.map((s) => parts[s.type]).join(this.#separator); |
| 164 | } |
| 165 | |
| 166 | #refresh() { |
| 167 | this._setUserInput(this.#format(this.#segmentValues)); |
| 168 | this._setValue(toDate(this.#segmentValues) ?? undefined); |
| 169 | } |
| 170 | |
| 171 | constructor(opts: DateOptions) { |
| 172 | const detected = opts.format |
| 173 | ? { segments: segmentsFor(opts.format), separator: opts.separator ?? '/' } |
| 174 | : detectLocaleFormat(opts.locale); |
| 175 | const sep = opts.separator ?? detected.separator; |
| 176 | const segments = opts.format ? segmentsFor(opts.format) : detected.segments; |
| 177 | |
| 178 | const initialDate = opts.initialValue ?? opts.defaultValue; |
| 179 | const segmentValues: DateParts = initialDate |
| 180 | ? { |
| 181 | year: String(initialDate.getUTCFullYear()).padStart(4, '0'), |
| 182 | month: String(initialDate.getUTCMonth() + 1).padStart(2, '0'), |
| 183 | day: String(initialDate.getUTCDate()).padStart(2, '0'), |
| 184 | } |
| 185 | : { year: '____', month: '__', day: '__' }; |
| 186 | |
| 187 | const initialDisplay = segments.map((s) => segmentValues[s.type]).join(sep); |
nothing calls this directly
no outgoing calls
no test coverage detected