Skip to content

Commit

Permalink
feat(input): add help/descriptor text (#516)
Browse files Browse the repository at this point in the history
## Feature
* feat(input): add help/descriptor text

## Chore
* chore: increment version
  • Loading branch information
stubbo authored Sep 13, 2022
1 parent afc0d29 commit f3edec2
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@karnama/vueish",
"version": "0.18.0",
"version": "0.19.0",
"files": [
"dist",
"types"
Expand Down
10 changes: 10 additions & 0 deletions src/assets/styles/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,13 @@
color: theme('colors.red.500') !important;
}
}

.ui-error-text {
@extend .text-color-error;
@apply text-sm mt-1;
}

.ui-help-text {
@extend .text-color-muted;
@apply text-gray-400 text-sm mt-1;
}
25 changes: 25 additions & 0 deletions src/components/input/Demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,27 @@
type="number"
placeholder="Placeholder text."
label="Placeholder" />

<UIInput v-model="helpProp"
:error="error"
:small="small"
:loading="loading"
name="ui-text15"
class="mt-10"
help="Descriptive message..."
label="Help (prop)" />

<UIInput v-model="helpSlot"
:error="error"
:small="small"
:loading="loading"
name="ui-text15"
class="mt-10"
label="Help (slot)">
<template #help>
<span class="underline ui-help-text">Descriptive message...</span>
</template>
</UIInput>
</div>
</template>

Expand All @@ -200,6 +221,8 @@ export default defineComponent({
const suffixProp = ref('100');
const suffixSlot = ref('Feather-weight');
const placeholder = ref<number|null>(null);
const helpProp = ref('I\'m an input!');
const helpSlot = ref('I\'m an input!');
const large = ref(false);
const small = ref(false);
const error = ref('');
Expand All @@ -224,6 +247,8 @@ export default defineComponent({
suffixProp,
suffixSlot,
placeholder,
helpProp,
helpSlot,
large,
error,
loading,
Expand Down
79 changes: 79 additions & 0 deletions src/components/input/UIInput.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,83 @@ describe('UIInput', () => {
expect(wrapper.lastEventValue()).toBeUndefined();
});
});

it('should display the error text when passed as a slot', () => {
const error = 'error';
const errorContainer = `<span id="error">${error}</span>`;

const wrapper = mount(UIInput, {
props: {
modelValue: '',
name: 'input'
},
slots: {
error: errorContainer
}
});

expect(wrapper.get('#error').text()).toBe(error);
});

it('should display the error text when passed as a prop', () => {
const error = 'error';

const wrapper = mount(UIInput, {
props: {
modelValue: '',
name: 'input',
error
}
});

expect(wrapper.get('.ui-error-text').text()).toBe(error);
});

it('should display the help text when passed as a slot', () => {
const help = 'help';
const helpContainer = `<span id="help">${help}</span>`;

const wrapper = mount(UIInput, {
props: {
modelValue: '',
name: 'input'
},
slots: {
help: helpContainer
}
});

expect(wrapper.get('#help').text()).toBe(help);
});

it('should display the help text when passed as a prop', () => {
const help = 'help';

const wrapper = mount(UIInput, {
props: {
modelValue: '',
name: 'input',
help
}
});

expect(wrapper.get('.ui-help-text').text()).toBe(help);
});

it('should display the only error text when both help and error are passed as prop', () => {
const help = 'help';
const error = 'error';

const wrapper = mount(UIInput, {
props: {
modelValue: '',
name: 'input',
help,
error
}
});

expect(wrapper.find('.ui-help-text').exists()).toBe(false);
expect(wrapper.get('.ui-error-text').text()).toBe(error);
});
});
16 changes: 15 additions & 1 deletion src/components/input/UIInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,16 @@

<UIExpandTransition>
<slot v-if="error || $slots.error" name="error">
<p class="text-color-error text-sm">
<p class="ui-error-text">
{{ error }}
</p>
</slot>

<slot v-else-if="(help || $slots.help)" name="help">
<p class="ui-help-text">
{{ help }}
</p>
</slot>
</UIExpandTransition>
</div>
</template>
Expand Down Expand Up @@ -226,6 +232,14 @@ export default defineComponent({
type: [Number, String]
},
/**
* Help message to show below the input.
*/
help: {
type: String
},
large,
small,
prefix,
Expand Down

0 comments on commit f3edec2

Please sign in to comment.