Newer
Older
import type { TInputDivScheme } from '@interfaces/componentsProp';
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
export const changeInputHandler = (
target: any,
container: HTMLElement,
inputPartsBy: boolean,
valueParts: string[],
indexesToValueIndex: Record<string, number>,
itemIndex: number,
inputIndex: number,
backspace: boolean,
) => {
let currentInput: HTMLInputElement | null = null;
let currentItem: HTMLElement | null = null;
let currentItemIndex: number | null;
const list = Array.from(container?.children[inputPartsBy ? 0 : 1].children);
cycle: for (let i = 0; i < list.length; i++) {
const item = list[i];
if (!item.classList.contains(String(itemIndex))) continue;
for (const child of item.children) {
if (child.classList.contains(String(inputIndex))) {
currentInput = child as HTMLInputElement;
currentItem = item as HTMLElement;
currentItemIndex = i;
break cycle;
}
}
}
// если значение ввели
if (currentInput?.value && currentItem) {
const valueIndex = indexesToValueIndex[
(itemIndex + '-' + inputIndex) as keyof typeof indexesToValueIndex
] as number;
if (target.value.length === 2) {
currentInput!.value = target.value[0] === valueParts[valueIndex] ? target.value[1] : target.value[0];
}
valueParts[valueIndex] = currentInput!.value;
// поиск следующего инпута в той же части (если есть)
let nextInputInSameItem: HTMLInputElement | null = null;
for (const child of currentItem.children) {
if (child.classList.contains(String(inputIndex + 1))) {
nextInputInSameItem = child as HTMLInputElement;
break;
}
}
if (nextInputInSameItem) {
nextInputInSameItem.focus();
} else {
// обработка следующей части, если она есть, иначе ничего не делать (или оставить старое значение, что ещё лучше)
currentItem = list?.[currentItemIndex! + 1] as HTMLElement | null;
if (currentItem) {
const targetInput = Array.from(currentItem.children)[0] as HTMLInputElement;
targetInput.focus();
}
}
} else if (backspace && currentItem) {
// если значение удалили
let prevInputInSameItem: HTMLInputElement | null = null;
for (const child of currentItem.children) {
if (child.classList.contains(String(inputIndex - 1))) {
prevInputInSameItem = child as HTMLInputElement;
break;
}
}
if (prevInputInSameItem) {
prevInputInSameItem.focus();
} else {
// обработка предыдущей части, если она есть, иначе ничего не делать
currentItem = list?.[currentItemIndex! - 1] as HTMLElement | null;
if (currentItem) {
const children = Array.from(currentItem.children);
const targetInput = children[children.length - 1] as HTMLInputElement;
targetInput.focus();
}
}
}
return valueParts;
};
export const moveFocus = (
direction: 'left' | 'right',
container: HTMLElement,
inputPartsBy: boolean,
itemIndex: number,
inputIndex: number,
) => {
let currentItem: HTMLElement | null;
let currentItemIndex: number | null;
let currentInputIndex: number | null;
const list = Array.from(container?.children[inputPartsBy ? 0 : 1].children);
cycle: for (let i = 0; i < list.length; i++) {
const item = list[i];
if (!item.classList.contains(String(itemIndex))) continue;
const itemChildren = Array.from(item.children);
for (let j = 0; j < itemChildren.length; j++) {
if (itemChildren[j].classList.contains(String(inputIndex))) {
currentItem = item as HTMLElement;
currentItemIndex = i;
currentInputIndex = j;
break cycle;
}
}
}
if (direction === 'left') {
let targetInput = [...currentItem!.children][currentInputIndex! - 1] as HTMLInputElement | null;
if (targetInput) {
targetInput.focus();
return;
}
currentItem = list?.[currentItemIndex! - 1] as HTMLElement | null;
if (currentItem) {
const itemChildren = [...currentItem.children];
targetInput = itemChildren[itemChildren.length - 1] as HTMLInputElement;
targetInput.focus();
}
} else {
let targetInput = [...currentItem!.children][currentInputIndex! + 1] as HTMLInputElement | null;
if (targetInput) {
targetInput.focus();
return;
}
currentItem = list?.[currentItemIndex! + 1] as HTMLElement | null;
if (currentItem) {
targetInput = [...currentItem.children][0] as HTMLInputElement;
targetInput.focus();
}
}
};
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
export const calcPartsBy = (scheme: TInputDivScheme) => {
if (!scheme.includes('by')) return null;
const splat = scheme.split('by');
const by = splat[1];
const result = [];
for (let i = 0; i < +splat[0]; i++) {
const arrayToPush = [];
for (let j = 0; j < +by; j++) {
arrayToPush.push(j);
}
result.push(arrayToPush);
}
return result;
};
export const calcPartsDash = (scheme: TInputDivScheme) => {
if (!scheme.includes('-')) return null;
const splat = scheme.split('-');
const result = [];
for (let i = 0; i < splat.length; i++) {
const item = splat[i];
const arrayToPush = [];
for (let j = 0; j < +item; j++) {
arrayToPush.push(j);
}
result.push(arrayToPush);
}
return result;
};