Skip to content

Commit

Permalink
feat(text-input): binds component attributes to native input
Browse files Browse the repository at this point in the history
  • Loading branch information
felipebritor committed Apr 15, 2023
1 parent 94f0972 commit 18b8dbf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/components/CvTextInput/CvTextInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
</label>
<div :class="[`${carbonPrefix}--text-input__field-wrapper`]">
<input
:id="cvId"
:class="[`${carbonPrefix}--text-input`]"
v-bind="$attrs"
type="text"
:id="cvId"
:value="modelValue"
@input="$event => $emit('update:modelValue', $event.target.value)"
/>
Expand All @@ -34,3 +35,9 @@ const props = defineProps({
const cvId = useCvId(props);
const emit = defineEmits(['update:modelValue']);
</script>

<script>
export default {
inheritAttrs: false,
};
</script>
32 changes: 32 additions & 0 deletions src/components/CvTextInput/__tests__/CvTextInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,36 @@ describe('CvTextInput', () => {
const input = getByLabelText(dummyLabel);
expect(input).toBeDefined();
});

describe('Attribute & event binding', () => {
it('binds attributes to native input', async () => {
const dummyName = 'dummy-name';
const { container } = render(CvTextInput, {
attrs: { name: dummyName },
});
const input = container.querySelector('input');
expect(input.getAttribute('name')).toBe(dummyName);
expect(container.firstChild.getAttribute('name')).not.toBe(dummyName);
});

it('does not overwrite native input type if attribute type is set', async () => {
const dummyType = 'number';
const { container } = render(CvTextInput, {
attrs: { type: dummyType },
});

const input = container.querySelector('input');
expect(input.getAttribute('type')).toBe('text');
});

it('does not overwrite native input value if attribute value is set', async () => {
const dummyValue = 'Dummy Value';
const { container } = render(CvTextInput, {
attrs: { value: dummyValue },
});

const input = container.querySelector('input');
expect(input.value).toBe('');
});
});
});

0 comments on commit 18b8dbf

Please sign in to comment.