Skip to content

Commit c1d7672

Browse files
committed
feat: added NeCheckbox
1 parent cc89686 commit c1d7672

File tree

6 files changed

+182
-4
lines changed

6 files changed

+182
-4
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"@fortawesome/free-solid-svg-icons": "^6.5.1",
4949
"@fortawesome/vue-fontawesome": "^3.0.5",
5050
"@headlessui/vue": "^1.7.16",
51+
"uuid": "^9.0.1",
5152
"vue": "^3.3.4",
5253
"vue-tippy": "^6.3.1"
5354
},

src/components/NeCheckbox.vue

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<!--
2+
Copyright (C) 2023 Nethesis S.r.l.
3+
SPDX-License-Identifier: GPL-3.0-or-later
4+
-->
5+
6+
<script lang="ts" setup>
7+
import { computed } from 'vue'
8+
import { v4 as uuidv4 } from 'uuid'
9+
10+
const props = defineProps({
11+
modelValue: Boolean,
12+
id: String,
13+
label: String,
14+
disableSelectOnLabel: Boolean,
15+
disabled: Boolean
16+
})
17+
18+
const emit = defineEmits(['update:modelValue'])
19+
20+
const componentId = computed(() => (props.id ? props.id : uuidv4()))
21+
22+
const model = computed({
23+
get() {
24+
return props.modelValue
25+
},
26+
set(val) {
27+
emit('update:modelValue', val)
28+
}
29+
})
30+
</script>
31+
<template>
32+
<div class="relative flex items-start">
33+
<div class="flex h-6 items-center">
34+
<input
35+
:id="componentId"
36+
v-model="model"
37+
:aria-describedby="componentId + '-description'"
38+
:disabled="disabled"
39+
class="h-5 w-5 rounded border-gray-300 text-primary-700 focus:ring-2 focus:ring-primary-500 focus:ring-offset-2 focus:ring-offset-white disabled:cursor-not-allowed disabled:opacity-50 dark:border-gray-500 dark:text-primary-500 dark:focus:ring-primary-300 dark:focus:ring-offset-primary-950 sm:h-4 sm:w-4"
40+
type="checkbox"
41+
v-bind="$attrs"
42+
/>
43+
</div>
44+
<div class="ml-3 text-sm leading-6">
45+
<!-- show label prop or default slot -->
46+
<label
47+
:class="[
48+
'font-medium text-gray-700 dark:text-gray-50',
49+
{ 'cursor-not-allowed opacity-50': disabled }
50+
]"
51+
:for="disableSelectOnLabel ? '' : componentId"
52+
>
53+
<slot>{{ label }}</slot>
54+
</label>
55+
<span v-if="$slots.tooltip" class="ml-2">
56+
<slot name="tooltip"></slot>
57+
</span>
58+
<div
59+
v-if="$slots.description"
60+
:id="componentId + '-description'"
61+
class="text-gray-500 dark:text-gray-400"
62+
>
63+
<slot name="description" />
64+
</div>
65+
</div>
66+
</div>
67+
</template>

src/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import NeTitle from '@/components/NeTitle.vue'
1111
import NeTooltip from '@/components/NeTooltip.vue'
1212
import NeBadge from '@/components/NeBadge.vue'
1313
import NeButton from '@/components/NeButton.vue'
14+
import NeCheckbox from '@/components/NeCheckbox.vue'
1415
// style import
1516
import '@/main.css'
1617

@@ -26,5 +27,6 @@ export {
2627
NeTitle,
2728
NeTooltip,
2829
NeBadge,
29-
NeButton
30+
NeButton,
31+
NeCheckbox
3032
}

stories/NeCheckbox.stories.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright (C) 2023 Nethesis S.r.l.
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
import type { Meta, StoryObj } from '@storybook/vue3'
5+
import { NeCheckbox, NeTooltip } from '../src/main'
6+
7+
const meta = {
8+
title: 'Control/NeCheckbox',
9+
component: NeCheckbox,
10+
tags: ['autodocs'],
11+
args: {
12+
label: 'Label',
13+
modelValue: true,
14+
disabled: false,
15+
disableSelectOnLabel: false,
16+
id: ''
17+
}
18+
} satisfies Meta<typeof NeCheckbox>
19+
20+
export default meta
21+
type Story = StoryObj<typeof meta>
22+
23+
const template = '<NeCheckbox v-bind="args" />'
24+
25+
export const Default: Story = {
26+
render: (args) => ({
27+
components: { NeCheckbox },
28+
setup() {
29+
return { args }
30+
},
31+
template: template
32+
}),
33+
args: {}
34+
}
35+
36+
const templateWithSlot = '<NeCheckbox v-bind="args">Label using slot instead of prop</NeCheckbox>'
37+
38+
export const WithLabelSlot: Story = {
39+
render: (args) => ({
40+
components: { NeCheckbox },
41+
setup() {
42+
return { args }
43+
},
44+
template: templateWithSlot
45+
}),
46+
args: { label: '' }
47+
}
48+
49+
export const Disabled: Story = {
50+
render: (args) => ({
51+
components: { NeCheckbox },
52+
setup() {
53+
return { args }
54+
},
55+
template: template
56+
}),
57+
args: { disabled: true }
58+
}
59+
60+
const templateWithDescription =
61+
'<NeCheckbox v-bind="args"><span>Label</span><template #description>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua</template></NeCheckbox>'
62+
63+
export const WithDescription: Story = {
64+
render: (args) => ({
65+
components: { NeCheckbox },
66+
setup() {
67+
return { args }
68+
},
69+
template: templateWithDescription
70+
}),
71+
args: {}
72+
}
73+
74+
const templateDisableSelectOnLabel =
75+
'<NeCheckbox v-bind="args">I agree to the <a class="underline cursor-pointer text-primary-700 dark:text-primary-500">Terms and conditions</a></NeCheckbox>'
76+
77+
export const DisableSelectOnLabel: Story = {
78+
render: (args) => ({
79+
components: { NeCheckbox },
80+
setup() {
81+
return { args }
82+
},
83+
template: templateDisableSelectOnLabel
84+
}),
85+
args: { label: '', disableSelectOnLabel: true }
86+
}
87+
88+
const templateWithTooltip =
89+
'<NeCheckbox v-bind="args">\
90+
<template #tooltip>\
91+
<NeTooltip>\
92+
<template #content>Tooltip</template>\
93+
</NeTooltip>\
94+
</template>\
95+
</NeCheckbox>'
96+
97+
export const WithTooltip: Story = {
98+
render: (args) => ({
99+
components: { NeCheckbox, NeTooltip },
100+
setup() {
101+
return { args }
102+
},
103+
template: templateWithTooltip
104+
}),
105+
args: {}
106+
}

vite.config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ export default defineConfig({
2020
'vue-tippy',
2121
'@fortawesome/vue-fontawesome',
2222
'@fortawesome/free-solid-svg-icons',
23-
'@headlessui/vue'
23+
'@headlessui/vue',
24+
'uuid'
2425
],
2526
output: {
2627
globals: {
2728
vue: 'Vue',
2829
'vue-tippy': 'Tippy',
2930
'@fortawesome/vue-fontawesome': 'FontAwesomeIcon',
3031
'@fortawesome/free-solid-svg-icons': 'fa',
31-
'@headlessui/vue': 'HeadlessUI'
32+
'@headlessui/vue': 'HeadlessUI',
33+
uuid: 'v'
3234
}
3335
}
3436
}

0 commit comments

Comments
 (0)