-
Notifications
You must be signed in to change notification settings - Fork 7
Adicionar suporte a v-model ao SegmentedControl #1093
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,8 +8,8 @@ | |
| type="button" | ||
| class="segment-control__button" | ||
| :class="{ | ||
| 'segment-control__button--active': segment === activeSegment, | ||
| 'segment-control__button--inactive': segment !== activeSegment, | ||
| 'segment-control__button--active': segment === model, | ||
| 'segment-control__button--inactive': segment !== model, | ||
| }" | ||
| @click="handleClick(segment, index)" | ||
| > | ||
|
|
@@ -28,48 +28,74 @@ | |
| </button> | ||
| </div> | ||
| </template> | ||
| <script> | ||
|
|
||
| <script setup> | ||
| import { onMounted } from 'vue'; | ||
| import CdsIcon from './Icon.vue'; | ||
| import CdsTooltip from './Tooltip.vue'; | ||
|
|
||
| export default { | ||
| name: 'CdsSegmentedControl', | ||
| components: { | ||
| CdsIcon, | ||
| CdsTooltip, | ||
| defineOptions({ name: 'CdsSegmentedControl' }); | ||
|
|
||
| /** | ||
| * Prop utilizada como v-model do componente. | ||
| */ | ||
| const model = defineModel({ | ||
| type: String, | ||
| default: '', | ||
| }); | ||
|
|
||
| const props = defineProps({ | ||
| /** | ||
| * Array de strings que serão exibidos como opções do componente. | ||
| */ | ||
| segments: { | ||
| type: Array, | ||
| default: () => [], | ||
| }, | ||
| props: { | ||
| segments: { | ||
| type: Array, | ||
| default: () => [], | ||
| }, | ||
| withIcon: { | ||
| type: Boolean, | ||
| default: false, | ||
| }, | ||
| segmentsTooltipText: { | ||
| type: Array, | ||
| default: () => [], | ||
| }, | ||
| /** | ||
| * Se verdadeiro, exibe ícones no lugar de texto. | ||
| */ | ||
| withIcon: { | ||
| type: Boolean, | ||
| default: false, | ||
| }, | ||
|
|
||
| data() { | ||
| return { | ||
| activeSegment: '', | ||
| }; | ||
| /** | ||
| * Array de strings que serão exibidos como tooltip quando o mouse estiver sobre o ícone. | ||
| */ | ||
| segmentsTooltipText: { | ||
| type: Array, | ||
| default: () => [], | ||
| }, | ||
| }); | ||
|
|
||
| mounted() { | ||
| this.activeSegment = this.segments[0]; | ||
| }, | ||
| const emit = defineEmits([ | ||
| /** | ||
| * Evento emitido quando o usuário clica em algum segmento. | ||
| * @event click | ||
| */ | ||
| 'click', | ||
| /** | ||
| * Evento emitido quando o usuário clica em algum segmento. | ||
| * @event update:model-value | ||
| */ | ||
| 'update:model-value', | ||
| ]); | ||
|
|
||
| methods: { | ||
| handleClick(segment, index) { | ||
| this.activeSegment = segment; | ||
| this.$emit('click', this.activeSegment, index); | ||
| }, | ||
| }, | ||
| } | ||
| onMounted(() => { | ||
| if (!model.value && props.segments.length > 0) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Rule Used: What: Sempre responda em português (PT-BR) durante... (source) |
||
| model.value = props.segments[0]; | ||
| } | ||
|
Comment on lines
+84
to
+87
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Rule Used: What: Sempre responda em português (PT-BR) durante... (source) |
||
| }); | ||
|
Comment on lines
+84
to
+88
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Rule Used: What: Sempre responda em português (PT-BR) durante... (source) |
||
|
|
||
| const handleClick = (segment, index) => { | ||
| model.value = segment; | ||
| /** | ||
| * Evento emitido quando o componente é clicado. | ||
| * @event click | ||
| * @type {Event} | ||
| */ | ||
| emit('click', segment, index); | ||
| }; | ||
| </script> | ||
|
|
||
| <style lang="scss"> | ||
|
|
@@ -116,4 +142,4 @@ export default { | |
| } | ||
| } | ||
| } | ||
| </style> | ||
| </style> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { describe, test, expect } from 'vitest'; | ||
| import SegmentedControl from '../components/SegmentedControl.vue'; | ||
| import { mount } from '@vue/test-utils'; | ||
| import { nextTick } from 'vue'; | ||
|
|
||
| describe('SegmentedControl v-model', () => { | ||
| test('should respect initial modelValue', async () => { | ||
| const wrapper = mount(SegmentedControl, { | ||
| props: { | ||
| segments: ['Segment 1', 'Segment 2'], | ||
| modelValue: 'Segment 2', | ||
| }, | ||
| }); | ||
|
|
||
| const activeButtons = wrapper.findAll('.segment-control__button--active'); | ||
| expect(activeButtons.length).toBe(1); | ||
| expect(activeButtons[0].text()).toBe('Segment 2'); | ||
| }); | ||
|
|
||
| test('should update modelValue when a segment is clicked', async () => { | ||
| const wrapper = mount(SegmentedControl, { | ||
| props: { | ||
| segments: ['Segment 1', 'Segment 2'], | ||
| modelValue: 'Segment 1', | ||
| 'onUpdate:modelValue': (e) => wrapper.setProps({ modelValue: e }), | ||
| }, | ||
| }); | ||
|
|
||
| const buttons = wrapper.findAll('.segment-control__button'); | ||
| await buttons[1].trigger('click'); | ||
|
|
||
| expect(wrapper.emitted('update:modelValue')).toBeTruthy(); | ||
| expect(wrapper.emitted('update:modelValue')![0]).toEqual(['Segment 2']); | ||
| }); | ||
|
|
||
| test('should default to the first segment if no modelValue is provided', async () => { | ||
| const wrapper = mount(SegmentedControl, { | ||
| props: { | ||
| segments: ['Segment 1', 'Segment 2'], | ||
| }, | ||
| }); | ||
|
|
||
| await nextTick(); | ||
|
|
||
| const activeButtons = wrapper.findAll('.segment-control__button--active'); | ||
| expect(activeButtons.length).toBe(1); | ||
| expect(activeButtons[0].text()).toBe('Segment 1'); | ||
| }); | ||
|
|
||
| test('should still emit click event when a segment is clicked', async () => { | ||
| const wrapper = mount(SegmentedControl, { | ||
| props: { | ||
| segments: ['Segment 1', 'Segment 2'], | ||
| modelValue: 'Segment 1', | ||
| }, | ||
| }); | ||
|
|
||
| const buttons = wrapper.findAll('.segment-control__button'); | ||
| await buttons[1].trigger('click'); | ||
|
|
||
| expect(wrapper.emitted('click')).toBeTruthy(); | ||
| expect(wrapper.emitted('click')![0]).toEqual(['Segment 2', 1]); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.