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 | 328x 328x 8x 8x 8x 2x 2x 2x 1x 1x 1x 1x 1x | import type {DateTime} from '@gravity-ui/date-utils';
import type {ValidationState} from '../../types';
import type {IncompleteDate} from '../IncompleteDate';
import type {DateFieldSection, FormatInfo} from '../types';
import {PAGE_STEP} from '../utils';
interface BaseDateFieldStateOptions<T = DateTime, V = IncompleteDate> {
value: T | null;
displayValue: T;
validationState?: ValidationState;
editableSections: DateFieldSection[];
formatInfo: FormatInfo;
readOnly?: boolean;
disabled?: boolean;
isEmpty: boolean;
setValue: (value: T | V | null) => void;
adjustSection: (sectionIndex: number, amount: number) => void;
setSection: (sectionIndex: number, amount: number) => void;
clearSection: (sectionIndex: number) => void;
setValueFromString: (str: string) => boolean;
confirmPlaceholder: () => void;
}
export interface 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 | null) => void;
/** Updates the remaining unfilled sections with the placeholder value. */
confirmPlaceholder: () => void;
/** Whether the field is read only. */
readOnly?: boolean;
/** Whether the field is disabled. */
disabled?: boolean;
/** A list of sections for the current value. */
sections: DateFieldSection[];
/** Some info about available sections */
formatInfo: FormatInfo;
/** The current validation state of the date field, based on the `validationState`, `minValue`, and `maxValue` props. */
validationState?: ValidationState;
/** Increments the currently selected section. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit. */
increment: (sectionIndex: number) => void;
/** Decrements the currently selected section. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit. */
decrement: (sectionIndex: number) => void;
/**
* Increments the currently selected section 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: (sectionIndex: number) => void;
/**
* Decrements the currently selected section 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: (sectionIndex: number) => void;
incrementToMax: (sectionIndex: number) => void;
decrementToMin: (sectionIndex: number) => void;
/** Clears the value of the currently selected section, reverting it to the placeholder. */
clearSection: (sectionIndex: number) => void;
/** Clears all sections, reverting them to the placeholder. */
clearAll: () => void;
/** Sets the value of the given section. */
setSection: (sectionIndex: number, amount: number) => void;
//** Tries to set value from str. Supports date in input format or ISO */
setValueFromString: (str: string) => boolean;
}
export function useBaseDateFieldState<T = DateTime, V = IncompleteDate>(
props: BaseDateFieldStateOptions<T, V>,
): DateFieldState<T> {
const {
value,
validationState,
displayValue,
editableSections,
formatInfo,
isEmpty,
setValue,
adjustSection,
setSection,
clearSection,
setValueFromString,
confirmPlaceholder,
} = props;
return {
value,
isEmpty,
displayValue,
setValue,
confirmPlaceholder,
readOnly: props.readOnly,
disabled: props.disabled,
sections: editableSections,
formatInfo,
validationState,
setSection,
increment(sectionIndex) {
Iif (this.readOnly || this.disabled) {
return;
}
Eif (sectionIndex !== -1) {
adjustSection(sectionIndex, 1);
}
},
decrement(sectionIndex) {
if (this.readOnly || this.disabled) {
return;
}
if (sectionIndex !== -1) {
adjustSection(sectionIndex, -1);
}
},
incrementPage(sectionIndex) {
Iif (this.readOnly || this.disabled) {
return;
}
Eif (sectionIndex !== -1) {
adjustSection(sectionIndex, PAGE_STEP[this.sections[sectionIndex].type] || 1);
}
},
decrementPage(sectionIndex) {
if (this.readOnly || this.disabled) {
return;
}
if (sectionIndex !== -1) {
adjustSection(sectionIndex, -(PAGE_STEP[this.sections[sectionIndex].type] || 1));
}
},
incrementToMax(sectionIndex) {
if (this.readOnly || this.disabled) {
return;
}
if (sectionIndex !== -1) {
const section = this.sections[sectionIndex];
if (typeof section.maxValue === 'number') {
setSection(sectionIndex, section.maxValue);
}
}
},
decrementToMin(sectionIndex) {
if (this.readOnly || this.disabled) {
return;
}
if (sectionIndex !== -1) {
const section = this.sections[sectionIndex];
if (typeof section.minValue === 'number') {
setSection(sectionIndex, section.minValue);
}
}
},
clearSection(sectionIndex) {
Iif (this.readOnly || this.disabled) {
return;
}
Iif (sectionIndex === -1) {
return;
}
clearSection(sectionIndex);
},
clearAll() {
Iif (this.readOnly || this.disabled) {
return;
}
setValue(null);
},
setValueFromString,
};
}
|