-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8159587
commit 20c6bc3
Showing
16 changed files
with
365 additions
and
10 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
packages/chusho/lib/components/CCheckbox/CCheckbox.spec.js
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,105 @@ | ||
import { mount } from '@vue/test-utils'; | ||
|
||
import CCheckbox from './CCheckbox'; | ||
|
||
describe('CCheckbox', () => { | ||
it('renders with config checked class', () => { | ||
const wrapper = mount(CCheckbox, { | ||
global: { | ||
provide: { | ||
$chusho: { | ||
options: { | ||
components: { | ||
checkbox: { | ||
class: ({ checked }) => { | ||
return { checked }; | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
props: { | ||
modelValue: true, | ||
}, | ||
}); | ||
|
||
expect(wrapper.classes()).toEqual(['checked']); | ||
}); | ||
|
||
it('renders with correct attributes by default', () => { | ||
const wrapper = mount(CCheckbox); | ||
|
||
expect(wrapper.html()).toEqual('<input type="checkbox">'); | ||
}); | ||
|
||
it('renders with extra attributes', () => { | ||
const wrapper = mount(CCheckbox, { | ||
attrs: { | ||
id: 'checkbox', | ||
}, | ||
}); | ||
|
||
expect(wrapper.html()).toEqual('<input id="checkbox" type="checkbox">'); | ||
}); | ||
|
||
it('keeps the modelValue synchronized with the checkbox value', async () => { | ||
const wrapper = mount({ | ||
components: { | ||
CCheckbox, | ||
}, | ||
data() { | ||
return { | ||
value: false, | ||
}; | ||
}, | ||
template: '<CCheckbox v-model="value" />', | ||
}); | ||
const checkbox = wrapper.findComponent(CCheckbox); | ||
|
||
expect(checkbox.vm.modelValue).toBe(false); | ||
expect(checkbox.element.checked).toBe(false); | ||
|
||
await checkbox.setValue(true); | ||
expect(checkbox.vm.modelValue).toBe(true); | ||
expect(checkbox.element.checked).toBe(true); | ||
expect(checkbox.emitted('update:modelValue')).toEqual([[true]]); | ||
|
||
await wrapper.setData({ value: false }); | ||
expect(checkbox.vm.modelValue).toBe(false); | ||
expect(checkbox.element.checked).toBe(false); | ||
}); | ||
|
||
it('accepts custom true/false values', async () => { | ||
const wrapper = mount({ | ||
components: { | ||
CCheckbox, | ||
}, | ||
data() { | ||
return { | ||
value: 'off', | ||
}; | ||
}, | ||
template: '<CCheckbox v-model="value" trueValue="on" falseValue="off" />', | ||
}); | ||
const checkbox = wrapper.findComponent(CCheckbox); | ||
|
||
expect(checkbox.vm.modelValue).toBe('off'); | ||
expect(checkbox.element.checked).toBe(false); | ||
|
||
await checkbox.setValue('on'); | ||
expect(checkbox.vm.modelValue).toBe('on'); | ||
expect(checkbox.element.checked).toBe(true); | ||
|
||
await checkbox.setValue('off'); | ||
expect(checkbox.vm.modelValue).toBe('off'); | ||
expect(checkbox.element.checked).toBe(false); | ||
|
||
expect(checkbox.emitted('update:modelValue')).toEqual([['on'], ['off']]); | ||
|
||
await wrapper.setData({ value: 'on' }); | ||
expect(checkbox.vm.modelValue).toBe('on'); | ||
expect(checkbox.element.checked).toBe(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { defineComponent, h, inject, mergeProps } from 'vue'; | ||
import { DollarChusho } from '../../types'; | ||
import { ALL_TYPES, generateConfigClass } from '../../utils/components'; | ||
import componentMixin from '../mixins/componentMixin'; | ||
|
||
export default defineComponent({ | ||
name: 'CCheckbox', | ||
|
||
mixins: [componentMixin], | ||
|
||
inheritAttrs: false, | ||
|
||
props: { | ||
/** | ||
* Bind the Checkbox state with the parent component. | ||
* @type {any} | ||
*/ | ||
modelValue: { | ||
type: ALL_TYPES, | ||
default: null, | ||
}, | ||
/** | ||
* Value set when the checkbox is checked. | ||
* @type {any} | ||
*/ | ||
trueValue: { | ||
type: ALL_TYPES, | ||
default: true, | ||
}, | ||
/** | ||
* Value set when the checkbox is unchecked. | ||
* @type {any} | ||
*/ | ||
falseValue: { | ||
type: ALL_TYPES, | ||
default: false, | ||
}, | ||
}, | ||
|
||
emits: ['update:modelValue'], | ||
|
||
render() { | ||
const checkboxConfig = inject<DollarChusho | null>('$chusho', null)?.options | ||
?.components?.checkbox; | ||
const checked = this.modelValue === this.trueValue; | ||
const attrs: Record<string, unknown> = { | ||
...generateConfigClass(checkboxConfig?.class, { | ||
...this.$props, | ||
checked, | ||
}), | ||
type: 'checkbox', | ||
checked, | ||
onChange: (e: Event) => { | ||
const checked = (e.target as HTMLInputElement).checked; | ||
this.$emit( | ||
'update:modelValue', | ||
checked ? this.trueValue : this.falseValue | ||
); | ||
}, | ||
}; | ||
|
||
return h('input', mergeProps(this.$attrs, attrs), this.$slots); | ||
}, | ||
}); |
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,3 @@ | ||
import CCheckbox from './CCheckbox'; | ||
|
||
export { CCheckbox }; |
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
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
13 changes: 13 additions & 0 deletions
13
packages/chusho/src/components/examples/components/checkbox/Controlled.vue
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,13 @@ | ||
<template> | ||
<CCheckbox v-model="value" /> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
data() { | ||
return { | ||
value: false, | ||
}; | ||
}, | ||
}; | ||
</script> |
16 changes: 16 additions & 0 deletions
16
packages/chusho/src/components/examples/components/checkbox/ControlledWithLabel.vue
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,16 @@ | ||
<template> | ||
<CLabel variant="inline" for="checkbox"> | ||
<CCheckbox id="checkbox" v-model="value" variant="inline" /> | ||
Check me if you can | ||
</CLabel> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
data() { | ||
return { | ||
value: false, | ||
}; | ||
}, | ||
}; | ||
</script> |
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,12 @@ | ||
<template> | ||
<CLabel variant="inline" for="my-checkbox"> | ||
<CCheckbox v-model="value" variant="inline" id="my-checkbox" /> | ||
Check me if you can | ||
</CLabel> | ||
</template> | ||
|
||
<script setup> | ||
import { ref } from 'vue'; | ||
const value = ref(false); | ||
</script> |
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
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,78 @@ | ||
# Checkbox | ||
|
||
Augmented form field for boolean input. | ||
|
||
<Showcase> | ||
<ExampleCheckbox /> | ||
</Showcase> | ||
|
||
## Config | ||
|
||
The options below are to be set in the [global configuration](/guide/config.html) at the following location: | ||
|
||
```js | ||
{ | ||
components: { | ||
checkbox: { ... }, | ||
}, | ||
} | ||
``` | ||
|
||
### class | ||
|
||
Classes applied to the input element, except when the prop `bare` is set to `true`. See [styling components](/guide/styling-components/). | ||
|
||
- **type:** `Array<String | Object> | Object | String | (props: Object) => {}` | ||
- **default:** `null` | ||
|
||
#### Example | ||
|
||
```js | ||
class({ checked }) { | ||
return ['checkbox', { | ||
'checkbox--checked': checked, | ||
}] | ||
} | ||
``` | ||
|
||
## API | ||
|
||
<Docgen :components="['CCheckbox']" /> | ||
|
||
## Examples | ||
|
||
### Controlled | ||
|
||
```vue | ||
<template> | ||
<CCheckbox v-model="value" /> | ||
</template> | ||
<script> | ||
export default { | ||
data() { | ||
return { | ||
value: true, // Checked by default | ||
}; | ||
}, | ||
}; | ||
</script> | ||
``` | ||
|
||
### Controlled with custom values | ||
|
||
```vue | ||
<template> | ||
<CCheckbox v-model="value" trueValue="on" falseValue="off" /> | ||
</template> | ||
<script> | ||
export default { | ||
data() { | ||
return { | ||
value: 'off', // Unchecked by default | ||
}; | ||
}, | ||
}; | ||
</script> | ||
``` |
Oops, something went wrong.