@aerogel/core 0.0.0-next.f1f5a990033d966dc0bb12d251110fbc9350dcc7 → 0.0.0-next.fd1bd21aea7a9ab8c4eab69a5f5776db5de8bf35

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.
Files changed (52) hide show
  1. package/dist/aerogel-core.cjs.js +1 -1
  2. package/dist/aerogel-core.cjs.js.map +1 -1
  3. package/dist/aerogel-core.d.ts +511 -119
  4. package/dist/aerogel-core.esm.js +1 -1
  5. package/dist/aerogel-core.esm.js.map +1 -1
  6. package/package.json +2 -2
  7. package/src/bootstrap/index.ts +6 -2
  8. package/src/bootstrap/options.ts +3 -0
  9. package/src/components/forms/AGCheckbox.vue +7 -1
  10. package/src/components/forms/AGForm.vue +9 -10
  11. package/src/components/forms/AGInput.vue +10 -6
  12. package/src/components/forms/AGSelect.story.vue +21 -3
  13. package/src/components/forms/AGSelect.vue +10 -3
  14. package/src/components/headless/forms/AGHeadlessButton.vue +17 -12
  15. package/src/components/headless/forms/AGHeadlessInput.ts +7 -3
  16. package/src/components/headless/forms/AGHeadlessInput.vue +3 -3
  17. package/src/components/headless/forms/AGHeadlessInputDescription.vue +28 -0
  18. package/src/components/headless/forms/AGHeadlessInputInput.vue +40 -4
  19. package/src/components/headless/forms/AGHeadlessInputTextArea.vue +40 -0
  20. package/src/components/headless/forms/AGHeadlessSelect.ts +15 -12
  21. package/src/components/headless/forms/AGHeadlessSelect.vue +23 -22
  22. package/src/components/headless/forms/AGHeadlessSelectOption.vue +6 -6
  23. package/src/components/headless/forms/composition.ts +10 -0
  24. package/src/components/headless/forms/index.ts +3 -0
  25. package/src/components/lib/AGErrorMessage.vue +2 -2
  26. package/src/components/lib/AGMarkdown.vue +7 -2
  27. package/src/components/modals/AGPromptModal.ts +30 -0
  28. package/src/components/modals/AGPromptModal.vue +34 -0
  29. package/src/components/modals/index.ts +10 -19
  30. package/src/directives/index.ts +2 -0
  31. package/src/directives/measure.ts +11 -2
  32. package/src/errors/Errors.ts +16 -19
  33. package/src/errors/index.ts +1 -10
  34. package/src/errors/utils.ts +19 -0
  35. package/src/forms/Form.ts +43 -3
  36. package/src/forms/index.ts +1 -0
  37. package/src/forms/utils.ts +15 -0
  38. package/src/jobs/Job.ts +5 -0
  39. package/src/jobs/index.ts +7 -0
  40. package/src/lang/Lang.ts +11 -23
  41. package/src/main.ts +3 -0
  42. package/src/services/App.state.ts +1 -2
  43. package/src/services/App.ts +21 -3
  44. package/src/services/Cache.ts +43 -0
  45. package/src/services/Events.test.ts +39 -0
  46. package/src/services/Events.ts +100 -30
  47. package/src/services/Service.ts +42 -12
  48. package/src/services/index.ts +4 -1
  49. package/src/services/store.ts +8 -5
  50. package/src/testing/index.ts +25 -0
  51. package/src/ui/UI.ts +51 -6
  52. package/src/ui/index.ts +2 -0
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@aerogel/core",
3
3
  "description": "The Lightest Solid",
4
- "version": "0.0.0-next.f1f5a990033d966dc0bb12d251110fbc9350dcc7",
4
+ "version": "0.0.0-next.fd1bd21aea7a9ab8c4eab69a5f5776db5de8bf35",
5
5
  "main": "dist/aerogel-core.cjs.js",
6
6
  "module": "dist/aerogel-core.esm.js",
