From 0d2d41168567986c05e50f8d219338d8e02daf0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=9C=D0=B0?= =?UTF-8?q?=D0=BB=D1=8E=D0=B3=D0=B8=D0=BD?= <d.malygin@iqdev.digital> Date: Fri, 14 Feb 2025 14:23:34 +0500 Subject: [PATCH] feat: 'InputDiv' logic of input in process --- src/components/InputDiv/InputDiv.vue | 72 ++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 20 deletions(-) diff --git a/src/components/InputDiv/InputDiv.vue b/src/components/InputDiv/InputDiv.vue index ee251f0..bafe63d 100644 --- a/src/components/InputDiv/InputDiv.vue +++ b/src/components/InputDiv/InputDiv.vue @@ -13,7 +13,8 @@ const props = withDefaults(defineProps<IInputDivProps>(), { }); const value = defineModel() as Ref<string>; -const container = document.querySelector('#inputDiv-container'); +let container; +setTimeout(() => (container = document.querySelector('#inputDiv-container')), 0); const inputPartsBy = computed(() => calcPartsBy(props.scheme)); const inputPartsDash = computed(() => calcPartsDash(props.scheme)); @@ -29,33 +30,64 @@ const inputHeight = computed(() => getValueFromSize(props.size, ['30px', '36px', const fontSize = computed(() => getValueFromSize(props.size, ['12px', '16px', '24px', '32px'])); const borderWidth = computed(() => (props.size === 'small' || props.size === 'normal' ? '1px' : '2px')); -const toggleInput = () => {}; +const toggleInput = (itemIndex: number, inputIndex: number) => { + let currentInput; + let currentItem; + const list = Array(container?.children[inputPartsBy.value ? 0 : 1].children)[0]; + + cycle: for (let i = 0; i < list.length; i++) { + const item = list[i]; + if (!item.classList.contains(itemIndex)) continue; + for (const child of item.children) { + if (child.classList.contains(inputIndex)) { + currentInput = child; + currentItem = item; + break cycle; + } + } + } + // еÑли значение ввели + if (currentInput.value) { + let nextInputInSameItem = null; + for (const child of currentItem.children) { + if (child.classList.contains(inputIndex + 1)) { + nextInputInSameItem = child; + break; + } + } + if (nextInputInSameItem) { + nextInputInSameItem.focus(); + } else { + // обработка Ñледующей чаÑти, еÑли она еÑÑ‚ÑŒ, иначе ничего не делать (или оÑтавить Ñтарое значение, что ещё лучше) + } + } else { + // еÑли значение удалили + let prevInputInSameItem = null; + for (const child of currentItem.children) { + if (child.classList.contains(inputIndex - 1)) { + prevInputInSameItem = child; + break; + } + } + if (prevInputInSameItem) { + prevInputInSameItem.focus(); + } else { + // обработка предыдущей чаÑти, еÑли она еÑÑ‚ÑŒ, иначе ничего не делать + } + } +}; </script> <template> <section id="inputDiv-container"> <div v-show="inputPartsBy" class="list"> - <div - v-for="(item, itemIndex) of inputPartsBy" - :key="item" - :class="[ - 'item', - { - itemIndex, - }, - ]" - > + <div v-for="(item, itemIndex) of inputPartsBy" :key="item" :class="`item ${itemIndex}`"> <input v-for="(input, inputIndex) of item" :key="inputIndex" - @change="toggleInput(itemIndex, inputIndex)" + @input.prevent="toggleInput(itemIndex, +inputIndex)" type="text" - :class="[ - 'input', - { - inputIndex, - }, - ]" + :class="`input ${inputIndex}`" /> </div> </div> @@ -73,7 +105,7 @@ const toggleInput = () => {}; <input v-for="(input, inputIndex) of item" :key="inputIndex" - @change="toggleInput(itemIndex, inputIndex)" + @input="toggleInput(itemIndex, +inputIndex)" type="text" :class="[ 'input', -- GitLab