Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Update VCheckbox component and its story (#1392)

Olga Bulatb8b2a709f3ec
diff --git a/src/assets/icons/checkmark.svg b/src/assets/icons/checkmark.svg
index 3a009af11..6dd54e1a2 100644
--- a/src/assets/icons/checkmark.svg
+++ b/src/assets/icons/checkmark.svg
@@ -1,5 +1,5 @@
-<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
+<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
<g id="icon">
- <path fill-rule="evenodd" clip-rule="evenodd" d="M17.9293 7.97825L10.731 17.6591L6.2147 14.3009L7.10972 13.0972L10.4223 15.5603L16.7255 7.08322L17.9293 7.97825Z" fill="currentColor"/>
+ <path d="M15.779 5 8.36 14.975l-4.3-3.198" fill="none" stroke="currentColor"/>
</g>
</svg>
diff --git a/src/components/VCheckbox/VCheckbox.vue b/src/components/VCheckbox/VCheckbox.vue
index ffe99d141..71af2ed9b 100644
--- a/src/components/VCheckbox/VCheckbox.vue
+++ b/src/components/VCheckbox/VCheckbox.vue
@@ -7,26 +7,43 @@
v-bind="inputAttrs"
@change="onChange"
/>
- <Checkmark
+ <VIcon
v-show="localCheckedState"
- class="checkmark"
- focusable="false"
- width="20"
- height="20"
- aria-hidden="true"
+ class="absolute start-0 text-white"
+ :icon-path="checkmark"
+ view-box="0 0 20 20"
+ :size="5"
/>
<!-- @slot The checkbox label --><slot />
</label>
</template>
-<script>
+<script lang="ts">
import { computed, defineComponent, ref, watch } from '@nuxtjs/composition-api'
-import Checkmark from '~/assets/icons/checkmark.svg?inline'
+import { defineEvent } from '~/types/emits'
-const VCheckbox = defineComponent({
+import VIcon from '~/components/VIcon/VIcon.vue'
+
+import checkmark from '~/assets/icons/checkmark.svg'
+
+type CheckboxAttrs = {
+ name: string
+ value: string
+ disabled?: boolean
+ checked?: boolean
+}
+
+/**
+ * A checkbox input component that allows selection of multiple options from a list .
+ *
+ * Unlike the native checkbox, this component only has two states: checked / not checked.
+ */
+export default defineComponent({
name: 'VCheckbox',
- components: { Checkmark },
+ components: {
+ VIcon,
+ },
props: {
/**
* Checkbox `id` is used for the input id property, connecting the label to
@@ -47,12 +64,14 @@ const VCheckbox = defineComponent({
default: false,
},
/**
- * The name and value parameters are used when sending the form data to the server
- * using HTML on form submit: if a checkbox is checked, `name=value` pair in
- * the POST request body.
- * In a Vue app, this is used as the name parameter in the emitted event's payload.
- * It is usually the category that this checkbox belongs to, eg. licenseType for a
- * 'by-nc-nd' checkbox.
+ * Usually this is the category that this checkbox belongs to, e.g. 'license' for a
+ * 'by-nc-nd' checkbox, or 'licenseType' for 'commercial' checkbox.
+ * This parameter is used in the emitted object.
+ *
+ * If the form submission is done natively, the name and value parameters are used
+ * when sending the form data to the server on form submit: if a checkbox is checked,
+ * `name=value` pair is added to the POST request body.
+ *
* If not set, the value of `id` prop is used instead.
*/
name: {
@@ -77,21 +96,24 @@ const VCheckbox = defineComponent({
default: false,
},
},
+ emits: {
+ change: defineEvent<[Omit<CheckboxAttrs, 'disabled'>]>(),
+ },
setup(props, { emit }) {
const localCheckedState = ref(props.checked || false)
const labelClasses = computed(() =>
props.disabled ? 'text-dark-charcoal-70' : 'text-dark-charcoal'
)
- const inputAttrs = computed(() => {
- const attrs = {
+ const inputAttrs = computed<CheckboxAttrs>(() => {
+ const attrs: CheckboxAttrs = {
name: props.name || props.id,
value: props.value || props.id,
}
if (props.disabled) {
- attrs.disabled = 'disabled'
+ attrs.disabled = true
}
if (localCheckedState.value) {
- attrs.checked = 'checked'
+ attrs.checked = true
}
return attrs
})
@@ -114,6 +136,7 @@ const VCheckbox = defineComponent({
})
}
return {
+ checkmark,
localCheckedState,
labelClasses,
inputAttrs,
@@ -121,7 +144,6 @@ const VCheckbox = defineComponent({
}
},
})
-export default VCheckbox
</script>
<style scoped>
.checkbox-label {
@@ -134,7 +156,4 @@ export default VCheckbox
@apply checked:bg-dark-charcoal;
@apply checked:disabled:bg-dark-charcoal-40;
}
-.checkmark {
- @apply absolute start-0 w-5 h-5 text-white;
-}
</style>
diff --git a/src/components/VCheckbox/meta/VCheckbox.stories.js b/src/components/VCheckbox/meta/VCheckbox.stories.js
deleted file mode 100644
index 7cf973d1f..000000000
--- a/src/components/VCheckbox/meta/VCheckbox.stories.js
+++ /dev/null
@@ -1,75 +0,0 @@
-import VCheckbox from '~/components/VCheckbox/VCheckbox.vue'
-import VLicense from '~/components/VLicense/VLicense.vue'
-
-export default {
- title: 'Components/VCheckbox',
- component: VCheckbox,
-}
-
-const SimpleCheckboxStory = (args, { argTypes }) => {
- return {
- template: `<div class="flex flex-col">
- <VCheckbox v-bind="$props" @change="handleChange" class="mb-4">Code is Poetry</VCheckbox>
- <p>Checkbox is {{isChecked ? '' : 'NOT ' }}checked.</p>
- <p>\`@change\` event payload: </p><pre class="ps-0">{{ payload }}</pre>
- </div>`,
- components: { VCheckbox },
- data: () => ({
- isChecked: args.checked,
- payload: null,
- }),
- props: Object.keys(argTypes),
- methods: {
- handleChange(params) {
- this.isChecked = params.checked
- this.payload = params
- },
- },
- }
-}
-
-const DisabledCheckboxStory = (args, { argTypes }) => ({
- template: `<fieldset><legend>Use</legend><VCheckbox v-bind="$props">Use commercially</VCheckbox></fieldset>`,
- components: { VCheckbox },
- props: Object.keys(argTypes),
-})
-
-const LicenseCheckboxStory = (args, { argTypes }) => ({
- template: `<VCheckbox v-bind="$props" @change="onChange">
- <VLicense license="by-nc" class="me-4"/>
- <span >Checked: {{ isChecked }}</span>
- </VCheckbox>`,
- components: { VCheckbox, VLicense },
- data: () => ({
- isChecked: args.checked,
- }),
- props: Object.keys(argTypes),
- methods: {
- onChange(params) {
- window.alert('License checkbox checked! ' + JSON.stringify(params))
- this.isChecked = params.checked
- },
- },
-})
-
-export const Default = SimpleCheckboxStory.bind({})
-Default.args = {
- id: 'simple',
- checked: true,
- name: 'checkboxes',
-}
-
-export const DisabledCheckbox = DisabledCheckboxStory.bind({})
-DisabledCheckbox.args = {
- id: 'disabled',
- checked: false,
- name: 'use',
- disabled: true,
-}
-
-export const LicenseCheckbox = LicenseCheckboxStory.bind({})
-LicenseCheckbox.args = {
- id: 'license',
- checked: false,
- name: 'checkboxes',
-}
diff --git a/src/components/VCheckbox/meta/VCheckbox.stories.mdx b/src/components/VCheckbox/meta/VCheckbox.stories.mdx
new file mode 100644
index 000000000..4732f80bc
--- /dev/null
+++ b/src/components/VCheckbox/meta/VCheckbox.stories.mdx
@@ -0,0 +1,90 @@
+import {
+ ArgsTable,
+ Canvas,
+ Description,
+ Meta,
+ Story,
+} from '@storybook/addon-docs'
+
+import VCheckbox from '~/components/VCheckbox/VCheckbox.vue'
+import VLicense from '~/components/VLicense/VLicense.vue'
+
+<Meta
+ title="Components/VCheckbox"
+ components={VCheckbox}
+ argTypes={{
+ change: {
+ action: 'change',
+ },
+ }}
+/>
+
+export const checkboxArgTypes = {
+ id: { control: { type: 'text' } },
+ checked: { control: { type: 'boolean' } },
+ name: { control: { type: 'text' } },
+ value: { control: { type: 'text' } },
+ disabled: { control: { type: 'boolean' } },
+}
+
+export const Template = (args) => ({
+ template: `<VCheckbox v-bind="args" v-on="args" class="mb-4">Code is Poetry</VCheckbox>`,
+ components: { VCheckbox },
+ setup() {
+ return { args }
+ },
+})
+
+# VCheckbox
+
+<Description of={VCheckbox} />
+
+## Default checkbox
+
+<Canvas>
+ <Story
+ name="Default"
+ args={{
+ id: 'default',
+ name: 'storybook',
+ value: 'codeIsPoetry',
+ }}
+ argTypes={checkboxArgTypes}
+ >
+ {Template.bind({})}
+ </Story>
+</Canvas>
+
+<ArgsTable of={VCheckbox} />
+
+export const LicenseCheckboxTemplate = (args) => ({
+ template: `<fieldset>
+<legend>License</legend>
+<VCheckbox v-bind="args" v-on="args" class="mb-4">
+<VLicense license="by-nc" class="me-4"/>
+</VCheckbox>
+</fieldset>`,
+ components: { VCheckbox, VLicense },
+ setup() {
+ return { args }
+ },
+})
+
+## License Checkbox
+
+The checkbox label can be a string or another component. Here, we use a VLicense component, and set `checked` prop to `true` to set the checkbox as checked on render.
+
+<Canvas>
+ <Story
+ name="License Checkbox"
+ args={{
+ id: 'cc-by',
+ name: 'license',
+ value: 'cc-by',
+ checked: true,
+ }}
+ argTypes={checkboxArgTypes}
+ >
+ {LicenseCheckboxTemplate.bind({})}
+ </Story>
+</Canvas>
diff --git a/test/storybook/visual-regression/v-checkbox.spec.ts b/test/storybook/visual-regression/v-checkbox.spec.ts
index d17a1db36..469d36449 100644
--- a/test/storybook/visual-regression/v-checkbox.spec.ts
+++ b/test/storybook/visual-regression/v-checkbox.spec.ts
@@ -3,7 +3,7 @@ import { test, expect } from '@playwright/test'
test.describe('v-checkbox', () => {
test.describe('default', () => {
test.beforeEach(async ({ page }) => {
- await page.goto('/iframe.html?id=components-vcheckbox--default')
+ await page.goto('/iframe.html?id=components-vcheckbox--default-story')
})
test('default', async ({ page }) => {
diff --git a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/default-linux.png b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/default-linux.png
index 229dfc442..bdac1ad28 100644
Binary files a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/default-linux.png and b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/default-linux.png differ
diff --git a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/off-linux.png b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/off-linux.png
index 63cba1568..1faa5b7a7 100644
Binary files a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/off-linux.png and b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/off-linux.png differ
diff --git a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-linux.png b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-linux.png
index 09acaa046..15e1f83a6 100644
Binary files a/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-linux.png and b/test/storybook/visual-regression/v-checkbox.spec.ts-snapshots/on-linux.png differ
diff --git a/tsconfig.json b/tsconfig.json
index a0d89d1c6..063681c0f 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -51,6 +51,7 @@
"src/components/VSourcesTable.vue",
"src/components/TableSortIcon.vue",
"src/components/VIcon/VIcon.vue",
+ "src/components/VCheckbox/VCheckbox.vue",
"src/components/VIconButton/VIconButton.vue",
"src/components/VLicense/VLicense.vue",
"src/components/VContentReport/VReportDescForm.vue",