sprintify-ui 0.0.31 → 0.0.33

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.
@@ -0,0 +1,68 @@
1
+ import { Ref } from 'vue';
2
+ import { NormalizedOption, Option } from '@/types/types';
3
+ import { isArray, isObject } from 'lodash';
4
+
5
+ export function useHasOptions(
6
+ modelValue: Ref<Option[] | Option | null | undefined>,
7
+ options: Ref<Option[]>,
8
+ labelKey: Ref<string>,
9
+ valueKey: Ref<string>,
10
+ multiple: Ref<boolean> = ref(false)
11
+ ) {
12
+ const normalizedModelValue = computed(
13
+ (): NormalizedOption[] | NormalizedOption | null => {
14
+ if (multiple.value) {
15
+ if (!isArray(modelValue.value)) {
16
+ return [];
17
+ }
18
+ return modelValue.value.map((option) => {
19
+ return {
20
+ label: option[labelKey.value] as string,
21
+ value: option[valueKey.value] as string | number,
22
+ option: option,
23
+ } as NormalizedOption;
24
+ });
25
+ } else {
26
+ if (!isObject(modelValue.value)) {
27
+ return null;
28
+ }
29
+
30
+ return {
31
+ label: modelValue.value[labelKey.value as never] as string,
32
+ value: modelValue.value[valueKey.value as never] as string | number,
33
+ option: modelValue.value,
34
+ } as NormalizedOption;
35
+ }
36
+ }
37
+ );
38
+
39
+ const normalizedOptions = computed((): NormalizedOption[] => {
40
+ return options.value.map((option) => {
41
+ return {
42
+ label: option[labelKey.value] as string,
43
+ value: option[valueKey.value] as string | number,
44
+ option: option,
45
+ } as NormalizedOption;
46
+ });
47
+ });
48
+
49
+ function isSelected(option: NormalizedOption): boolean {
50
+ if (isArray(normalizedModelValue.value)) {
51
+ return normalizedModelValue.value.some((modelValue) => {
52
+ return modelValue.value === option.value;
53
+ });
54
+ }
55
+
56
+ if (isObject(normalizedModelValue.value)) {
57
+ return normalizedModelValue.value.value == option.value;
58
+ }
59
+
60
+ return false;
61
+ }
62
+
63
+ return {
64
+ normalizedOptions,
65
+ normalizedModelValue,
66
+ isSelected,
67
+ };
68
+ }
@@ -21,16 +21,12 @@ export type OptionValue = string | number;
21
21
 
22
22
  export type Option = Record<string, any>;
23
23
 
24
- export type Selection = Record<string, any> | null | undefined;
25
-
26
24
  export type NormalizedOption = {
27
25
  option: Option;
28
26
  value: OptionValue;
29
27
  label: string;
30
28
  };
31
29
 
32
- export type NormalizedSelection = NormalizedOption | null | undefined;
33
-
34
30
  export type MediaLibraryPayload = {
35
31
  to_remove: string[];
36
32
  to_add: UploadedFile[];