Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 | 18x 18x 187x 187x 187x 21x 21x 24x 4x 4x 4x 4x 5x 5x 5x 5x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 14x 14x 14x 14x 14x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x | import React from 'react';
import type {DateTime} from '@gravity-ui/date-utils';
import type {ValidationState} from '../../types';
import {createPlaceholderValue} from '../../utils/dates';
import type {DateFieldSection, DateFieldSectionType, FormatInfo} from '../types';
import {
EDITABLE_SEGMENTS,
formatSections,
getCurrentEditableSectionIndex,
getDurationUnitFromSectionType,
} from '../utils';
const PAGE_STEP: Partial<Record<DateFieldSectionType, number>> = {
year: 5,
quarter: 2,
month: 2,
weekday: 3,
day: 7,
hour: 2,
minute: 15,
second: 15,
};
export type BaseDateFieldStateOptions<T = DateTime> = {
value: T | null;
displayValue: T;
placeholderValue?: DateTime;
timeZone: string;
validationState?: ValidationState;
editableSections: DateFieldSection[];
formatInfo: FormatInfo;
readOnly?: boolean;
disabled?: boolean;
selectedSectionIndexes: {startIndex: number; endIndex: number} | null;
selectedSections: number | 'all';
isEmpty: boolean;
flushAllValidSections: () => void;
flushValidSection: (sectionIndex: number) => void;
setSelectedSections: (position: number | 'all') => void;
setValue: (value: T) => void;
setDate: (value: T | null) => void;
adjustSection: (sectionIndex: number, amount: number) => void;
setSection: (sectionIndex: number, amount: number) => void;
getSectionValue: (sectionIndex: number) => DateTime;
setSectionValue: (sectionIndex: number, currentValue: DateTime) => void;
createPlaceholder: () => T;
setValueFromString: (str: string) => boolean;
};
export type DateFieldState<T = DateTime> = {
/** The current field value. */
value: T | null;
/** Is no part of value is filled. */
isEmpty: boolean;
/** The current used value. value or placeholderValue */
displayValue: T;
/** Sets the field's value. */
setValue: (value: T) => void;
/** Sets the date value. */
setDate: (value: T | null) => void;
/** Formatted value */
text: string;
/** Whether the field is read only. */
readOnly?: boolean;
/** Whether the field is disabled. */
disabled?: boolean;
/** A list of segments for the current value. */
sections: DateFieldSection[];
/** Some info about available sections */
formatInfo: FormatInfo;
/**
* @deprecated use formatInfo.hasDate instead.
* Whether the the format is containing date parts
*/
hasDate: boolean;
/**
* @deprecated use formatInfo.hasTime instead.
* Whether the the format is containing time parts
*/
hasTime: boolean;
/** Selected sections */
selectedSectionIndexes: {startIndex: number; endIndex: number} | null;
/** The current validation state of the date field, based on the `validationState`, `minValue`, and `maxValue` props. */
validationState?: ValidationState;
/** Sets selection for segment in position. */
setSelectedSections: (position: number | 'all') => void;
/** Focuses segment in position */
focusSectionInPosition: (position: number) => void;
/** Focuses the next segment if present */
focusNextSection: () => void;
/** Focuses the previous segment if present */
focusPreviousSection: () => void;
/** Focuses the first segment */
focusFirstSection: () => void;
/** Focuses the last segment */
focusLastSection: () => void;
/** Increments the currently selected segment. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit. */
increment: () => void;
/** Decrements the currently selected segment. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit. */
decrement: () => void;
/**
* Increments the currently selected segment by a larger amount, rounding it to the nearest increment.
* The amount to increment by depends on the field, for example 15 minutes, 7 days, and 5 years.
* Upon reaching the minimum or maximum value, the value wraps around to the opposite limit.
*/
incrementPage: () => void;
/**
* Decrements the currently selected segment by a larger amount, rounding it to the nearest decrement.
* The amount to increment by depends on the field, for example 15 minutes, 7 days, and 5 years.
* Upon reaching the minimum or maximum value, the value wraps around to the opposite limit.
*/
decrementPage: () => void;
incrementToMax: () => void;
decrementToMin: () => void;
/** Clears the value of the currently selected segment, reverting it to the placeholder. */
clearSection: () => void;
/** Clears all segments, reverting them to the placeholder. */
clearAll: () => void;
/** Handles input key in the currently selected segment */
onInput: (key: string) => void;
//** Tries to set value from str. Supports date in input format or ISO */
setValueFromString: (str: string) => boolean;
};
export function useBaseDateFieldState<T = DateTime>(
props: BaseDateFieldStateOptions<T>,
): DateFieldState<T> {
const {
value,
validationState,
displayValue,
editableSections,
formatInfo,
selectedSectionIndexes,
selectedSections,
isEmpty,
flushAllValidSections,
flushValidSection,
setSelectedSections,
setValue,
setDate,
adjustSection,
setSection,
getSectionValue,
setSectionValue,
createPlaceholder,
setValueFromString,
} = props;
const enteredKeys = React.useRef('');
return {
value,
isEmpty,
displayValue,
setValue,
setDate,
text: formatSections(editableSections),
readOnly: props.readOnly,
disabled: props.disabled,
sections: editableSections,
formatInfo,
hasDate: formatInfo.hasDate,
hasTime: formatInfo.hasTime,
selectedSectionIndexes,
validationState,
setSelectedSections(position) {
enteredKeys.current = '';
setSelectedSections(position);
},
focusSectionInPosition(position) {
const nextSectionIndex = this.sections.findIndex((s) => s.end >= position);
const index = nextSectionIndex === -1 ? 0 : nextSectionIndex;
const nextSection = this.sections[index];
Eif (nextSection) {
this.setSelectedSections(
EDITABLE_SEGMENTS[nextSection.type] ? index : nextSection.nextEditableSection,
);
}
},
focusNextSection() {
const currentIndex = selectedSections === 'all' ? 0 : selectedSections;
const newIndex = this.sections[currentIndex]?.nextEditableSection ?? -1;
Eif (newIndex !== -1) {
this.setSelectedSections(newIndex);
}
},
focusPreviousSection() {
const currentIndex = selectedSections === 'all' ? 0 : selectedSections;
const newIndex = this.sections[currentIndex]?.previousEditableSection ?? -1;
Eif (newIndex !== -1) {
this.setSelectedSections(newIndex);
}
},
focusFirstSection() {
const newIndex = this.sections[0]?.previousEditableSection ?? -1;
Eif (newIndex !== -1) {
setSelectedSections(newIndex);
}
},
focusLastSection() {
const newIndex = this.sections[this.sections.length - 1]?.nextEditableSection ?? -1;
Eif (newIndex !== -1) {
this.setSelectedSections(newIndex);
}
},
increment() {
Iif (this.readOnly || this.disabled) {
return;
}
enteredKeys.current = '';
const sectionIndex = getCurrentEditableSectionIndex(this.sections, selectedSections);
Eif (sectionIndex !== -1) {
adjustSection(sectionIndex, 1);
}
},
decrement() {
if (this.readOnly || this.disabled) {
return;
}
enteredKeys.current = '';
const sectionIndex = getCurrentEditableSectionIndex(this.sections, selectedSections);
if (sectionIndex !== -1) {
adjustSection(sectionIndex, -1);
}
},
incrementPage() {
Iif (this.readOnly || this.disabled) {
return;
}
enteredKeys.current = '';
const sectionIndex = getCurrentEditableSectionIndex(this.sections, selectedSections);
Eif (sectionIndex !== -1) {
adjustSection(sectionIndex, PAGE_STEP[this.sections[sectionIndex].type] || 1);
}
},
decrementPage() {
if (this.readOnly || this.disabled) {
return;
}
enteredKeys.current = '';
const sectionIndex = getCurrentEditableSectionIndex(this.sections, selectedSections);
if (sectionIndex !== -1) {
adjustSection(sectionIndex, -(PAGE_STEP[this.sections[sectionIndex].type] || 1));
}
},
incrementToMax() {
Iif (this.readOnly || this.disabled) {
return;
}
enteredKeys.current = '';
const sectionIndex = getCurrentEditableSectionIndex(this.sections, selectedSections);
Eif (sectionIndex !== -1) {
const section = this.sections[sectionIndex];
Eif (typeof section.maxValue === 'number') {
setSection(sectionIndex, section.maxValue);
}
}
},
decrementToMin() {
if (this.readOnly || this.disabled) {
return;
}
enteredKeys.current = '';
const sectionIndex = getCurrentEditableSectionIndex(this.sections, selectedSections);
if (sectionIndex !== -1) {
const section = this.sections[sectionIndex];
if (typeof section.minValue === 'number') {
setSection(sectionIndex, section.minValue);
}
}
},
clearSection() {
Iif (this.readOnly || this.disabled) {
return;
}
enteredKeys.current = '';
Iif (selectedSections === 'all') {
this.clearAll();
return;
}
const sectionIndex = getCurrentEditableSectionIndex(this.sections, selectedSections);
Iif (sectionIndex === -1) {
return;
}
flushValidSection(sectionIndex);
const section = this.sections[sectionIndex];
const placeholder = createPlaceholderValue({
placeholderValue: props.placeholderValue,
timeZone: props.timeZone,
}).timeZone(props.timeZone);
const displayPortion = getSectionValue(sectionIndex);
let currentValue = displayPortion;
// Reset day period to default without changing the hour.
Iif (section.type === 'dayPeriod') {
const isPM = displayPortion.hour() >= 12;
const shouldBePM = placeholder.hour() >= 12;
if (isPM && !shouldBePM) {
currentValue = displayPortion.set('hour', displayPortion.hour() - 12);
} else if (!isPM && shouldBePM) {
currentValue = displayPortion.set('hour', displayPortion.hour() + 12);
}
} else {
const type = getDurationUnitFromSectionType(section.type);
currentValue = displayPortion.set(type, placeholder[type]());
}
setSectionValue(sectionIndex, currentValue);
},
clearAll() {
Iif (this.readOnly || this.disabled) {
return;
}
enteredKeys.current = '';
flushAllValidSections();
Iif (value !== null) {
setDate(null);
}
const date = createPlaceholder();
setValue(date);
},
onInput(key: string) {
if (this.readOnly || this.disabled) {
return;
}
const sectionIndex = getCurrentEditableSectionIndex(this.sections, selectedSections);
if (sectionIndex === -1) {
return;
}
const section = this.sections[sectionIndex];
const isLastSection = section.nextEditableSection === sectionIndex;
let newValue = enteredKeys.current + key;
const onInputNumber = (numberValue: number) => {
let sectionValue = section.type === 'month' ? numberValue - 1 : numberValue;
const sectionMaxValue = section.maxValue ?? 0;
const allowsZero = section.minValue === 0;
let shouldResetUserInput;
if (
// 12-hour clock format with AM/PM
section.type === 'hour' &&
(sectionMaxValue === 11 || section.minValue === 12)
) {
if (sectionValue > 12) {
sectionValue = Number(key);
newValue = key;
}
if (sectionValue === 0) {
sectionValue = -Infinity;
}
if (sectionValue === 12) {
sectionValue = 0;
}
if (section.minValue === 12) {
sectionValue += 12;
}
shouldResetUserInput = Number(newValue + '0') > 12;
} else if (sectionValue > sectionMaxValue) {
sectionValue = Number(key) - (section.type === 'month' ? 1 : 0);
newValue = key;
if (sectionValue > sectionMaxValue) {
enteredKeys.current = '';
return;
}
}
const shouldSetValue = sectionValue > 0 || (sectionValue === 0 && allowsZero);
if (shouldSetValue) {
setSection(sectionIndex, sectionValue);
}
if (shouldResetUserInput === undefined) {
shouldResetUserInput = Number(newValue + '0') > sectionMaxValue;
}
const isMaxLength = newValue.length >= String(sectionMaxValue).length;
if (shouldResetUserInput) {
enteredKeys.current = '';
if (shouldSetValue) {
this.focusNextSection();
}
} else if (isMaxLength && shouldSetValue && !isLastSection) {
this.focusNextSection();
} else {
enteredKeys.current = newValue;
}
};
const onInputString = (stringValue: string) => {
const options = section.options ?? [];
let sectionValue = stringValue.toLocaleUpperCase();
let foundOptions = options.filter((v) => v.startsWith(sectionValue));
if (foundOptions.length === 0) {
if (stringValue !== key) {
sectionValue = key.toLocaleUpperCase();
foundOptions = options.filter((v) => v.startsWith(sectionValue));
}
if (foundOptions.length === 0) {
enteredKeys.current = '';
return;
}
}
const foundValue = foundOptions[0];
const index = options.indexOf(foundValue);
if (section.type === 'dayPeriod') {
setSection(sectionIndex, index === 1 ? 12 : 0);
} else {
setSection(sectionIndex, index);
}
if (foundOptions.length > 1) {
enteredKeys.current = newValue;
} else {
enteredKeys.current = '';
this.focusNextSection();
}
};
switch (section.type) {
case 'day':
case 'hour':
case 'minute':
case 'second':
case 'quarter':
case 'year': {
if (!Number.isInteger(Number(newValue))) {
return;
}
const numberValue = Number(newValue);
onInputNumber(numberValue);
break;
}
case 'dayPeriod': {
onInputString(newValue);
break;
}
case 'weekday':
case 'month': {
if (Number.isInteger(Number(newValue))) {
const numberValue = Number(newValue);
onInputNumber(numberValue);
} else {
onInputString(newValue);
}
break;
}
}
},
setValueFromString(str: string) {
enteredKeys.current = '';
return setValueFromString(str);
},
};
}
|