7
7
  "types": "dist/aerogel-core.d.ts",
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "@headlessui/vue": "^1.7.14",
38
- "@noeldemartin/utils": "0.5.0-next.dce7f1ca99862fde01601bb39d71d0eaaed3abe5",
38
+ "@noeldemartin/utils": "0.5.1-next.49cc6c9b4a20930cbf922a949135981791acc5c3",
39
39
  "dompurify": "^3.0.3",
40
40
  "marked": "^5.0.4",
41
41
  "pinia": "^2.1.6",
@@ -6,6 +6,7 @@ import errors from '@/errors';
6
6
  import Events from '@/services/Events';
7
7
  import lang from '@/lang';
8
8
  import services from '@/services';
9
+ import testing from '@/testing';
9
10
  import ui from '@/ui';
10
11
  import { installPlugins } from '@/plugins';
11
12
  import type { AerogelOptions } from '@/bootstrap/options';
@@ -13,9 +14,11 @@ import type { AerogelOptions } from '@/bootstrap/options';
13
14
  export { AerogelOptions };
14
15
 
15
16
  export async function bootstrapApplication(app: App, options: AerogelOptions = {}): Promise<void> {
16
- const plugins = [directives, errors, lang, services, ui, ...(options.plugins ?? [])];
17
+ const plugins = [testing, directives, errors, lang, services, ui, ...(options.plugins ?? [])];
17
18
 
18
19
  await installPlugins(plugins, app, options);
20
+ await options.install?.(app);
21
+ await Events.emit('application-ready');
19
22
  }
20
23
 
21
24
  export async function bootstrap(rootComponent: Component, options: AerogelOptions = {}): Promise<void> {
@@ -26,11 +29,12 @@ export async function bootstrap(rootComponent: Component, options: AerogelOption
26
29
  app.mount('#app');
27
30
  app._container?.classList.remove('loading');
28
31
 
29
- Events.emit('application-mounted');
32
+ await Events.emit('application-mounted');
30
33
  }
31
34
 
32
35
  declare module '@/services/Events' {
33
36
  export interface EventsPayload {
37
+ 'application-ready': void;
34
38
  'application-mounted': void;
35
39
  }
36
40
  }
@@ -1,5 +1,8 @@
1
+ import type { App } from 'vue';
2
+
1
3
  import type { Plugin } from '@/plugins';
2
4
 
3
5
  export interface AerogelOptions {
4
6
  plugins?: Plugin[];
7
+ install?(app: App): void | Promise<void>;
5
8
  }
@@ -1,5 +1,10 @@
1
1
  <template>
2
- <AGHeadlessInput ref="$input" :name="name" class="flex">
2
+ <AGHeadlessInput
3
+ ref="$input"
4
+ :name="name"
5
+ class="flex"
6
+ @update:model-value="$emit('update:modelValue', $event)"
7
+ >
3
8
  <AGHeadlessInputInput
4
9
  v-bind="$attrs"
5
10
  type="checkbox"
@@ -30,6 +35,7 @@ import AGHeadlessInputLabel from '../headless/forms/AGHeadlessInputLabel.vue';
30
35
 
31
36
  defineProps({ name: stringProp() });
32
37
  defineOptions({ inheritAttrs: false });
38
+ defineEmits(['update:modelValue']);
33
39
 
34
40
  const $input = componentRef<IAGHeadlessInput>();
35
41
  </script>
@@ -1,26 +1,25 @@
1
1
  <template>
2
- <form @submit.prevent="submit">
2
+ <form @submit.prevent="form?.submit()">
3
3
  <slot />
4
4
  </form>
5
5
  </template>
6
6
 
7
7
  <script setup lang="ts">
8
- import { provide } from 'vue';
8
+ import { provide, watchEffect } from 'vue';
9
9
 
10
10
  import { objectProp } from '@/utils/vue';
11
11
  import type Form from '@/forms/Form';
12
12
 
13
+ let offSubmit: (() => void) | undefined;
13
14
  const props = defineProps({ form: objectProp<Form>() });
14
-
15
15
  const emit = defineEmits<{ submit: [] }>();
16
16
 
17
- provide('form', props.form);
17
+ watchEffect((onCleanup) => {
18
+ offSubmit?.();
19
+ offSubmit = props.form?.on('submit', () => emit('submit'));
18
20
 
19
- function submit() {
20
- if (props.form && !props.form.submit()) {
21
- return;
22
- }
21
+ onCleanup(() => offSubmit?.());
22
+ });
23
23
 
24
- emit('submit');
25
- }
24
+ provide('form', props.form);
26
25
  </script>
@@ -3,8 +3,9 @@
3
3
  ref="$input"
4
4
  class="relative flex flex-col items-center"
5
5
  :class="className"
6
- :name="name"
6
+ v-bind="props"
7
7
  >
8
+ <AGHeadlessInputLabel class="sr-only" />
8
9
  <AGHeadlessInputInput
9
10
  v-bind="attrs"
10
11
  class="block w-full border-0 py-1.5 text-gray-900 ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600"
@@ -12,6 +13,7 @@
12
13
  'ring-1 ring-red-500': $input?.errors,
13
14
  }"
