Skip to content

Commit cc3bf14

Browse files
authored
feat: add lib functions (#23)
1 parent 7eee631 commit cc3bf14

File tree

9 files changed

+148
-20
lines changed

9 files changed

+148
-20
lines changed

package-lock.json

Lines changed: 26 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
"@fortawesome/vue-fontawesome": "^3.0.5",
5555
"@headlessui/vue": "^1.7.16",
5656
"@vueuse/core": "^10.7.0",
57+
"date-fns": "^2.30.0",
58+
"date-fns-tz": "^2.0.0",
5759
"lodash-es": "^4.17.21",
5860
"uuid": "^9.0.1",
5961
"vue": "^3.3.4",

src/components/NeCheckbox.vue

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,26 @@ import { computed } from 'vue'
88
import { v4 as uuidv4 } from 'uuid'
99
1010
const props = defineProps({
11-
modelValue: Boolean,
12-
id: String,
13-
label: String,
14-
disableSelectOnLabel: Boolean,
15-
disabled: Boolean
11+
modelValue: {
12+
type: Boolean,
13+
default: false
14+
},
15+
id: {
16+
type: String,
17+
default: ''
18+
},
19+
label: {
20+
type: String,
21+
default: ''
22+
},
23+
disableSelectOnLabel: {
24+
type: Boolean,
25+
default: false
26+
},
27+
disabled: {
28+
type: Boolean,
29+
default: false
30+
}
1631
})
1732
1833
const emit = defineEmits(['update:modelValue'])
@@ -36,7 +51,7 @@ const model = computed({
3651
v-model="model"
3752
:aria-describedby="componentId + '-description'"
3853
: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 sm:h-4 sm:w-4 dark:border-gray-500 dark:text-primary-500 dark:focus:ring-primary-300 dark:focus:ring-offset-primary-950"
54+
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"
4055
type="checkbox"
4156
v-bind="$attrs"
4257
/>

src/components/NeRadioSelection.vue

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ type RadioOption = {
2323
2424
const props = defineProps({
2525
modelValue: {
26-
type: String
26+
type: String,
27+
default: ''
2728
},
2829
label: {
2930
required: true,
3031
type: String
3132
},
3233
description: {
33-
required: false,
34-
type: String
34+
type: String,
35+
default: ''
3536
},
3637
options: {
3738
required: true,
@@ -126,16 +127,16 @@ function focus() {
126127
<button
127128
v-for="option in options"
128129
:key="option.id"
130+
ref="inputRef"
129131
type="button"
130132
:class="[
131133
`${cardClasses[cardSize]}`,
132134
value == option.id ? 'ring-2 ring-primary-700 dark:ring-primary-500' : ''
133135
]"
134136
class="relative flex w-full items-center overflow-hidden rounded-md border text-gray-700 shadow-sm hover:bg-gray-200/70 focus:outline-none focus:ring-2 focus:ring-primary-700 focus:ring-offset-2 focus:ring-offset-white disabled:cursor-not-allowed disabled:opacity-50 dark:border-gray-500 dark:text-gray-50 dark:hover:bg-gray-600/30 dark:focus:ring-primary-500 dark:focus:ring-offset-primary-950"
135137
role="button"
136-
@click="value = option.id"
137138
:disabled="option.disabled || disabled"
138-
ref="inputRef"
139+
@click="value = option.id"
139140
>
140141
<FontAwesomeIcon
141142
v-if="option.icon"
@@ -169,13 +170,13 @@ function focus() {
169170
<div v-for="option in options" :key="option.id" class="flex items-center">
170171
<input
171172
:id="option.id"
173+
ref="inputRef"
172174
v-model="value"
173175
:checked="value == option.id"
174176
:value="option.id"
175177
class="peer border-gray-300 text-primary-700 focus:outline-none focus:ring-2 focus:ring-primary-500 focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 dark:bg-gray-950 dark:text-primary-500 checked:dark:bg-primary-500 dark:focus:ring-primary-300 focus:dark:ring-primary-200 focus:dark:ring-offset-gray-900"
176178
type="radio"
177179
:disabled="option.disabled || disabled"
178-
ref="inputRef"
179180
/>
180181
<label
181182
:for="option.id"

src/components/NeSideDrawer.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function closeDrawer() {
4949
leave-to="translate-x-full"
5050
>
5151
<DialogPanel
52-
class="3xl:w-[35rem] relative ml-16 flex w-[80vw] flex-1 overflow-y-auto bg-gray-50 text-gray-700 shadow-[0px_20px_40px_0_rgba(0,0,0,0.2)] sm:w-[25rem] lg:w-[30rem] dark:bg-gray-900 dark:text-gray-100 dark:shadow-[0px_20px_40px_0_rgba(0,0,0,0.6)]"
52+
class="3xl:w-[35rem] relative ml-16 flex w-[80vw] flex-1 overflow-y-auto bg-gray-50 text-gray-700 shadow-[0px_20px_40px_0_rgba(0,0,0,0.2)] dark:bg-gray-900 dark:text-gray-100 dark:shadow-[0px_20px_40px_0_rgba(0,0,0,0.6)] sm:w-[25rem] lg:w-[30rem]"
5353
>
5454
<TransitionChild
5555
v-if="!title"

src/lib/storage.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (C) 2024 Nethesis S.r.l.
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
/**
5+
* Save a string or a JSON object to local storage
6+
*
7+
* @param name name of the entry
8+
* @param value value of the entry
9+
*/
10+
export const saveToStorage = (name: string, value: any) => {
11+
if (typeof value !== 'string') {
12+
value = JSON.stringify(value)
13+
}
14+
localStorage.setItem(name, value)
15+
}
16+
17+
/**
18+
* Get a JSON object from local storage
19+
*
20+
* @param name name of the entry
21+
* @returns a JSON object
22+
*/
23+
export const getJsonFromStorage = (name: string) => {
24+
const item = localStorage.getItem(name)
25+
26+
if (item) {
27+
return JSON.parse(item)
28+
}
29+
}
30+
31+
/**
32+
* Get a string from local storage
33+
*
34+
* @param name name of the entry
35+
* @returns the string value of the entry
36+
*/
37+
export const getStringFromStorage = (name: string) => {
38+
return localStorage.getItem(name)
39+
}
40+
41+
export const deleteFromStorage = (name: string) => {
42+
localStorage.removeItem(name)
43+
}
44+
45+
/**
46+
* Save a user preference to local storage entry "preferences-<username>"
47+
*
48+
* @param preferenceName name of the preference
49+
* @param preferenceValue string or JSON object
50+
* @param currentUsername username currently logged in
51+
*/
52+
export const savePreference = (
53+
preferenceName: string,
54+
preferenceValue: any,
55+
currentUsername: string
56+
) => {
57+
const preferences = getJsonFromStorage(`preferences-${currentUsername}`) || {}
58+
preferences[preferenceName] = preferenceValue
59+
saveToStorage(`preferences-${currentUsername}`, preferences)
60+
}
61+
62+
/**
63+
* Get a user preference from the local storage entry "preferences-<username>"
64+
*
65+
* @param preferenceName name of the preference
66+
* @param currentUsername username currently logged in
67+
* @returns a string or a JSON object
68+
*/
69+
export const getPreference = (preferenceName: string, currentUsername: string) => {
70+
const preferences = getJsonFromStorage(`preferences-${currentUsername}`) || {}
71+
return preferences[preferenceName]
72+
}

src/main.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,18 @@ export {
4242
byteFormat1000,
4343
kbpsFormat
4444
} from '@/lib/utils'
45+
export {
46+
formatDateLoc,
47+
formatInTimeZoneLoc,
48+
getDateFnsLocale,
49+
formatDurationLoc,
50+
humanDistanceToNowLoc
51+
} from '@/lib/dateTime'
52+
export {
53+
saveToStorage,
54+
getJsonFromStorage,
55+
getStringFromStorage,
56+
deleteFromStorage,
57+
savePreference,
58+
getPreference
59+
} from '@/lib/storage'

stories/NeProgressBar.stories.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export const WithLabelAndProgress: Story = {
7575
args: { progress: 75, label: 'Task progress', showProgress: true }
7676
}
7777

78-
export const color: Story = {
78+
export const Color: Story = {
7979
render: (args) => ({
8080
components: { NeProgressBar },
8181
setup() {
@@ -86,7 +86,7 @@ export const color: Story = {
8686
args: { progress: 75, color: 'blue' }
8787
}
8888

89-
export const customColor: Story = {
89+
export const CustomColor: Story = {
9090
render: (args) => ({
9191
components: { NeProgressBar },
9292
setup() {
@@ -97,7 +97,7 @@ export const customColor: Story = {
9797
args: { progress: 75, color: 'custom', customColorClasses: 'bg-fuchsia-600 dark:bg-fuchsia-500' }
9898
}
9999

100-
export const indeterminate: Story = {
100+
export const Indeterminate: Story = {
101101
render: (args) => ({
102102
components: { NeProgressBar },
103103
setup() {

stories/NeSideDrawer.stories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { NeSideDrawer } from '../src/main'
77
const meta = {
88
title: 'Visual/NeSideDrawer',
99
args: {
10-
isShown: false,
10+
isShown: true,
1111
title: 'Drawer title',
1212
closeAriaLabel: 'Close side drawer'
1313
}

0 commit comments

Comments
 (0)