vueless 0.0.556 → 0.0.558

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,43 +1,4 @@
1
- <template>
2
- <ULabel
3
- :for="elementId"
4
- :size="size"
5
- :label="label"
6
- :description="description"
7
- :align="labelAlign"
8
- :disabled="disabled"
9
- :data-test="dataTest"
10
- v-bind="switchLabelAttrs"
11
- @click="onClickToggle"
12
- >
13
- <label :for="elementId" v-bind="wrapperAttrs">
14
- <input
15
- :id="elementId"
16
- v-model="checkedValue"
17
- type="checkbox"
18
- :disabled="disabled"
19
- v-bind="inputAttrs"
20
- @click="onClickToggle"
21
- @keydown.space="onKeydownSpace"
22
- />
23
-
24
- <span v-bind="circleAttrs">
25
- <UIcon
26
- v-if="toggleIcon"
27
- internal
28
- :name="checkedValue ? config.defaults.onIcon : config.defaults.offIcon"
29
- :color="iconColor"
30
- :size="iconSize"
31
- v-bind="toggleIconAttrs"
32
- />
33
- </span>
34
-
35
- <span v-if="toggleLabel" v-bind="toggleLabelAttrs" v-text="switchLabel" />
36
- </label>
37
- </ULabel>
38
- </template>
39
-
40
- <script setup>
1
+ <script setup lang="ts">
41
2
  import { computed, useId } from "vue";
42
3
  import { merge } from "lodash-es";
43
4
 
@@ -45,111 +6,25 @@ import UIcon from "../ui.image-icon/UIcon.vue";
45
6
  import ULabel from "../ui.form-label/ULabel.vue";
46
7
  import { getDefault } from "../utils/ui.ts";
47
8
 
48
- import { USwitch } from "./constants.js";
49
- import defaultConfig from "./config.js";
50
- import useAttrs from "./useAttrs.js";
9
+ import { USwitch } from "./constants.ts";
10
+ import defaultConfig from "./config.ts";
11
+ import useAttrs from "./useAttrs.ts";
51
12
  import { useLocale } from "../composables/useLocale.ts";
52
13
 
