Skip to content

Commit bed27f6

Browse files
author
“RafaelGondi”
committed
feat: implementa focus automático ao criar novo input no MultiInput e evita que sejam criados novos inputs quando o último estiver vazio
1 parent c5b9575 commit bed27f6

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/components/MultiInput.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
gap="3"
1212
>
1313
<CdsTextInput
14+
:ref="(el) => { inputRefs[index] = el }"
1415
v-model="item.label"
1516
floating-label
1617
:label="inputLabel"
@@ -52,7 +53,7 @@
5253
</template>
5354

5455
<script setup>
55-
import { defineProps, ref, watch } from 'vue';
56+
import { defineProps, ref, watch, nextTick } from 'vue';
5657
import CdsTextInput from './TextInput.vue';
5758
import CdsIcon from './Icon.vue';
5859
import CdsFlexbox from './Flexbox.vue';
@@ -87,6 +88,7 @@ defineProps({
8788
8889
8990
const internalModel = ref([]);
91+
const inputRefs = ref([]);
9092
9193
watch(model, (newValue) => {
9294
if (JSON.stringify(newValue) === JSON.stringify(internalModel.value)) return;
@@ -101,10 +103,16 @@ watch(internalModel, (newValue) => {
101103
}, { deep: true });
102104
103105
function addInput() {
106+
if (internalModel.value[internalModel.value.length - 1].label === '') return;
107+
104108
internalModel.value.push({
105109
value: generateKey(),
106110
label: '',
107111
});
112+
113+
nextTick(() => {
114+
inputRefs.value[internalModel.value.length - 1]?.focus();
115+
});
108116
}
109117
110118
function removeInput(index) {

0 commit comments

Comments
 (0)