This repository was archived by the owner on Oct 23, 2023. It is now read-only.
-
Couldn't load subscription status.
- Fork 3
fix(vue): handle vmodel directive #110
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@polymorphic-factory/vue': minor | ||
| --- | ||
|
|
||
| Polymorphic Vue components now support the v-model directive. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| declare module '*.vue' { | ||
| import Vue from 'vue' | ||
| export default Vue | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { computed } from 'vue' | ||
|
|
||
| const formElements = ['input', 'select', 'textarea', 'fieldset', 'datalist', 'option', 'optgroup'] | ||
|
|
||
| export const useVModel = ( | ||
| elementTag: string, | ||
| modelValue: any, | ||
| emit: CallableFunction, | ||
| attrs: any, | ||
| ) => { | ||
| let vmodelAttrs = {} | ||
| const handleMultipleCheckbox = (value: any) => { | ||
| const currentModelValue = [...modelValue] | ||
| if (currentModelValue.includes(value)) { | ||
| currentModelValue.splice(currentModelValue.indexOf(value), 1) | ||
| return currentModelValue | ||
| } else { | ||
| return [...currentModelValue, value] | ||
| } | ||
| } | ||
| const handleInput = (e: Event) => { | ||
| emit( | ||
| 'update:modelValue', | ||
| attrs.type === 'checkbox' && Array.isArray(modelValue) | ||
| ? handleMultipleCheckbox((e?.currentTarget as HTMLInputElement)?.value) | ||
| : typeof modelValue === 'boolean' | ||
| ? (e?.currentTarget as HTMLInputElement)?.checked | ||
| : (e?.currentTarget as HTMLInputElement)?.value, | ||
| ) | ||
| emit('input', e, (e?.currentTarget as HTMLInputElement)?.value) | ||
| emit( | ||
| 'change', | ||
| e, | ||
| typeof modelValue === 'boolean' | ||
| ? (e?.currentTarget as HTMLInputElement)?.checked | ||
| : (e?.currentTarget as HTMLInputElement)?.value, | ||
| ) | ||
| } | ||
|
|
||
| if (formElements.includes(elementTag)) { | ||
| let val: Record<string, unknown> = { value: modelValue } | ||
| if (elementTag === 'input' && (attrs.type === 'checkbox' || attrs.type === 'radio')) { | ||
| const isChecked = computed(() => | ||
| typeof modelValue === 'boolean' ? modelValue : modelValue.includes(attrs.value), | ||
| ) | ||
| val = { | ||
| checked: isChecked.value, | ||
| } | ||
| } | ||
| vmodelAttrs = { | ||
| ...val, | ||
| onInput: handleInput, | ||
| } | ||
| } | ||
|
|
||
| return vmodelAttrs | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <script setup lang="ts"> | ||
| import { polymorphicFactory } from '../src' | ||
| import { ref } from 'vue' | ||
|
|
||
| const poly = polymorphicFactory() | ||
| const message = ref('test') | ||
| const selectValue = ref('3') | ||
| const radioValue = ref('vue') | ||
| const checkedNames = ref([]) | ||
| const singleCheckbox = ref(true) | ||
| </script> | ||
| <template> | ||
| <poly.input v-model="message" data-testid="poly-input" /> | ||
| <poly.div data-testid="poly-div">{{ message }}</poly.div> | ||
| <poly.select v-model="selectValue" data-testid="poly-select"> | ||
| <poly.option value="1">Value 1</poly.option> | ||
| <poly.option value="2">Value 2</poly.option> | ||
| <poly.option value="3">Value 3</poly.option> | ||
| </poly.select> | ||
| <poly.p>selected : {{ selectValue }}</poly.p> | ||
|
|
||
| <poly.div> | ||
| <poly.input type="radio" v-model="radioValue" value="vue"></poly.input> | ||
| <poly.input type="radio" value="react" v-model="radioValue" data-testid="react-radio"></poly.input> | ||
| <poly.input type="radio" value="angular" v-model="radioValue"></poly.input> | ||
| </poly.div> | ||
| <poly.p>Radio: {{ radioValue }}</poly.p> | ||
|
|
||
| <poly.div> | ||
| <poly.input type="checkbox" id="jack" value="Jack" v-model="checkedNames" data-testid="checkbox-jack"/> | ||
| <poly.label for="jack">Jack</poly.label> | ||
| <poly.input type="checkbox" id="john" value="John" v-model="checkedNames" data-testid="checkbox-john" /> | ||
| <poly.label for="john">John</poly.label> | ||
| <poly.input type="checkbox" id="mike" value="Mike" v-model="checkedNames" /> | ||
| <poly.label for="mike">Mike</poly.label> | ||
| </poly.div> | ||
| <poly.p>checked : {{checkedNames.join(',')}}</poly.p> | ||
| <poly.input type="checkbox" v-model="singleCheckbox" data-testid="single-checkbox"/> | ||
| <poly.p>single checked : {{singleCheckbox}}</poly.p> | ||
| </template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { screen, render, fireEvent } from '@testing-library/vue' | ||
| import PolyTest from './poly-test.vue' | ||
|
|
||
| describe('SFC related features', () => { | ||
| it('has default value with v-model', () => { | ||
| render(PolyTest) | ||
|
|
||
| const inputElement: HTMLInputElement = screen.getByTestId('poly-input') | ||
| const divElement: any = screen.getByTestId('poly-div') | ||
|
|
||
| expect(inputElement.value).toBe('test') | ||
| // Div element should not have "value" bound to them | ||
| expect(divElement.value).toBeUndefined() | ||
| expect(divElement.modelvalue).toBeUndefined() | ||
| }) | ||
|
|
||
| it('updates input with vmodel correctly', async () => { | ||
| render(PolyTest) | ||
|
|
||
| const inputElement: HTMLInputElement = screen.getByTestId('poly-input') | ||
|
|
||
| await fireEvent.update(inputElement, 'Done') | ||
|
|
||
| expect(inputElement.value).toBe('Done') | ||
| }) | ||
|
|
||
| it('updates select correctly', async () => { | ||
| render(PolyTest) | ||
|
|
||
| const select = screen.getByTestId('poly-select') | ||
|
|
||
| await fireEvent.change(select, { target: { value: 2 } }) | ||
|
|
||
| const options: HTMLOptionElement[] = screen.getAllByRole("option") | ||
|
|
||
| expect(options[0].selected).toBeFalsy() | ||
| expect(options[1].selected).toBeTruthy() | ||
| expect(options[2].selected).toBeFalsy() | ||
| }) | ||
|
|
||
| it('updates radio correctly', async () => { | ||
| render(PolyTest) | ||
|
|
||
| const radio = screen.getByTestId('react-radio') | ||
|
|
||
| await fireEvent.click(radio) | ||
|
|
||
| screen.getByText("Radio: react") | ||
| }) | ||
|
|
||
| it("updates multiple checkbox selection", async () => { | ||
| render(PolyTest) | ||
|
|
||
| const john = screen.getByTestId("checkbox-john") | ||
| const jack = screen.getByTestId("checkbox-jack") | ||
|
|
||
| await fireEvent.click(john) | ||
|
|
||
| screen.getByText("checked : John") | ||
|
|
||
| await fireEvent.click(jack) | ||
|
|
||
| screen.getByText("checked : John,Jack") | ||
| }) | ||
|
|
||
| it("updates single checkbox with boolean", async () => { | ||
| render(PolyTest) | ||
|
|
||
| const singleCheckbox = screen.getByTestId("single-checkbox") | ||
|
|
||
| await fireEvent.click(singleCheckbox) | ||
|
|
||
| screen.getByText("single checked : false") | ||
|
|
||
| await fireEvent.click(singleCheckbox) | ||
|
|
||
| screen.getByText("single checked : true") | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
praise (non-blocking): love the test suite!