53
- defineOptions({ inheritAttrs: false });
54
-
55
- const props = defineProps({
56
- /**
57
- * Switch value.
58
- */
59
- modelValue: {
60
- type: Boolean,
61
- default: false,
62
- },
63
-
64
- /**
65
- * Label alignment.
66
- * @values top, left, right
67
- */
68
- labelAlign: {
69
- type: String,
70
- default: getDefault(defaultConfig, USwitch).labelAlign,
71
- },
72
-
73
- /**
74
- * Switch label.
75
- */
76
- label: {
77
- type: String,
78
- default: "",
79
- },
80
-
81
- /**
82
- * Switch description.
83
- */
84
- description: {
85
- type: String,
86
- default: "",
87
- },
88
-
89
- /**
90
- * Switch size.
91
- * @values sm, md, lg
92
- */
93
- size: {
94
- type: String,
95
- default: getDefault(defaultConfig, USwitch).size,
96
- },
97
-
98
- /**
99
- * Switch color.
100
- * @values brand, grayscale, gray, red, orange, amber, yellow, lime, green, emerald, teal, cyan, sky, blue, indigo, violet, purple, fuchsia, pink, rose, white */
101
- color: {
102
- type: String,
103
- default: getDefault(defaultConfig, USwitch).color,
104
- },
14
+ import type { USwitchProps, IconSize } from "./types.ts";
105
15
 
106
- /**
107
- * Show toggle icons inside the circle.
108
- */
109
- toggleIcon: {
110
- type: Boolean,
111
- default: getDefault(defaultConfig, USwitch).toggleIcon,
112
- },
113
-
114
- /**
115
- * Show toggle labels (on / off).
116
- */
117
- toggleLabel: {
118
- type: Boolean,
119
- default: getDefault(defaultConfig, USwitch).toggleLabel,
120
- },
121
-
122
- /**
123
- * Set switch disabled.
124
- */
125
- disabled: {
126
- type: Boolean,
127
- default: getDefault(defaultConfig, USwitch).disabled,
128
- },
129
-
130
- /**
131
- * Unique element id.
132
- */
133
- id: {
134
- type: String,
135
- default: "",
136
- },
137
-
138
- /**
139
- * Component config object.
140
- */
141
- config: {
142
- type: Object,
143
- default: () => ({}),
144
- },
16
+ defineOptions({ inheritAttrs: false });
145
17
 
146
- /**
147
- * Data-test attribute for automated testing.
148
- */
149
- dataTest: {
150
- type: String,
151
- default: "",
152
- },
18
+ const props = withDefaults(defineProps<USwitchProps>(), {
19
+ labelAlign: getDefault<USwitchProps>(defaultConfig, USwitch).labelAlign,
20
+ size: getDefault<USwitchProps>(defaultConfig, USwitch).size,
21
+ color: getDefault<USwitchProps>(defaultConfig, USwitch).color,
22
+ toggleIcon: getDefault<USwitchProps>(defaultConfig, USwitch).toggleIcon,
23
+ toggleLabel: getDefault<USwitchProps>(defaultConfig, USwitch).toggleLabel,
24
+ disabled: getDefault<USwitchProps>(defaultConfig, USwitch).disabled,
25
+ modelValue: false,
26
+ dataTest: "",
27
+ config: () => ({}),
153
28
  });
154
29
 
155
30
  const emit = defineEmits([
@@ -163,7 +38,7 @@ const emit = defineEmits([
163
38
  const { tm } = useLocale();
164
39
 
165
40
  const i18nGlobal = tm(USwitch);
166
- const currentLocale = computed(() => merge(defaultConfig.i18n, i18nGlobal, props.config.i18n));
41
+ const currentLocale = computed(() => merge(defaultConfig.i18n, i18nGlobal, props.config?.i18n));
167
42
 
168
43
  const checkedValue = computed({
169
44
  get: () => props.modelValue,
@@ -193,7 +68,7 @@ const iconSize = computed(() => {
193
68
  lg: "sm",
194
69
  };
195
70
 
196
- return sizes[props.size];
71
+ return sizes[props.size] as IconSize;
197
72
  });
198
73
 
199
74
  const iconColor = computed(() => {
@@ -214,3 +89,42 @@ function onKeydownSpace() {
214
89
  toggle();
215
90
  }
216
91
  </script>
92
+
93
+ <template>
94
+ <ULabel
95
+ :for="elementId"
96
+ :size="size"
97
+ :label="label"
98
+ :description="description"
99
+ :align="labelAlign"
100
+ :disabled="disabled"
101
+ :data-test="dataTest"
102
+ v-bind="switchLabelAttrs"
103
+ @click="onClickToggle"
104
+ >
105
+ <label :for="elementId" v-bind="wrapperAttrs">
106
+ <input
107
+ :id="elementId"
108
+ v-model="checkedValue"
109
+ type="checkbox"
110
+ :disabled="disabled"
111
+ v-bind="inputAttrs"
112
+ @click="onClickToggle"
113
+ @keydown.space="onKeydownSpace"
114
+ />
115
+
116
+ <span v-bind="circleAttrs">
117
+ <UIcon
118
+ v-if="toggleIcon"
119
+ internal
120
+ :name="checkedValue ? config.defaults?.onIcon : config.defaults?.offIcon"
121
+ :color="iconColor"
122
+ :size="iconSize"
123
+ v-bind="toggleIconAttrs"
124
+ />
125
+ </span>
126
+
127
+ <span v-if="toggleLabel" v-bind="toggleLabelAttrs" v-text="switchLabel" />
128
+ </label>
129
+ </ULabel>
130
+ </template>
@@ -1,8 +1,8 @@
1
1
  import { Meta, Title, Subtitle, Description, Primary, Controls, Stories, Source } from "@storybook/blocks";
2
2
  import { getSource } from "../../utils/storybook.ts";
3
3
 
4
- import * as stories from "./stories.js";
5
- import defaultConfig from "../config.js?raw"
4
+ import * as stories from "./stories.ts";
5
+ import defaultConfig from "../config.ts?raw"
6
6
 
7
7
  <Meta of={stories} />
8
8
  <Title of={stories} />
@@ -4,6 +4,13 @@ import USwitch from "../../ui.form-switch/USwitch.vue";
4
4
  import UIcon from "../../ui.image-icon/UIcon.vue";
5
5
  import URow from "../../ui.container-row/URow.vue";
6
6
 
7
+ import type { Meta, StoryFn } from "@storybook/vue3";
8
+ import type { USwitchProps } from "../types.ts";
9
+
10
+ interface USwitchArgs extends USwitchProps {
11
+ slotTemplate?: string;
12
+ enum: "size" | "color";
13
+ }
7
14
  /**
8
15
  * The `USwitch` component. | [View on GitHub](https://github.com/vuelessjs/vueless/tree/main/src/ui.form-switch)
9
16
  */
@@ -17,9 +24,9 @@ export default {
17
24
  argTypes: {
18
25
  ...getArgTypes(USwitch.__name),
19
26
  },
20
- };
27
+ } as Meta;
21
28
 
22
- const DefaultTemplate = (args) => ({
29
+ const DefaultTemplate: StoryFn<USwitchArgs> = (args: USwitchArgs) => ({
23
30
  components: { USwitch, UIcon },
24
31
  setup() {
25
32
  const slots = getSlotNames(USwitch.__name);
@@ -28,17 +35,17 @@ const DefaultTemplate = (args) => ({
28
35
  },
29
36
  template: `
30
37
  <USwitch v-bind="args" v-model="args.modelValue">
31
- ${args.slotTemplate || getSlotsFragment()}
38
+ ${args.slotTemplate || getSlotsFragment("")}
32
39
  </USwitch>
33
40
  `,
34
41
  });
35
42
 
36
- const EnumVariantTemplate = (args, { argTypes }) => ({
43
+ const EnumVariantTemplate: StoryFn<USwitchArgs> = (args: USwitchArgs, { argTypes }) => ({
37
44
  components: { USwitch, URow },
38
45
  setup() {
39
46
  return {
40
47
  args,
41
- options: argTypes[args.enum].options,
48
+ options: argTypes?.[args.enum]?.options,
42
49
  };
43
50
  },
44
51
  template: `
@@ -0,0 +1,88 @@
1
+ import defaultConfig from "./config.ts";
2
+
3
+ export type Config = Partial<typeof defaultConfig>;
4
+
5
+ export type IconSize = "2xs" | "xs" | "sm";
6
+
7
+ export interface USwitchProps {
8
+ /**
9
+ * Switch value.
10
+ */
11
+ modelValue?: boolean;
12
+
13
+ /**
14
+ * Label alignment.
15
+ */
16
+ labelAlign?: "top" | "left" | "right";
17
+
18
+ /**
19
+ * Switch label.
20
+ */
21
+ label?: string;
22
+
23
+ /**
24
+ * Switch description.
25
+ */
26
+ description?: string;
27
+
28
+ /**
29
+ * Switch size.
30
+ */
31
+ size?: "sm" | "md" | "lg";
32
+
33
+ /**
34
+ * Switch color.
35
+ */
36
+ color?:
37
+ | "grayscale"
38
+ | "red"
39
+ | "orange"
40
+ | "amber"
41
+ | "yellow"
42
+ | "lime"
43
+ | "green"
44
+ | "emerald"
45
+ | "teal"
46
+ | "cyan"
47
+ | "sky"
48
+ | "blue"
49
+ | "indigo"
50
+ | "violet"
51
+ | "purple"
52
+ | "fuchsia"
53
+ | "pink"
54
+ | "rose"
55
+ | "gray"
56
+ | "white"
57
+ | "brand";
58
+
59
+ /**
60
+ * Show toggle icons inside the circle.
61
+ */
62
+ toggleIcon?: boolean;
63
+
64
+ /**
65
+ * Show toggle labels (on / off).
66
+ */
67
+ toggleLabel?: boolean;
68
+
69
+ /**
70
+ * Set switch disabled.
71
+ */
72
+ disabled?: boolean;
73
+
74
+ /**
75
+ * Unique element id.
76
+ */
77
+ id?: string;
78
+
79
+ /**
80
+ * Component config object.
81
+ */
82
+ config?: Config;
83
+
84
+ /**
85
+ * Data-test attribute for automated testing.
86
+ */
87
+ dataTest?: string;
88
+ }
@@ -0,0 +1,28 @@
1
+ import { computed } from "vue";
2
+ import useUI from "../composables/useUI.ts";
3
+
4
+ import defaultConfig from "./config.ts";
5
+
6
+ import type { Ref } from "vue";
7
+ import type { UseAttrs } from "../types.ts";
8
+ import type { USwitchProps, Config } from "./types.ts";
9
+
10
+ type ComponentState = {
11
+ checked: Ref<boolean>;
12
+ };
13
+
14
+ export default function useAttrs(
15
+ props: USwitchProps,
16
+ { checked }: ComponentState,
17
+ ): UseAttrs<Config> {
18
+ const { config, getKeysAttrs } = useUI<Config>(defaultConfig, () => props.config);
19
+
20
+ const mutatedProps = computed(() => ({
21
+ checked: Boolean(checked.value),
22
+ }));
23
+
24
+ return {
25
+ config,
26
+ ...getKeysAttrs(mutatedProps),
27
+ };
28
+ }