14
15
  />
16
+ <AGHeadlessInputDescription />
15
17
  <div class="absolute bottom-0 left-0 translate-y-full">
16
18
  <AGHeadlessInputError class="mt-1 text-sm text-red-500" />
17
19
  </div>
@@ -19,18 +21,20 @@
19
21
  </template>
20
22
 
21
23
  <script setup lang="ts">
22
- import { componentRef, stringProp } from '@/utils/vue';
23
-
24
- import { useInputAttrs } from '@/utils';
24
+ import { componentRef } from '@/utils/vue';
25
+ import { useInputAttrs } from '@/utils/composition/forms';
26
+ import { useInputProps } from '@/components/headless/forms/AGHeadlessInput';
25
27
  import type { IAGHeadlessInput } from '@/components/headless/forms/AGHeadlessInput';
26
28
 
27
29
  import AGHeadlessInput from '../headless/forms/AGHeadlessInput.vue';
28
- import AGHeadlessInputInput from '../headless/forms/AGHeadlessInputInput.vue';
30
+ import AGHeadlessInputDescription from '../headless/forms/AGHeadlessInputDescription.vue';
29
31
  import AGHeadlessInputError from '../headless/forms/AGHeadlessInputError.vue';
32
+ import AGHeadlessInputInput from '../headless/forms/AGHeadlessInputInput.vue';
33
+ import AGHeadlessInputLabel from '../headless/forms/AGHeadlessInputLabel.vue';
30
34
 
31
- defineProps({ name: stringProp() });
32
35
  defineOptions({ inheritAttrs: false });
33
36
 
37
+ const props = defineProps(useInputProps());
34
38
  const $input = componentRef<IAGHeadlessInput>();
35
39
  const [attrs, className] = useInputAttrs();
36
40
  </script>
@@ -1,7 +1,13 @@
1
1
  <template>
2
2
  <Story>
3
3
  <div class="h-96">
4
- <AGSelect v-model="bestMugiwara" :label="label" :options="options" />
4
+ <AGSelect v-model="bestMugiwara" :label="bestMugiwaraLabel" :options="bestMugiwaraOptions" />
5
+ <AGSelect
6
+ v-model="bestFood"
7
+ :label="bestFoodLabel"
8
+ :options="bestFoodOptions"
9
+ options-text="name"
10
+ />
5
11
  </div>
6
12
  </Story>
7
13
  </template>
@@ -12,8 +18,10 @@ import { ref } from 'vue';
12
18
  import AGSelect from './AGSelect.vue';
13
19
 
14
20
  const bestMugiwara = ref(null);
