-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(form): add toggle, radio's documentation and testcase
- Loading branch information
Showing
14 changed files
with
456 additions
and
87 deletions.
There are no files selected for viewing
This file contains 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 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,10 @@ | ||
{ | ||
"recommendations": [ | ||
"darkriszty.markdown-table-prettify", | ||
"vue.volar", | ||
"cpylua.language-postcss", | ||
"bradlc.vscode-tailwindcss", | ||
"editorconfig.editorconfig", | ||
"lokalise.i18n-ally" | ||
] | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,26 +1,24 @@ | ||
import { syncRef } from "@vueuse/shared" | ||
import { computed, getCurrentInstance, ref, Ref } from "vue-demi" | ||
import { computed, getCurrentInstance, ref, Ref, watch } from "vue-demi" | ||
|
||
export interface InputProps<V = string> { | ||
modelValue: V, | ||
readonly?: boolean, | ||
disabled?: boolean, | ||
} | ||
|
||
export function useVModel<V>(props: InputProps<V>): Ref<V> { | ||
const localValue = ref(props?.modelValue) as Ref<V> | ||
const localValue = ref(props.modelValue) as Ref<V> | ||
const { emit } = getCurrentInstance() | ||
const model = computed<V>({ | ||
|
||
const model = computed<V>({ | ||
get () { | ||
return localValue.value | ||
return props.modelValue | ||
}, | ||
set (newValue) { | ||
if (!props.readonly && !props.disabled) | ||
emit('update:modelValue', newValue) | ||
set (value) { | ||
emit('update:modelValue', value) | ||
}, | ||
}) | ||
|
||
syncRef(localValue, model) | ||
|
||
return model | ||
return localValue | ||
} |
This file contains 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 |
---|---|---|
|
@@ -100,3 +100,6 @@ function onClick () { | |
</tr> | ||
</tbody> | ||
</table> | ||
|
||
## See Also | ||
- [Spinner](/spinner/component) |
This file contains 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,125 @@ | ||
import { fireEvent, render } from "@testing-library/vue" | ||
import { vi } from "vitest" | ||
import { ref } from "vue-demi" | ||
import Radio from "./Radio.vue" | ||
|
||
it('should render properly without any prop', () => { | ||
const screen = render({ | ||
components: { Radio }, | ||
template : ` | ||
<Radio /> | ||
`, | ||
}) | ||
|
||
const radio = screen.getByTestId('radio') | ||
|
||
expect(radio).toBeInTheDocument() | ||
expect(radio).toHaveClass('radio', 'radio--radio') | ||
}) | ||
|
||
it('should checked at start if `checked` props is provided', () => { | ||
const screen = render({ | ||
components: { Radio }, | ||
template : ` | ||
<Radio checked /> | ||
`, | ||
}) | ||
|
||
const radio = screen.getByTestId('radio') | ||
|
||
expect(radio).toBeInTheDocument() | ||
expect(radio).toHaveClass('radio--checked') | ||
}) | ||
|
||
it('should have readonly style if `readonly` props is provided', () => { | ||
const screen = render({ | ||
components: { Radio }, | ||
template : ` | ||
<Radio readonly /> | ||
`, | ||
}) | ||
|
||
const radio = screen.getByTestId('radio') | ||
|
||
expect(radio).toBeInTheDocument() | ||
expect(radio).toHaveClass('radio--readonly') | ||
}) | ||
|
||
it('should have disabled style if `disabled` props is provided', () => { | ||
const screen = render({ | ||
components: { Radio }, | ||
template : ` | ||
<Radio disabled /> | ||
`, | ||
}) | ||
|
||
const radio = screen.getByTestId('radio') | ||
|
||
expect(radio).toBeInTheDocument() | ||
expect(radio).toHaveClass('radio--disabled') | ||
}) | ||
|
||
it('should have style checkbox if `apperance` set to "checkbox"', () => { | ||
const screen = render({ | ||
components: { Radio }, | ||
template : ` | ||
<Radio apperance="checkbox" /> | ||
`, | ||
}) | ||
|
||
const radio = screen.getByTestId('radio') | ||
|
||
expect(radio).toBeInTheDocument() | ||
expect(radio).toHaveClass('radio--checkbox') | ||
expect(radio).not.toHaveClass('radio--radio') | ||
}) | ||
|
||
it('should modify state in v-model', async () => { | ||
const model = ref() | ||
const screen = render({ | ||
components: { Radio }, | ||
template : ` | ||
<Radio v-model="model" value="apple">Apple</Radio> | ||
<Radio v-model="model" value="grape">Grape</Radio> | ||
`, | ||
setup () { | ||
return { | ||
model, | ||
} | ||
} | ||
}) | ||
|
||
const radioApple = screen.queryByText('Apple') | ||
const radioGrape = screen.queryByText('Grape') | ||
|
||
await fireEvent.click(radioApple) | ||
|
||
expect(model.value).toBe('apple') | ||
|
||
await fireEvent.click(radioGrape) | ||
|
||
expect(model.value).toBe('grape') | ||
}) | ||
|
||
it('should trigger event "change" when clicked', async () => { | ||
const onChange = vi.fn() | ||
const screen = render({ | ||
components: { Radio }, | ||
template : ` | ||
<Radio @change="onChange"> | ||
Apple | ||
</Radio> | ||
`, | ||
setup () { | ||
return { | ||
onChange, | ||
} | ||
} | ||
}) | ||
|
||
const radioApple = screen.queryByText('Apple') | ||
|
||
await fireEvent.click(radioApple) | ||
|
||
expect(onChange).toBeCalledWith(true) | ||
}) |
This file contains 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
Oops, something went wrong.