Skip to content

Commit 556ea65

Browse files
jvictordev1jvictordev1
andauthored
fix: remove emissão de objeto vazio ao realizar blur (#1096)
#### Por favor, verifique se o seu pull request está de acordo com o checklist abaixo: - [x] A implementação feita possui testes (Caso haja um motivo para não haver testes/haver apenas testes de snapshot, descrever abaixo) - [x] A documentação no mdx foi feita ou atualizada, caso necessário - [x] O eslint passou localmente ### 1 - Resumo Ao focar no select (com o mouse ou teclado) e depois desfocar o model era atualizado para um objeto vazio `{}`. Esse comportamento também acontecia quando selecionávamos uma opção, depois pesquisassemos por um item inexistente e desfocasse o input. Isso é um problema pois nos nossos formulários o objeto vazio é truthy e, com isso, enviado ao back-end. Caso o usuário foque num select não obrigatório e tente enviar o formulário o back-end enviará um erro de validação ou, se não houver uma validação bem implementada, armazenará o objeto vazio na base. ### 2 - Tipo de pull request - [ ] 🧱 Novo componente - [ ] ✨ Nova feature ou melhoria - [X] 🐛 Fix - [ ] 👨‍💻 Refatoração - [ ] 📝 Documentação - [ ] 🎨 Estilo - [ ] 🤖 Build ou CI/CD ### 3 - Esse PR fecha alguma issue? Favor referenciá-la #1095 ### 4 - Quais são os passos para avaliar o pull request? - Foque no select com o mouse ou teclado e depois desfoque sem selecionar nada; verifique que o evento `update:model-value` não é disparado - Com o input no modo searchable selecione um item qualquer, depois pesquise por um item inexistente e desfoque sem selecionar nada; verifique que o evento `update:model-value` é disparado com `null` ### 5 - Imagem ou exemplo de uso: [Gravação de tela de 2026-06-29 10-52-11.webm](https://github.com/user-attachments/assets/a55573f8-3fe7-4fbc-b55e-19887cc02c60) ### 6 - Esse pull request adiciona _breaking changes_? - [ ] Sim - [X] Não --------- Co-authored-by: jvictordev1 <jvictordev1@gmail.com>
1 parent 4e4ec33 commit 556ea65

3 files changed

Lines changed: 62 additions & 9 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sysvale/cuida",
3-
"version": "3.158.3",
3+
"version": "3.158.4",
44
"description": "A design system built by Sysvale, using storybook and Vue components",
55
"repository": {
66
"type": "git",

‎src/components/Select.vue‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ function filterOptions(value) {
446446
return;
447447
}
448448
449-
if (props.searchable && props.addable) {
449+
if (props.searchable) {
450450
searchString.value = value;
451451
}
452452
@@ -601,11 +601,6 @@ function hide() {
601601
localValue.value = null;
602602
}
603603
604-
if (!searchString.value && !shouldClearSelection) {
605-
localValue.value = localOptions.value.some(item => item[props.optionsField]?.toLowerCase() === get(localValue.value, props.optionsField)?.toLowerCase())
606-
? localValue.value
607-
: {};
608-
}
609604
610605
nextTick(() => {
611606
localOptions.value = pristineOptions.value;

‎src/tests/Select.spec.js‎

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { describe, test, expect } from 'vitest';
21
import { mount } from '@vue/test-utils';
3-
import Select from '../components/Select.vue';
2+
import { describe, expect, test } from 'vitest';
43
import CdsBaseInput from '../components/BaseInput.vue';
4+
import Select from '../components/Select.vue';
55

66
describe('Select', () => {
77
test('renders correctly', () => {
@@ -180,4 +180,62 @@ describe('Select', () => {
180180
await wrapper.find('input').trigger('blur');
181181
expect(wrapper.vm.active).toBe(false);
182182
});
183+
184+
test('does not emit update:modelValue when focused and blurred without changes (when modelValue is empty)', async () => {
185+
const wrapper = mount(Select, {
186+
props: {
187+
label: 'label',
188+
id: 'select-input',
189+
options: [{ value: 'Option 1' }, { value: 'Option 2' }],
190+
modelValue: undefined,
191+
},
192+
});
193+
194+
await wrapper.find('input').trigger('focus');
195+
await wrapper.find('input').trigger('blur');
196+
197+
expect(wrapper.emitted('update:modelValue')).toBeUndefined();
198+
});
199+
200+
test('does not reset modelValue when blurred with active filtered search (searchable: true)', async () => {
201+
const wrapper = mount(Select, {
202+
props: {
203+
label: 'label',
204+
id: 'select-input',
205+
options: [{ value: 'Option 1' }, { value: 'Option 2' }],
206+
searchable: true,
207+
modelValue: { value: 'Option 1' },
208+
},
209+
});
210+
211+
await wrapper.find('input').trigger('focus');
212+
await wrapper.find('input').setValue('Option 2');
213+
await wrapper.findComponent(CdsBaseInput).trigger('input');
214+
215+
await wrapper.find('input').trigger('blur');
216+
217+
expect(wrapper.emitted('update:modelValue')).toBeUndefined();
218+
expect(wrapper.vm.localValue).toEqual({ value: 'Option 1' });
219+
});
220+
221+
test('set modelValue to null when blurred with active filtered search that does not find any items (searchable: true)', async () => {
222+
const wrapper = mount(Select, {
223+
props: {
224+
label: 'label',
225+
id: 'select-input',
226+
options: [{ value: 'Option 1' }, { value: 'Option 2' }],
227+
searchable: true,
228+
modelValue: { value: 'Option 1' },
229+
},
230+
});
231+
232+
await wrapper.find('input').trigger('focus');
233+
await wrapper.find('input').setValue('Not an option');
234+
await wrapper.findComponent(CdsBaseInput).trigger('input');
235+
236+
await wrapper.find('input').trigger('blur');
237+
238+
expect(wrapper.emitted('update:modelValue')).toStrictEqual([[ null ]]);
239+
expect(wrapper.vm.localValue).toBeNull();
240+
});
183241
});

0 commit comments

Comments
 (0)