15
- const label = 'Who\'s the best Mugiwara?';
16
- const options = [
21
+ const bestFood = ref(null);
22
+ const bestMugiwaraLabel = 'Who\'s the best Mugiwara?';
23
+ const bestFoodLabel = 'What\'s the best food?';
24
+ const bestMugiwaraOptions = [
17
25
  'Monkey D. Luffy',
18
26
  'Roronoa Zoro',
19
27
  'Nami',
@@ -25,4 +33,14 @@ const options = [
25
33
  'Brook',
26
34
  'Jinbe',
27
35
  ];
36
+ const bestFoodOptions = [
37
+ {
38
+ id: 'ramen',
39
+ name: 'Ramen',
40
+ },
41
+ {
42
+ id: 'pizza',
43
+ name: 'Pizza',
44
+ },
45
+ ];
28
46
  </script>
@@ -1,5 +1,10 @@
1
1
  <template>
2
- <AGHeadlessSelect v-bind="props" ref="$select" as="div">
2
+ <AGHeadlessSelect
3
+ v-bind="props"
4
+ ref="$select"
5
+ as="div"
6
+ @update:model-value="$emit('update:modelValue', $event)"
7
+ >
3
8
  <AGHeadlessSelectLabel class="block text-sm font-medium leading-6 text-gray-900" />
4
9
  <div class="relative" :class="{ 'mt-2': $select?.label }">
5
10
  <AGHeadlessSelectButton
@@ -21,7 +26,7 @@
21
26
  <AGHeadlessSelectOption
22
27
  v-for="(option, index) in $select?.options ?? []"
23
28
  :key="index"
24
- :value="option.value"
29
+ :value="option"
25
30
  class="relative block cursor-default select-none truncate py-2 pl-3 pr-9"
26
31
  selected-class="font-semibold"
27
32
  unselected-class="font-normal"
@@ -38,7 +43,7 @@
38
43
  import IconCheveronDown from '~icons/zondicons/cheveron-down';
39
44
 
40
45
  import { componentRef } from '@/utils/vue';
41
- import { useSelectProps } from '@/components/headless/forms/AGHeadlessSelect';
46
+ import { useSelectEmits, useSelectProps } from '@/components/headless/forms/AGHeadlessSelect';
42
47
  import type { IAGHeadlessSelect } from '@/components/headless/forms/AGHeadlessSelect';
43
48
 
44
49
  import AGHeadlessSelect from '../headless/forms/AGHeadlessSelect.vue';
@@ -48,6 +53,8 @@ import AGHeadlessSelectLabel from '../headless/forms/AGHeadlessSelectLabel.vue';
48
53
  import AGHeadlessSelectOption from '../headless/forms/AGHeadlessSelectOption.vue';
49
54
  import AGHeadlessSelectOptions from '../headless/forms/AGHeadlessSelectOptions';
50
55
 
56
+ defineEmits(useSelectEmits());
57
+
51
58
  const props = defineProps(useSelectProps());
52
59
  const $select = componentRef<IAGHeadlessSelect>();
53
60
  </script>
@@ -1,5 +1,5 @@
1
1
  <template>
2
- <component :is="component.tag" v-bind="component.props">
2
+ <component :is="component.as" v-bind="component.props">
3
3
  <slot />
4
4
  </component>
5
5
  </template>
@@ -10,7 +10,8 @@ import { objectWithoutEmpty } from '@noeldemartin/utils';
10
10
 
11
11
  import { booleanProp, objectProp, stringProp } from '@/utils/vue';
12
12
 
13
- const { href, url, route, routeParams, routeQuery, submit } = defineProps({
13
+ const props = defineProps({
14
+ as: objectProp(),
14
15
  href: stringProp(),
15
16
  url: stringProp(),
16
17
  route: stringProp(),
@@ -20,32 +21,36 @@ const { href, url, route, routeParams, routeQuery, submit } = defineProps({
20
21
  });
21
22
 
22
23
  const component = computed(() => {
23
- if (route) {
24
+ if (props.as) {
25
+ return { as: props.as, props: {} };
26
+ }
27
+
28
+ if (props.route) {
24
29
  return {
25
- tag: 'router-link',
30
+ as: 'router-link',
26
31
  props: {
27
32
  to: objectWithoutEmpty({
28
- name: route,
29
- params: routeParams,
30
- query: routeQuery,
33
+ name: props.route,
34
+ params: props.routeParams,
35
+ query: props.routeQuery,
31
36
  }),
32
37
  },
33
38
  };
34
39
  }
35
40
 
36
- if (href || url) {
41
+ if (props.href || props.url) {
37
42
  return {
38
- tag: 'a',
43
+ as: 'a',
39
44
  props: {
40
45
  target: '_blank',
41
- href: href || url,
46
+ href: props.href || props.url,
42
47
  },
43
48
  };
44
49
  }
45
50
 
46
51
  return {
47
- tag: 'button',
48
- props: { type: submit ? 'submit' : 'button' },
52
+ as: 'button',
53
+ props: { type: props.submit ? 'submit' : 'button' },
49
54
  };
50
55
  });
51
56
  </script>
@@ -1,20 +1,24 @@
1
1
  import type { ComputedRef, DeepReadonly, ExtractPropTypes, Ref } from 'vue';
2
2
 
3
- import { stringProp } from '@/utils';
3
+ import { mixedProp, stringProp } from '@/utils';
4
4
  import { extractComponentProps } from '@/components/utils';
5
+ import type { FormFieldValue } from '@/forms/Form';
5
6
 
6
7
  export interface IAGHeadlessInput {
7
8
  id: string;
8
9
  name: ComputedRef<string | null>;
9
10
  label: ComputedRef<string | null>;
10
- value: ComputedRef<string | number | boolean | null>;
11
+ description: ComputedRef<string | boolean | null>;
12
+ value: ComputedRef<FormFieldValue | null>;
11
13
  errors: DeepReadonly<Ref<string[] | null>>;
12
- update(value: string | number | boolean | null): void;
14
+ update(value: FormFieldValue | null): void;
13
15
  }
14
16
 
15
17
  export const inputProps = {
16
18
  name: stringProp(),
17
19
  label: stringProp(),
20
+ description: stringProp(),
21
+ modelValue: mixedProp<FormFieldValue>([String, Number, Boolean]),
18
22
  };
19
23
 
20
24
  export function useInputProps(): typeof inputProps {
@@ -9,7 +9,7 @@
9
9
  import { computed, inject, provide, readonly } from 'vue';
10
10
  import { uuid } from '@noeldemartin/utils';
11
11
 
12
- import { mixedProp, stringProp } from '@/utils/vue';
12
+ import { stringProp } from '@/utils/vue';
13
13
  import type Form from '@/forms/Form';
14
14
 
15
15
  import { useInputProps } from './AGHeadlessInput';
@@ -18,7 +18,6 @@ import type { IAGHeadlessInput } from './AGHeadlessInput';
18
18
  const emit = defineEmits(['update:modelValue']);
19
19
  const props = defineProps({
20
20
  as: stringProp('div'),
21
- modelValue: mixedProp<string | number | boolean>([String, Number, Boolean]),
22
21
  ...useInputProps(),
23
22
  });
24
23
  const errors = computed(() => {
@@ -33,9 +32,10 @@ const api: IAGHeadlessInput = {
33
32
  id: `input-${uuid()}`,
34
33
  name: computed(() => props.name),
35
34
  label: computed(() => props.label),
35
+ description: computed(() => props.description),
36
36
  value: computed(() => {
37
37
  if (form && props.name) {
38
- return form.getFieldValue(props.name) as string | number | boolean | null;
38
+ return form.getFieldValue(props.name);
39
39
  }
40
40
 
41
41
  return props.modelValue;
@@ -0,0 +1,28 @@
1
+ <template>
2
+ <slot :id="`${input.id}-description`">
3
+ <AGMarkdown
4
+ v-if="show"
5
+ v-bind="$attrs"
6
+ :id="`${input.id}-description`"
7
+ :text="text"
8
+ />
9
+ </slot>
10
+ </template>
11
+
12
+ <script setup lang="ts">
13
+ import { computed } from 'vue';
14
+
15
+ import { injectReactiveOrFail } from '@/utils/vue';
16
+
17
+ import AGMarkdown from '../../lib/AGMarkdown.vue';
18
+ import type { IAGHeadlessInput } from './AGHeadlessInput';
19
+
20
+ defineOptions({ inheritAttrs: false });
21
+
22
+ const input = injectReactiveOrFail<IAGHeadlessInput>(
23
+ 'input',
24
+ '<AGHeadlessInputDescription> must be a child of a <AGHeadlessInput>',
25
+ );
26
+ const text = computed(() => (typeof input.description === 'string' ? input.description : ''));
27
+ const show = computed(() => !!input.description);
28
+ </script>
@@ -2,20 +2,25 @@
2
2
  <input
3
3
  :id="input.id"
4
4
  ref="$input"
5
+ :name="name"
5
6
  :type="type"
6
- :value="value"
7
7
  :aria-invalid="input.errors ? 'true' : 'false'"
8
- :aria-describedby="input.errors ? `${input.id}-error` : undefined"
8
+ :aria-describedby="
9
+ input.errors ? `${input.id}-error` : input.description ? `${input.id}-description` : undefined
10
+ "
9
11
  :checked="checked"
10
12
  @input="update"
11
13
  >
12
14
  </template>
13
15
 
14
16
  <script setup lang="ts">
15
- import { computed, ref } from 'vue';
17
+ import { computed, ref, watchEffect } from 'vue';
16
18
 
17
19
  import { injectReactiveOrFail, stringProp } from '@/utils';
18
20
  import type { IAGHeadlessInput } from '@/components/headless/forms/AGHeadlessInput';
21
+ import type { FormFieldValue } from '@/forms/Form';
22
+
23
+ import { onFormFocus } from './composition';
19
24
 
20
25
  const props = defineProps({
21
26
  type: stringProp('text'),
@@ -26,6 +31,7 @@ const input = injectReactiveOrFail<IAGHeadlessInput>(
26
31
  'input',
27
32
  '<AGHeadlessInputInput> must be a child of a <AGHeadlessInput>',
28
33
  );
34
+ const name = computed(() => input.name ?? undefined);
29
35
  const value = computed(() => input.value);
30
36
  const checked = computed(() => {
31
37
  if (props.type !== 'checkbox') {
@@ -40,6 +46,36 @@ function update() {
40
46
  return;
41
47
  }
42
48
 
43
- input.update(props.type === 'checkbox' ? $input.value.checked : $input.value.value);
49
+ input.update(getValue());
44
50
  }
51
+
52
+ function getValue(): FormFieldValue | null {
53
+ if (!$input.value) {
54
+ return null;
55
+ }
56
+
57
+ switch (props.type) {
58
+ case 'checkbox':
59
+ return $input.value.checked;
60
+ case 'date':
61
+ return $input.value.valueAsDate;
62
+ default:
63
+ return $input.value.value;
64
+ }
65
+ }
66
+
67
+ onFormFocus(input, () => $input.value?.focus());
68
+ watchEffect(() => {
69
+ if (!$input.value) {
70
+ return;
71
+ }
72
+
73
+ if (props.type === 'date') {
74
+ $input.value.valueAsDate = value.value as Date;
75
+
76
+ return;
77
+ }
78
+
79
+ $input.value.value = value.value as string;
80
+ });
45
81
  </script>
@@ -0,0 +1,40 @@
1
+ <template>
2
+ <textarea
3
+ :id="input.id"
4
+ ref="$textArea"
5
+ :name="name"
6
+ :value="value"
7
+ :aria-invalid="input.errors ? 'true' : 'false'"
8
+ :aria-describedby="
9
+ input.errors ? `${input.id}-error` : input.description ? `${input.id}-description` : undefined
10
+ "
11
+ @input="update"
12
+ />
13
+ </template>
14
+
15
+ <script setup lang="ts">
16
+ import { computed, ref } from 'vue';
17
+
18
+ import { injectReactiveOrFail } from '@/utils';
19
+ import type { IAGHeadlessInput } from '@/components/headless/forms/AGHeadlessInput';
20
+
21
+ import { onFormFocus } from './composition';
22
+
23
+ const $textArea = ref<HTMLTextAreaElement>();
24
+ const input = injectReactiveOrFail<IAGHeadlessInput>(
25
+ 'input',
26
+ '<AGHeadlessInputTextArea> must be a child of a <AGHeadlessInput>',
27
+ );
28
+ const name = computed(() => input.name ?? undefined);
29
+ const value = computed(() => input.value as string);
30
+
31
+ function update() {
32
+ if (!$textArea.value) {
33
+ return;
34
+ }
35
+
36
+ input.update($textArea.value.value);
37
+ }
38
+
39
+ onFormFocus(input, () => $textArea.value?.focus());
40
+ </script>
@@ -1,37 +1,40 @@
1
1
  import type { ComputedRef, DeepReadonly, ExtractPropTypes, Ref } from 'vue';
2
+ import type { Writable } from '@noeldemartin/utils';
2
3
 
3
- import { requiredArrayProp, stringProp } from '@/utils/vue';
4
+ import { mixedProp, requiredArrayProp, stringProp } from '@/utils/vue';
4
5
  import { extractComponentProps } from '@/components/utils';
5
-
6
- export interface IAGSelectOption {
7
- value: string | number | boolean | object | null;
8
- text: string;
9
- }
10
-
11
- export type IAGSelectOptionValue = string | number | boolean | object | null;
6
+ import type { FormFieldValue } from '@/forms/Form';
12
7
 
13
8
  export interface IAGHeadlessSelect {
14
9
  id: string;
15
10
  label: ComputedRef<string | null>;
16
11
  noSelectionText: ComputedRef<string>;
17
12
  buttonText: ComputedRef<string>;
18
- selectedOption: ComputedRef<IAGSelectOption | undefined>;
19
- options: ComputedRef<IAGSelectOption[]>;
13
+ renderText: ComputedRef<(value: FormFieldValue) => string>;
14
+ selectedOption: ComputedRef<FormFieldValue | null>;
15
+ options: ComputedRef<FormFieldValue[]>;
20
16
  errors: DeepReadonly<Ref<string[] | null>>;
21
- update(value: IAGSelectOptionValue): void;
17
+ update(value: FormFieldValue): void;
22
18
  }
23
19
 
24
20
  export const selectProps = {
25
21
  name: stringProp(),
26
22
  label: stringProp(),
27
- options: requiredArrayProp<IAGSelectOptionValue>(),
23
+ options: requiredArrayProp<FormFieldValue>(),
28
24
  noSelectionText: stringProp(),
25
+ optionsText: mixedProp<string | ((option: FormFieldValue) => string)>(),
29
26
  };
30
27
 
28
+ export const selectEmits = ['update:modelValue'] as const;
29
+
31
30
  export function useSelectProps(): typeof selectProps {
32
31
  return selectProps;
33
32
  }
34
33
 
34
+ export function useSelectEmits(): Writable<typeof selectEmits> {
35
+ return [...selectEmits];
36
+ }
37
+
35
38
  export function extractSelectProps<T extends ExtractPropTypes<typeof selectProps>>(
36
39
  props: T,
37
40
  ): Pick<T, keyof typeof selectProps> {
@@ -1,7 +1,7 @@
1
1
  <template>
2
2
  <Listbox
3
3
  v-slot="{ value, open, disabled }: ComponentProps"
4
- :model-value="selectedOption?.value"
4
+ :model-value="selectedOption"
5
5
  @update:model-value="update($event)"
6
6
  >
7
7
  <slot :value="value" :open="open" :disabled="disabled" />
@@ -10,38 +10,37 @@
10
10
 
11
11
  <script setup lang="ts">
12
12
  import { computed, inject, provide } from 'vue';
13
- import { isObject, toString, uuid } from '@noeldemartin/utils';
13
+ import { toString, uuid } from '@noeldemartin/utils';
14
14
  import { Listbox } from '@headlessui/vue';
15
15
 
16
16
  import { mixedProp } from '@/utils/vue';
17
17
  import { translateWithDefault } from '@/lang/utils';
18
18
  import type Form from '@/forms/Form';
19
+ import type { FormFieldValue } from '@/forms/Form';
19
20
  import type { ComponentProps } from '@/utils/vue';
20
21
 
21
- import { useSelectProps } from './AGHeadlessSelect';
22
- import type { IAGHeadlessSelect, IAGSelectOption, IAGSelectOptionValue } from './AGHeadlessSelect';
22
+ import { useSelectEmits, useSelectProps } from './AGHeadlessSelect';
23
+ import type { IAGHeadlessSelect } from './AGHeadlessSelect';
23
24
 
24
- const emit = defineEmits(['update:modelValue']);
25
+ const emit = defineEmits(useSelectEmits());
25
26
  const props = defineProps({
26
- modelValue: mixedProp<IAGSelectOptionValue>(),
27
+ modelValue: mixedProp<FormFieldValue>(),
27
28
  ...useSelectProps(),
28
29
  });
29
- const form = inject<Form | null>('form', null);
30
- const noSelectionText = computed(() => props.noSelectionText ?? translateWithDefault('select.noSelection', '-'));
31
- const options = computed(() =>
32
- props.options.map((value) => {
33
- const option: IAGSelectOption = {
34
- value,
35
- text: toString(isObject(value) && 'text' in value ? value.text : value),
36
- };
30
+ const renderText = computed(() => {
31
+ if (typeof props.optionsText === 'function') {
32
+ return props.optionsText;
33
+ }
37
34
 
38
- return option;
39
- }));
40
- const selectedOption = computed(() => {
41
- const selectedOptionValue = form && props.name ? form.getFieldValue(props.name) : props.modelValue;
35
+ if (typeof props.optionsText === 'string') {
36
+ return (option: FormFieldValue): string => toString(option[props.optionsText as keyof FormFieldValue]);
37
+ }
42
38
 
43
- return options.value.find((option) => option.value === selectedOptionValue);
39
+ return (option: FormFieldValue) => toString(option);
44
40
  });
41
+ const form = inject<Form | null>('form', null);
42
+ const noSelectionText = computed(() => props.noSelectionText ?? translateWithDefault('select.noSelection', '-'));
43
+ const selectedOption = computed(() => (form && props.name ? form.getFieldValue(props.name) : props.modelValue));
45
44
  const errors = computed(() => {
46
45
  if (!form || !props.name) {
47
46
  return null;
@@ -50,7 +49,7 @@ const errors = computed(() => {
50
49
  return form.errors[props.name] ?? null;
51
50
  });
52
51
 
53
- function update(value: IAGSelectOptionValue) {
52
+ function update(value: FormFieldValue) {
54
53
  if (form && props.name) {
55
54
  form.setFieldValue(props.name, value);
56
55
 
@@ -62,12 +61,14 @@ function update(value: IAGSelectOptionValue) {
62
61
 
63
62
  const api: IAGHeadlessSelect = {
64
63
  id: `select-${uuid()}`,
65
- options,
66
64
  noSelectionText,
67
65
  selectedOption,
68
66
  errors,
67
+ options: computed(() => props.options),
69
68
  label: computed(() => props.label),
70
- buttonText: computed(() => selectedOption.value?.text ?? noSelectionText.value),
69
+ buttonText: computed(() =>
70
+ selectedOption.value === null ? noSelectionText.value : renderText.value(selectedOption.value)),
71
+ renderText,
71
72
  update,
72
73
  };
73
74