From aad8d84e13ce31c1b7c1cbb930fb8bd4c0abe13a Mon Sep 17 00:00:00 2001 From: Corentin Thomasset Date: Sun, 7 May 2023 23:31:10 +0200 Subject: ui-lib(new-component): added text input component in the c-lib --- src/ui/c-input-text/c-input-text.demo.vue | 39 ++++++ src/ui/c-input-text/c-input-text.test.ts | 87 +++++++++++++ src/ui/c-input-text/c-input-text.theme.ts | 20 +++ src/ui/c-input-text/c-input-text.vue | 198 ++++++++++++++++++++++++++++++ 4 files changed, 344 insertions(+) create mode 100644 src/ui/c-input-text/c-input-text.demo.vue create mode 100644 src/ui/c-input-text/c-input-text.test.ts create mode 100644 src/ui/c-input-text/c-input-text.theme.ts create mode 100644 src/ui/c-input-text/c-input-text.vue (limited to 'src/ui/c-input-text') diff --git a/src/ui/c-input-text/c-input-text.demo.vue b/src/ui/c-input-text/c-input-text.demo.vue new file mode 100644 index 0000000..2363219 --- /dev/null +++ b/src/ui/c-input-text/c-input-text.demo.vue @@ -0,0 +1,39 @@ + + + diff --git a/src/ui/c-input-text/c-input-text.test.ts b/src/ui/c-input-text/c-input-text.test.ts new file mode 100644 index 0000000..56b5855 --- /dev/null +++ b/src/ui/c-input-text/c-input-text.test.ts @@ -0,0 +1,87 @@ +import { describe, expect, it, beforeEach } from 'vitest'; +import { shallowMount } from '@vue/test-utils'; +import { setActivePinia, createPinia } from 'pinia'; +import _ from 'lodash'; +import CInputText from './c-input-text.vue'; + +describe('CInputText', () => { + beforeEach(() => { + setActivePinia(createPinia()); + }); + + it('Renders a label', () => { + const wrapper = shallowMount(CInputText, { + props: { + label: 'Label', + }, + }); + + expect(wrapper.get('.label').text()).to.equal('Label'); + }); + + it('Renders a placeholder', () => { + const wrapper = shallowMount(CInputText, { + props: { + placeholder: 'Placeholder', + }, + }); + + expect(wrapper.get('.input').attributes('placeholder')).to.equal('Placeholder'); + }); + + it('Renders a value', () => { + const wrapper = shallowMount(CInputText, { + props: { + value: 'Value', + }, + }); + + expect(wrapper.vm.value).to.equal('Value'); + }); + + it('Renders a provided id', () => { + const wrapper = shallowMount(CInputText, { + props: { + id: 'id', + }, + }); + + expect(wrapper.get('.input').attributes('id')).to.equal('id'); + }); + + it('updates value on input', async () => { + const wrapper = shallowMount(CInputText); + + await wrapper.get('input').setValue('Hello'); + + expect(_.get(wrapper.emitted(), 'update:value.0.0')).to.equal('Hello'); + }); + + it('cannot be edited when disabled', async () => { + const wrapper = shallowMount(CInputText, { + props: { + disabled: true, + }, + }); + + await wrapper.get('input').setValue('Hello'); + + expect(_.get(wrapper.emitted(), 'update:value')).toBeUndefined(); + }); + + it('renders a feedback message for invalid rules', async () => { + const wrapper = shallowMount(CInputText, { + props: { rules: [{ validator: () => false, message: 'Message' }] }, + }); + + expect(wrapper.get('.feedback').text()).to.equal('Message'); + }); + + it('feedback does not render for valid rules', async () => { + const wrapper = shallowMount(CInputText, { + props: { rules: [{ validator: () => true, message: 'Message' }] }, + }); + + expect(wrapper.find('.feedback').exists()).to.equal(false); + }); +}); diff --git a/src/ui/c-input-text/c-input-text.theme.ts b/src/ui/c-input-text/c-input-text.theme.ts new file mode 100644 index 0000000..93739d4 --- /dev/null +++ b/src/ui/c-input-text/c-input-text.theme.ts @@ -0,0 +1,20 @@ +import { defineThemes } from '../theme/theme.models'; + +export const { useTheme } = defineThemes({ + dark: { + backgroundColor: '#333333', + borderColor: '#333333', + + focus: { + backgroundColor: '#1ea54c1a', + }, + }, + light: { + backgroundColor: '#ffffff', + borderColor: '#e0e0e69e', + + focus: { + backgroundColor: '#ffffff', + }, + }, +}); diff --git a/src/ui/c-input-text/c-input-text.vue b/src/ui/c-input-text/c-input-text.vue new file mode 100644 index 0000000..a40d26a --- /dev/null +++ b/src/ui/c-input-text/c-input-text.vue @@ -0,0 +1,198 @@ + + + + + -- cgit v1.2.3