MCPcopy
hub / github.com/bombshell-dev/clack / #onKey

Method #onKey

packages/core/src/prompts/date.ts:270–407  ·  view source on GitHub ↗
(char: string | undefined, key: Key)

Source from the content-addressed store, hash-verified

268 }
269
270 #onKey(char: string | undefined, key: Key) {
271 // Backspace
272 const isBackspace =
273 key?.name === 'backspace' ||
274 key?.sequence === '\x7f' ||
275 key?.sequence === '\b' ||
276 char === '\x7f' ||
277 char === '\b';
278 if (isBackspace) {
279 this.inlineError = '';
280 const ctx = this.#seg();
281 if (!ctx) return;
282 if (!this.#segmentValues[ctx.segment.type].replace(/_/g, '')) {
283 this.#navigate(-1);
284 return;
285 }
286 this.#segmentValues[ctx.segment.type] = '_'.repeat(ctx.segment.len);
287 this.#segmentSelected = true;
288 this.#cursor.positionInSegment = 0;
289 this.#refresh();
290 return;
291 }
292
293 // Tab navigation
294 if (key?.name === 'tab') {
295 this.inlineError = '';
296 const ctx = this.#seg();
297 if (!ctx) return;
298 const dir = key.shift ? -1 : 1;
299 const next = ctx.index + dir;
300 if (next >= 0 && next < this.#segments.length) {
301 this.#cursor.segmentIndex = next;
302 this.#cursor.positionInSegment = 0;
303 this.#segmentSelected = true;
304 }
305 return;
306 }
307
308 // Digit input
309 if (char && /^[0-9]$/.test(char)) {
310 const ctx = this.#seg();
311 if (!ctx) return;
312 const { segment } = ctx;
313 const isBlank = !this.#segmentValues[segment.type].replace(/_/g, '');
314
315 // Pending tens digit: complete the two-digit entry
316 if (this.#segmentSelected && this.#pendingTensDigit !== null && !isBlank) {
317 const newVal = this.#pendingTensDigit + char;
318 const newParts = { ...this.#segmentValues, [segment.type]: newVal };
319 const err = this.#validateSegment(newParts, segment);
320 if (err) {
321 this.inlineError = err;
322 this.#pendingTensDigit = null;
323 this.#segmentSelected = false;
324 return;
325 }
326 this.inlineError = '';
327 this.#segmentValues[segment.type] = newVal;

Callers 1

constructorMethod · 0.95

Calls 6

#segMethod · 0.95
#navigateMethod · 0.95
#refreshMethod · 0.95
#validateSegmentMethod · 0.95
validPartsFunction · 0.85
daysInMonthFunction · 0.85

Tested by

no test coverage detected