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 | 17x 124x 124x 10x 10x 124x 105x 105x 10x 95x 81x 81x 14x 14x 14x 14x 14x 14x 12x 1x 1x 124x 58x 46x 12x 12x 12x 124x 5x 6x 6x 6x 6x 6x 6x 6x 1x 5x 4x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x | import React from 'react';
import type {DateTime} from '@gravity-ui/date-utils';
import type {TextInputProps} from '@gravity-ui/uikit';
import type {
AccessibilityProps,
DateFieldBase,
TextInputProps as DateFieldTextInputProps,
DomProps,
FocusableProps,
InputDOMProps,
KeyboardEvents,
StyleProps,
TextInputExtendProps,
} from '../../types';
import {cleanString} from '../utils';
import type {DateFieldState} from './useBaseDateFieldState';
export interface DateFieldProps<T = DateTime>
extends
DateFieldBase<T>,
DateFieldTextInputProps,
TextInputExtendProps,
DomProps,
InputDOMProps,
FocusableProps,
KeyboardEvents,
StyleProps,
AccessibilityProps {}
export function useDateFieldProps<T = DateTime>(
state: DateFieldState<T>,
props: DateFieldProps<T>,
): {inputProps: TextInputProps} {
const inputRef = React.useRef<HTMLInputElement>(null);
const [, setInnerState] = React.useState({});
function setSelectedSections(section: 'all' | number) {
state.setSelectedSections(section);
setInnerState({});
}
React.useLayoutEffect(() => {
const inputElement = inputRef.current;
if (!inputElement) {
return;
}
if (state.selectedSectionIndexes === null) {
Iif (inputElement.scrollLeft) {
// Ensure that input content is not marked as selected.
// setting selection range to 0 causes issues in Safari.
// https://bugs.webkit.org/show_bug.cgi?id=224425
inputElement.scrollLeft = 0;
}
return;
}
const firstSelectedSection = state.sections[state.selectedSectionIndexes.startIndex];
const lastSelectedSection = state.sections[state.selectedSectionIndexes.endIndex];
Eif (firstSelectedSection && lastSelectedSection) {
const selectionStart = firstSelectedSection.start;
const selectionEnd = lastSelectedSection.end;
if (
selectionStart !== inputElement.selectionStart ||
selectionEnd !== inputElement.selectionEnd
) {
inputElement.setSelectionRange(selectionStart, selectionEnd);
}
}
});
function syncSelectionFromDOM() {
state.focusSectionInPosition(inputRef.current?.selectionStart ?? 0);
setInnerState({});
}
const inputMode = React.useMemo(() => {
if (!state.selectedSectionIndexes) {
return 'text';
}
const activeSection = state.sections[state.selectedSectionIndexes.startIndex];
Iif (!activeSection || activeSection.contentType === 'letter') {
return 'text';
}
return 'tel';
}, [state.selectedSectionIndexes, state.sections]);
return {
inputProps: {
value: state.text,
view: props.view,
size: props.size,
disabled: state.disabled,
hasClear: !state.readOnly && !state.isEmpty && props.hasClear,
placeholder: props.placeholder,
id: props.id,
label: props.label,
startContent: props.startContent,
endContent: props.endContent,
pin: props.pin,
autoFocus: props.autoFocus,
controlRef: inputRef,
autoComplete: 'off',
type: 'text',
validationState: state.validationState,
errorMessage: props.errorMessage,
errorPlacement: props.errorPlacement,
onUpdate(value) {
Iif (!value) {
state.clearAll();
}
},
onFocus(e) {
props.onFocus?.(e);
Iif (state.selectedSectionIndexes !== null) {
return;
}
const input = e.target;
const isAutofocus = !inputRef.current;
setTimeout(() => {
Iif (!input || input !== inputRef.current) {
return;
}
if (isAutofocus) {
state.focusSectionInPosition(0);
} else if (
// avoid selecting all sections when focusing empty field without value
input.value.length &&
Number(input.selectionEnd) - Number(input.selectionStart) ===
input.value.length
) {
setSelectedSections('all');
} else {
syncSelectionFromDOM();
}
});
},
onBlur(e) {
props.onBlur?.(e);
setSelectedSections(-1);
},
onKeyDown(e) {
props.onKeyDown?.(e);
Iif (e.key === 'ArrowLeft') {
e.preventDefault();
state.focusPreviousSection();
I} else if (e.key === 'ArrowRight') {
e.preventDefault();
state.focusNextSection();
I} else if (e.key === 'Home') {
e.preventDefault();
state.decrementToMin();
I} else if (e.key === 'End') {
e.preventDefault();
state.incrementToMax();
} else if (e.key === 'ArrowUp' && !e.altKey) {
e.preventDefault();
state.increment();
E} else if (e.key === 'ArrowDown' && !e.altKey) {
e.preventDefault();
state.decrement();
} else if (e.key === 'PageUp') {
e.preventDefault();
state.incrementPage();
} else if (e.key === 'PageDown') {
e.preventDefault();
state.decrementPage();
} else if (e.key === 'Backspace' || e.key === 'Delete') {
e.preventDefault();
state.clearSection();
} else if (e.key === 'a' && (e['ctrlKey'] || e['metaKey'])) {
e.preventDefault();
setSelectedSections('all');
}
},
onKeyUp: props.onKeyUp,
controlProps: {
'aria-label': props['aria-label'] || undefined,
'aria-labelledby': props['aria-labelledby'] || undefined,
'aria-describedby': props['aria-describedby'] || undefined,
'aria-details': props['aria-details'] || undefined,
'aria-disabled': state.disabled || undefined,
readOnly: state.readOnly,
inputMode,
onClick() {
syncSelectionFromDOM();
},
onMouseUp(e: React.MouseEvent) {
e.preventDefault();
},
onBeforeInput(e) {
e.preventDefault();
const key = e.data;
if (key !== undefined && key !== null) {
state.onInput(key);
}
},
onPaste(e: React.ClipboardEvent) {
e.preventDefault();
if (state.readOnly) {
return;
}
const pastedValue = cleanString(e.clipboardData.getData('text'));
if (
state.selectedSectionIndexes &&
state.selectedSectionIndexes.startIndex ===
state.selectedSectionIndexes.endIndex
) {
const activeSection =
state.sections[state.selectedSectionIndexes.startIndex];
const digitsOnly = /^\d+$/.test(pastedValue);
const lettersOnly = /^[a-zA-Z]+$/.test(pastedValue);
const isValidValue = Boolean(
activeSection &&
((activeSection.contentType === 'digit' && digitsOnly) ||
(activeSection.contentType === 'letter' && lettersOnly)),
);
if (isValidValue) {
state.onInput(pastedValue);
return;
}
if (digitsOnly || lettersOnly) {
return;
}
}
state.setValueFromString(pastedValue);
},
},
},
};
}
|