From 9b1197d03a254eefeafea37034d7e4e7e05392c7 Mon Sep 17 00:00:00 2001 From: ClemCornet Date: Wed, 7 Jul 2021 12:12:01 +0200 Subject: [PATCH 1/3] feat(LcRadio): [ch5583] create LcRadio component --- src/components/LcForm.vue | 8 ++ src/components/LcRadio/LcRadio.d.ts | 3 + src/components/LcRadio/LcRadio.stories.ts | 81 +++++++++++++++ src/components/LcRadio/LcRadio.vue | 99 +++++++++++++++++++ src/components/LcRadio/LcRadioGroup.vue | 90 +++++++++++++++++ .../LcRadio/__tests__/LcRadio.spec.ts | 58 +++++++++++ .../LcRadio/__tests__/LcRadioGroup.spec.ts | 74 ++++++++++++++ src/components/LcRadio/index.ts | 2 + src/stories/LcForm.stories.ts | 16 +++ 9 files changed, 431 insertions(+) create mode 100644 src/components/LcRadio/LcRadio.d.ts create mode 100644 src/components/LcRadio/LcRadio.stories.ts create mode 100644 src/components/LcRadio/LcRadio.vue create mode 100644 src/components/LcRadio/LcRadioGroup.vue create mode 100644 src/components/LcRadio/__tests__/LcRadio.spec.ts create mode 100644 src/components/LcRadio/__tests__/LcRadioGroup.spec.ts create mode 100644 src/components/LcRadio/index.ts diff --git a/src/components/LcForm.vue b/src/components/LcForm.vue index 191a7f2..9ec60ac 100644 --- a/src/components/LcForm.vue +++ b/src/components/LcForm.vue @@ -33,6 +33,12 @@ v-model="field.model" v-bind="field.attr" /> + @@ -86,6 +92,7 @@ import LcMultiselect from './LcMultiselect/LcMultiselect.vue' import LcCheckbox from './LcCheckbox.vue' import LcInput from './LcInput.vue' import LcTextarea from './LcTextarea' +import { LcRadioGroup } from './LcRadio' configure({ generateMessage: localize({ @@ -108,6 +115,7 @@ export default defineComponent({ LcInput, LcMultiselect, LcTextarea, + LcRadioGroup, vForm, }, props: { diff --git a/src/components/LcRadio/LcRadio.d.ts b/src/components/LcRadio/LcRadio.d.ts new file mode 100644 index 0000000..dd6e676 --- /dev/null +++ b/src/components/LcRadio/LcRadio.d.ts @@ -0,0 +1,3 @@ +import { DefineComponent } from 'vue' +const component: DefineComponent<{}, {}, any> +export default component diff --git a/src/components/LcRadio/LcRadio.stories.ts b/src/components/LcRadio/LcRadio.stories.ts new file mode 100644 index 0000000..8b1af9c --- /dev/null +++ b/src/components/LcRadio/LcRadio.stories.ts @@ -0,0 +1,81 @@ +import LcRadio from './LcRadio.vue' +import LcRadioGroup from './LcRadioGroup.vue' + +export default { + title: 'Example/LcRadio', + component: LcRadioGroup, + subcomponents: { LcRadio }, +} + +const Template = (args: any) => ({ + components: { LcRadioGroup, LcRadio }, + setup() { + return { args } + }, + template: ` + + + + + `, +}) + +export const Base = Template.bind({}) as any +Base.args = { + modelValue: '', + name: 'civility', + options: [ + { + label: 'Monsieur', + value: 'mr', + }, + { + label: 'Madame', + value: 'ms', + }, + { + label: 'Non binaire', + value: 'unknown', + }, + ], +} + +export const WithLabel = Template.bind({}) as any +WithLabel.args = { + ...Base.args, + label: 'Choisir votre civilité :', +} + +export const Vertical = Template.bind({}) as any +Vertical.args = { + ...Base.args, + vertical: true, +} + +export const VerticalWithLabel = Template.bind({}) as any +VerticalWithLabel.args = { + ...Base.args, + label: 'Choisir votre civilité :', + vertical: true, +} + +export const Disabled = Template.bind({}) as any +Disabled.args = { + ...Base.args, + label: 'Choisir votre civilité :', + options: [ + { + label: 'Monsieur', + value: 'mr', + disabled: true, + }, + { + label: 'Madame', + value: 'ms', + }, + { + label: 'Non binaire', + value: 'unknown', + }, + ], +} diff --git a/src/components/LcRadio/LcRadio.vue b/src/components/LcRadio/LcRadio.vue new file mode 100644 index 0000000..b422c8c --- /dev/null +++ b/src/components/LcRadio/LcRadio.vue @@ -0,0 +1,99 @@ + + + + diff --git a/src/components/LcRadio/LcRadioGroup.vue b/src/components/LcRadio/LcRadioGroup.vue new file mode 100644 index 0000000..1f3972e --- /dev/null +++ b/src/components/LcRadio/LcRadioGroup.vue @@ -0,0 +1,90 @@ + + + + diff --git a/src/components/LcRadio/__tests__/LcRadio.spec.ts b/src/components/LcRadio/__tests__/LcRadio.spec.ts new file mode 100644 index 0000000..b74fb1b --- /dev/null +++ b/src/components/LcRadio/__tests__/LcRadio.spec.ts @@ -0,0 +1,58 @@ +import { mount } from '@vue/test-utils' +import { LcRadio } from '../index' + +let wrapper: any + +beforeEach(() => { + wrapper = mount(LcRadio, { + props: { + name: 'civility', + value: 'mr', + vertical: false, + }, + }) +}) + +afterEach(() => { + wrapper?.unmount() +}) + +describe('LcRadio', () => { + it('is a Vue instance', () => { + expect(wrapper.vm).toBeTruthy() + }) + + describe('input behaviour', () => { + it('should emit the event', () => { + const radioButton = wrapper.find('input[type="radio"]') + radioButton.trigger('change') + + expect(wrapper.emitted()).toHaveProperty('update:modelValue') + }) + + it('should emit the right value', () => { + const radioButton = wrapper.find('input[type="radio"]') + radioButton.trigger('change') + + const changeEvent = wrapper.emitted('update:modelValue') + + expect(changeEvent[0]).toEqual(['mr']) + }) + + it('should disabled the radio button', async() => { + await wrapper.setProps({ disabled: true }) + const radioButton = wrapper.find('input[type="radio"]') + + expect(radioButton.attributes()).toHaveProperty('disabled') + }) + }) + + describe('input style', () => { + it('should set style for vertical layout', async() => { + await wrapper.setProps({ vertical: true }) + const label = wrapper.find('label') + + expect(label.classes()).toContain('mb-2') + }) + }) +}) diff --git a/src/components/LcRadio/__tests__/LcRadioGroup.spec.ts b/src/components/LcRadio/__tests__/LcRadioGroup.spec.ts new file mode 100644 index 0000000..abf9da2 --- /dev/null +++ b/src/components/LcRadio/__tests__/LcRadioGroup.spec.ts @@ -0,0 +1,74 @@ +import { mount } from '@vue/test-utils' +import { LcRadioGroup, LcRadio } from '../index' + +let wrapper: any + +beforeEach(() => { + wrapper = mount(LcRadioGroup, { + props: { + options: [ + { label: 'Monsieur', value: 'mr' }, + { label: 'Madame', value: 'ms' }, + { label: 'Non spécifié', value: 'unspecified' }, + ], + name: 'civility', + modelValue: '', + }, + global: { + components: { LcRadio }, + }, + }) +}) + +afterEach(() => { + wrapper?.unmount() +}) + +describe('LcRadioGroup', () => { + it('is a Vue instance', () => { + expect(wrapper.vm).toBeTruthy() + }) + + describe('input behaviour', () => { + it('should render good number of radio-buttons', () => { + const radiosButtons = wrapper.findAll('input[type="radio"]') + + expect(radiosButtons).toHaveLength(3) + }) + + it('should set good name attribute', () => { + const radiosButtons = wrapper.findAll('input[type="radio"]') + + expect(radiosButtons[1].attributes('value')).toEqual('ms') + }) + + it('should set good value attributes', () => { + const radiosButtons = wrapper.findAll('input[type="radio"]') + + expect(radiosButtons[1].attributes('value')).toEqual('ms') + }) + + it('should emit the event', () => { + const radioButton = wrapper.find('input[type="radio"]') + radioButton.trigger('change') + + expect(wrapper.emitted()).toHaveProperty('update:modelValue') + }) + }) + + describe('input layout', () => { + it('it should render the right label', async() => { + await wrapper.setProps({ label: 'Your civility :' }) + + const label = wrapper.find('label') + expect(label.text()).toEqual('Your civility :') + }) + + it('it should render vertical layout', async() => { + await wrapper.setProps({ vertical: true }) + + const wrapperBtn = wrapper.findAll('div')[1] + expect(wrapperBtn.classes()).toContain('flex-col') + }) + }) +}) diff --git a/src/components/LcRadio/index.ts b/src/components/LcRadio/index.ts new file mode 100644 index 0000000..2a78255 --- /dev/null +++ b/src/components/LcRadio/index.ts @@ -0,0 +1,2 @@ +export { default as LcRadio } from './LcRadio.vue' +export { default as LcRadioGroup } from './LcRadioGroup.vue' diff --git a/src/stories/LcForm.stories.ts b/src/stories/LcForm.stories.ts index 2e0a8e4..ff2b2ce 100644 --- a/src/stories/LcForm.stories.ts +++ b/src/stories/LcForm.stories.ts @@ -72,11 +72,27 @@ Base.args = { model: false, inputType: 'checkbox', attr: { + class: 'mb-4', label: 'Je souhaite recevoir occasionnellement une sélection d’expériences et de maisons.', name: 'newsletter', rules: 'required', }, }, + { + model: '', + inputType: 'radio', + attr: { + label: 'Votre civilité :', + name: 'civility', + rules: 'required', + vertical: false, + }, + options: [ + { label: 'Monsieur', value: 'mr' }, + { label: 'Madame', value: 'ms' }, + { label: 'Non spécifié', value: 'unspecified' }, + ], + }, { model: null, inputType: 'select', From 64955fd2873a5149398539940bf389d100fc3225 Mon Sep 17 00:00:00 2001 From: ClemCornet Date: Mon, 12 Jul 2021 13:28:30 +0200 Subject: [PATCH 2/3] feat(LcRadio): [ch5583] feedbacks: use css class, fix unit test & add storie --- README.md | 1 + src/components/LcRadio/LcRadio.stories.ts | 64 +++------------ src/components/LcRadio/LcRadio.vue | 38 +++++---- .../LcRadio/LcRadioGroup.stories.ts | 78 +++++++++++++++++++ src/components/LcRadio/LcRadioGroup.vue | 20 ++++- .../LcRadio/__tests__/LcRadio.spec.ts | 14 ++-- .../LcRadio/__tests__/LcRadioGroup.spec.ts | 34 ++++---- src/library.ts | 3 + 8 files changed, 155 insertions(+), 97 deletions(-) create mode 100644 src/components/LcRadio/LcRadioGroup.stories.ts diff --git a/README.md b/README.md index 87c83b9..e329c20 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ This repo provides a basic setup for developing component libraries in Vite with - [x] LcInput - [x] LcModal - [x] LcPagination +- [x] LcRadioGroup & LcRadio - [x] LcTable - [x] LcTooltip diff --git a/src/components/LcRadio/LcRadio.stories.ts b/src/components/LcRadio/LcRadio.stories.ts index 8b1af9c..ca789bc 100644 --- a/src/components/LcRadio/LcRadio.stories.ts +++ b/src/components/LcRadio/LcRadio.stories.ts @@ -1,81 +1,37 @@ +// import { action } from '@storybook/addon-actions' + import LcRadio from './LcRadio.vue' -import LcRadioGroup from './LcRadioGroup.vue' export default { title: 'Example/LcRadio', - component: LcRadioGroup, - subcomponents: { LcRadio }, + component: LcRadio, } const Template = (args: any) => ({ - components: { LcRadioGroup, LcRadio }, + components: { LcRadio }, setup() { return { args } }, - template: ` - - - - - `, + template: '', }) export const Base = Template.bind({}) as any Base.args = { modelValue: '', name: 'civility', - options: [ - { - label: 'Monsieur', - value: 'mr', - }, - { - label: 'Madame', - value: 'ms', - }, - { - label: 'Non binaire', - value: 'unknown', - }, - ], + value: 'mr', + vertical: false, } export const WithLabel = Template.bind({}) as any WithLabel.args = { ...Base.args, - label: 'Choisir votre civilité :', -} - -export const Vertical = Template.bind({}) as any -Vertical.args = { - ...Base.args, - vertical: true, -} - -export const VerticalWithLabel = Template.bind({}) as any -VerticalWithLabel.args = { - ...Base.args, - label: 'Choisir votre civilité :', - vertical: true, + label: 'Monsieur', } export const Disabled = Template.bind({}) as any Disabled.args = { ...Base.args, - label: 'Choisir votre civilité :', - options: [ - { - label: 'Monsieur', - value: 'mr', - disabled: true, - }, - { - label: 'Madame', - value: 'ms', - }, - { - label: 'Non binaire', - value: 'unknown', - }, - ], + label: 'Monsieur', + disabled: true, } diff --git a/src/components/LcRadio/LcRadio.vue b/src/components/LcRadio/LcRadio.vue index b422c8c..3187243 100644 --- a/src/components/LcRadio/LcRadio.vue +++ b/src/components/LcRadio/LcRadio.vue @@ -1,10 +1,9 @@