@aerogel/core 0.0.0-next.926bde19326fe7b6b24b277666936862b64d8295 → 0.0.0-next.9487bb13082b9d479112445804d906125ded5cbc

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 (88) 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 +850 -126
  4. package/dist/aerogel-core.esm.js +1 -1
  5. package/dist/aerogel-core.esm.js.map +1 -1
  6. package/dist/virtual.d.ts +11 -0
  7. package/histoire.config.ts +7 -0
  8. package/noeldemartin.config.js +4 -1
  9. package/package.json +8 -4
  10. package/postcss.config.js +6 -0
  11. package/src/assets/histoire.css +3 -0
  12. package/src/bootstrap/index.ts +8 -2
  13. package/src/components/AGAppModals.vue +15 -0
  14. package/src/components/AGAppOverlays.vue +5 -7
  15. package/src/components/AGAppSnackbars.vue +13 -0
  16. package/src/components/basic/AGErrorMessage.vue +16 -0
  17. package/src/components/basic/AGLink.vue +9 -0
  18. package/src/components/basic/AGMarkdown.vue +10 -9
  19. package/src/components/basic/index.ts +3 -1
  20. package/src/components/constants.ts +8 -0
  21. package/src/components/forms/AGButton.vue +33 -10
  22. package/src/components/forms/AGCheckbox.vue +35 -0
  23. package/src/components/forms/AGInput.vue +8 -4
  24. package/src/components/forms/AGSelect.story.vue +28 -0
  25. package/src/components/forms/AGSelect.vue +53 -0
  26. package/src/components/forms/index.ts +5 -5
  27. package/src/components/headless/forms/AGHeadlessButton.vue +7 -7
  28. package/src/components/headless/forms/AGHeadlessInput.ts +2 -2
  29. package/src/components/headless/forms/AGHeadlessInput.vue +7 -7
  30. package/src/components/headless/forms/AGHeadlessInputError.vue +1 -1
  31. package/src/components/headless/forms/AGHeadlessInputInput.vue +15 -3
  32. package/src/components/headless/forms/AGHeadlessSelect.ts +43 -0
  33. package/src/components/headless/forms/AGHeadlessSelect.vue +77 -0
  34. package/src/components/headless/forms/AGHeadlessSelectButton.vue +24 -0
  35. package/src/components/headless/forms/AGHeadlessSelectError.vue +26 -0
  36. package/src/components/headless/forms/AGHeadlessSelectLabel.vue +21 -0
  37. package/src/components/headless/forms/AGHeadlessSelectOption.ts +4 -0
  38. package/src/components/headless/forms/AGHeadlessSelectOption.vue +39 -0
  39. package/src/components/headless/forms/AGHeadlessSelectOptions.ts +3 -0
  40. package/src/components/headless/forms/index.ts +9 -1
  41. package/src/components/headless/index.ts +1 -0
  42. package/src/components/headless/modals/AGHeadlessModalPanel.vue +5 -1
  43. package/src/components/headless/snackbars/AGHeadlessSnackbar.vue +10 -0
  44. package/src/components/headless/snackbars/index.ts +25 -0
  45. package/src/components/index.ts +2 -0
  46. package/src/components/modals/AGAlertModal.vue +0 -1
  47. package/src/components/modals/AGConfirmModal.vue +3 -3
  48. package/src/components/modals/AGErrorReportModal.ts +20 -0
  49. package/src/components/modals/AGErrorReportModal.vue +62 -0
  50. package/src/components/modals/AGErrorReportModalButtons.vue +109 -0
  51. package/src/components/modals/AGErrorReportModalTitle.vue +25 -0
  52. package/src/components/modals/AGLoadingModal.vue +19 -0
  53. package/src/components/modals/AGModal.ts +1 -1
  54. package/src/components/modals/AGModal.vue +23 -4
  55. package/src/components/modals/AGModalTitle.vue +9 -0
  56. package/src/components/modals/index.ts +18 -2
  57. package/src/components/snackbars/AGSnackbar.vue +42 -0
  58. package/src/components/snackbars/index.ts +3 -0
  59. package/src/directives/index.ts +16 -3
  60. package/src/errors/Errors.state.ts +31 -0
  61. package/src/errors/Errors.ts +183 -0
  62. package/src/errors/index.ts +59 -0
  63. package/src/forms/Form.ts +26 -15
  64. package/src/forms/utils.ts +17 -0
  65. package/src/lang/Lang.ts +12 -4
  66. package/src/lang/index.ts +3 -5
  67. package/src/lang/utils.ts +4 -0
  68. package/src/main.histoire.ts +1 -0
  69. package/src/main.ts +1 -2
  70. package/src/plugins/Plugin.ts +1 -0
  71. package/src/plugins/index.ts +19 -0
  72. package/src/services/App.state.ts +10 -2
  73. package/src/services/App.ts +14 -1
  74. package/src/services/Service.ts +132 -45
  75. package/src/services/index.ts +21 -4
  76. package/src/services/store.ts +27 -0
  77. package/src/types/virtual.d.ts +11 -0
  78. package/src/ui/UI.state.ts +11 -1
  79. package/src/ui/UI.ts +52 -8
  80. package/src/ui/index.ts +7 -1
  81. package/src/utils/composition/forms.ts +11 -0
  82. package/src/utils/index.ts +1 -0
  83. package/src/utils/markdown.ts +11 -2
  84. package/src/utils/vue.ts +5 -3
  85. package/tailwind.config.js +4 -0
  86. package/tsconfig.json +1 -0
  87. package/vite.config.ts +2 -1
  88. package/src/globals.ts +0 -6
@@ -0,0 +1,15 @@
1
+ <template>
2
+ <aside v-if="modal">
3
+ <AGModalContext :child-index="1" :modal="modal" />
4
+ </aside>
5
+ </template>
6
+
7
+ <script setup lang="ts">
8
+ import { computed } from 'vue';
9
+
10
+ import UI from '@/ui/UI';
11
+
12
+ import AGModalContext from './modals/AGModalContext.vue';
13
+
14
+ const modal = computed(() => UI.modals[0] ?? null);
15
+ </script>
@@ -1,21 +1,19 @@
1
1
  <template>
2
2
  <div ref="$backdrop" class="pointer-events-none fixed inset-0 z-50 bg-black/30 opacity-0" />
3
- <aside v-if="modal">
4
- <AGModalContext :child-index="1" :modal="modal" />
5
- </aside>
3
+ <AGAppModals />
4
+ <AGAppSnackbars />
6
5
  </template>
7
6
 
8
7
  <script setup lang="ts">
9
- import { computed, ref } from 'vue';
8
+ import { ref } from 'vue';
10
9
 
11
- import UI from '@/ui/UI';
12
10
  import { useEvent } from '@/utils/composition/events';
13
11
 
14
- import AGModalContext from './modals/AGModalContext.vue';
12
+ import AGAppModals from './AGAppModals.vue';
13
+ import AGAppSnackbars from './AGAppSnackbars.vue';
15
14
 
16
15
  const $backdrop = ref<HTMLElement | null>(null);
17
16
  const backdropHidden = ref(true);
18
- const modal = computed(() => UI.modals[0] ?? null);
19
17
 
20
18
  useEvent('show-overlays-backdrop', async () => {
21
19
  if (!$backdrop.value || !backdropHidden.value) {
@@ -0,0 +1,13 @@
1
+ <template>
2
+ <div aria-live="assertive" class="z-60 pointer-events-none fixed inset-0 flex items-end px-4 py-6 sm:p-6">
3
+ <div class="flex w-full flex-col items-center space-y-4 sm:items-end">
4
+ <component
5
+ :is="snackbar.component"
6
+ v-for="snackbar of $ui.snackbars"
7
+ :id="snackbar.id"
8
+ :key="snackbar.id"
9
+ v-bind="snackbar.properties"
10
+ />
11
+ </div>
12
+ </div>
13
+ </template>
@@ -0,0 +1,16 @@
1
+ <template>
2
+ <AGMarkdown :text="message" inline />
3
+ </template>
4
+
5
+ <script setup lang="ts">
6
+ import { computed } from 'vue';
7
+
8
+ import Errors from '@/errors/Errors';
9
+ import { requiredObjectProp } from '@/utils/vue';
10
+ import type { ErrorSource } from '@/errors/Errors.state';
11
+
12
+ import AGMarkdown from './AGMarkdown.vue';
13
+
14
+ const props = defineProps({ error: requiredObjectProp<ErrorSource>() });
15
+ const message = computed(() => Errors.getErrorMessage(props.error));
16
+ </script>
@@ -0,0 +1,9 @@
1
+ <template>
2
+ <AGHeadlessButton class="font-medium hover:underline">
3
+ <slot />
4
+ </AGHeadlessButton>
5
+ </template>
6
+
7
+ <script setup lang="ts">
8
+ import AGHeadlessButton from '../headless/forms/AGHeadlessButton.vue';
9
+ </script>
@@ -6,30 +6,31 @@
6
6
  import { computed, h } from 'vue';
7
7
 
8
8
  import { renderMarkdown } from '@/utils/markdown';
9
- import { booleanProp, stringProp } from '@/utils/vue';
9
+ import { booleanProp, objectProp, stringProp } from '@/utils/vue';
10
10
  import { translate } from '@/lang';
11
11
 
12
12
  const props = defineProps({
13
- as: stringProp('div'),
13
+ as: stringProp(),
14
+ inline: booleanProp(),
14
15
  langKey: stringProp(),
16
+ langParams: objectProp<Record<string, unknown>>(),
15
17
  text: stringProp(),
16
- inline: booleanProp(),
17
- raw: booleanProp(),
18
18
  });
19
19
 
20
- const markdown = computed(() => props.text ?? (props.langKey && translate(props.langKey)));
20
+ const markdown = computed(() => props.text ?? (props.langKey && translate(props.langKey, props.langParams ?? {})));
21
21
  const html = computed(() => {
22
22
  if (!markdown.value) {
23
23
  return null;
24
24
  }
25
25
 
26
- let html = renderMarkdown(markdown.value);
26
+ let renderedHtml = renderMarkdown(markdown.value);
27
27
 
28
28
  if (props.inline) {
29
- html = html.replace('<p>', '<span>').replace('</p>', '</span>');
29
+ renderedHtml = renderedHtml.replace('<p>', '<span>').replace('</p>', '</span>');
30
30
  }
31
31
 
32
- return html;
32
+ return renderedHtml;
33
33
  });
34
- const root = () => h(props.as, { class: props.raw ? '' : 'prose', innerHTML: html.value });
34
+ const root = () =>
35
+ h(props.as ?? (props.inline ? 'span' : 'div'), { class: props.inline ? '' : 'prose', innerHTML: html.value });
35
36
  </script>
@@ -1,3 +1,5 @@
1
+ import AGErrorMessage from './AGErrorMessage.vue';
2
+ import AGLink from './AGLink.vue';
1
3
  import AGMarkdown from './AGMarkdown.vue';
2
4
 
3
- export { AGMarkdown };
5
+ export { AGErrorMessage, AGLink, AGMarkdown };
@@ -0,0 +1,8 @@
1
+ export const Colors = {
2
+ Primary: 'primary',
3
+ Secondary: 'secondary',
4
+ Danger: 'danger',
5
+ Clear: 'clear',
6
+ } as const;
7
+
8
+ export type Color = (typeof Colors)[keyof typeof Colors];
@@ -1,21 +1,44 @@
1
1
  <template>
2
- <AGHeadlessButton
3
- class="px-2.5 py-1.5 text-white focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2"
4
- :class="{
5
- 'bg-indigo-600 hover:bg-indigo-500 focus-visible:outline-indigo-600': !secondary,
6
- 'bg-gray-600 hover:bg-gray-500 focus-visible:outline-gray-600': secondary,
7
- }"
8
- >
2
+ <AGHeadlessButton class="px-2.5 py-1.5 focus-visible:outline focus-visible:outline-2" :class="colorClasses">
9
3
  <slot />
10
4
  </AGHeadlessButton>
11
5
  </template>
12
6
 
13
7
  <script setup lang="ts">
14
- import { booleanProp } from '@/utils';
8
+ import { computed } from 'vue';
9
+
10
+ import { enumProp } from '@/utils/vue';
11
+ import { Colors } from '@/components/constants';
15
12
 
16
13
  import AGHeadlessButton from '../headless/forms/AGHeadlessButton.vue';
17
14
 
18
- defineProps({
19
- secondary: booleanProp(),
15
+ const props = defineProps({
16
+ color: enumProp(Colors, Colors.Primary),
17
+ });
18
+
19
+ const colorClasses = computed(() => {
20
+ switch (props.color) {
21
+ case Colors.Secondary:
22
+ return [
23
+ 'text-white bg-gray-600',
24
+ 'hover:bg-gray-500',
25
+ 'focus-visible:outline-offset-2 focus-visible:outline-gray-600',
26
+ ].join(' ');
27
+ case Colors.Clear:
28
+ return 'hover:bg-gray-500/20 focus-visible:outline-gray-500/60';
29
+ case Colors.Danger:
30
+ return [
31
+ 'text-white bg-red-600',
32
+ 'hover:bg-red-500',
33
+ 'focus-visible:outline-offset-2 focus-visible:outline-red-600',
34
+ ].join(' ');
35
+ case Colors.Primary:
36
+ default:
37
+ return [
38
+ 'text-white bg-indigo-600',
39
+ 'hover:bg-indigo-500',
40
+ 'focus-visible:outline-offset-2 focus-visible:outline-indigo-600',
41
+ ].join(' ');
42
+ }
20
43
  });
21
44
  </script>
@@ -0,0 +1,35 @@
1
+ <template>
2
+ <AGHeadlessInput ref="$input" :name="name" class="flex">
3
+ <AGHeadlessInputInput
4
+ v-bind="$attrs"
5
+ type="checkbox"
6
+ :class="{
7
+ 'text-indigo-600 focus:ring-indigo-600': !$input?.errors,
8
+ 'border-red-200 text-red-600 focus:ring-red-600': $input?.errors,
9
+ }"
10
+ />
11
+
12
+ <div class="ml-2">
13
+ <AGHeadlessInputLabel v-if="$slots.default">
14
+ <slot />
15
+ </AGHeadlessInputLabel>
16
+ <AGHeadlessInputError class="text-sm text-red-600" />
17
+ </div>
18
+ </AGHeadlessInput>
19
+ </template>
20
+
21
+ <script setup lang="ts">
22
+ import { componentRef, stringProp } from '@/utils/vue';
23
+
24
+ import type { IAGHeadlessInput } from '@/components/headless/forms/AGHeadlessInput';
25
+
26
+ import AGHeadlessInput from '../headless/forms/AGHeadlessInput.vue';
27
+ import AGHeadlessInputError from '../headless/forms/AGHeadlessInputError.vue';
28
+ import AGHeadlessInputInput from '../headless/forms/AGHeadlessInputInput.vue';
29
+ import AGHeadlessInputLabel from '../headless/forms/AGHeadlessInputLabel.vue';
30
+
31
+ defineProps({ name: stringProp() });
32
+ defineOptions({ inheritAttrs: false });
33
+
34
+ const $input = componentRef<IAGHeadlessInput>();
35
+ </script>
@@ -1,24 +1,27 @@
1
1
  <template>
2
2
  <AGHeadlessInput
3
3
  ref="$input"
4
- as="div"
5
- class="flex flex-col items-center"
4
+ class="relative flex flex-col items-center"
5
+ :class="className"
6
6
  :name="name"
7
7
  >
8
8
  <AGHeadlessInputInput
9
- v-bind="$attrs"
9
+ v-bind="attrs"
10
10
  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"
11
11
  :class="{
12
12
  'ring-1 ring-red-500': $input?.errors,
13
13
  }"
14
14
  />
15
- <AGHeadlessInputError class="mt-1 text-sm text-red-500" />
15
+ <div class="absolute bottom-0 left-0 translate-y-full">
16
+ <AGHeadlessInputError class="mt-1 text-sm text-red-500" />
17
+ </div>
16
18
  </AGHeadlessInput>
17
19
  </template>
18
20
 
19
21
  <script setup lang="ts">
20
22
  import { componentRef, stringProp } from '@/utils/vue';
21
23
 
24
+ import { useInputAttrs } from '@/utils';
22
25
  import type { IAGHeadlessInput } from '@/components/headless/forms/AGHeadlessInput';
23
26
 
24
27
  import AGHeadlessInput from '../headless/forms/AGHeadlessInput.vue';
@@ -29,4 +32,5 @@ defineProps({ name: stringProp() });
29
32
  defineOptions({ inheritAttrs: false });
30
33
 
31
34
  const $input = componentRef<IAGHeadlessInput>();
35
+ const [attrs, className] = useInputAttrs();
32
36
  </script>
@@ -0,0 +1,28 @@
1
+ <template>
2
+ <Story>
3
+ <div class="h-96">
4
+ <AGSelect v-model="bestMugiwara" :label="label" :options="options" />
5
+ </div>
6
+ </Story>
7
+ </template>
8
+
9
+ <script setup lang="ts">
10
+ import { ref } from 'vue';
11
+
12
+ import AGSelect from './AGSelect.vue';
13
+
14
+ const bestMugiwara = ref(null);
15
+ const label = 'Who\'s the best Mugiwara?';
16
+ const options = [
17
+ 'Monkey D. Luffy',
18
+ 'Roronoa Zoro',
19
+ 'Nami',
20
+ 'Usopp',
21
+ 'Sanji',
22
+ 'Tony Tony Chopper',
23
+ 'Nico Robin',
24
+ 'Franky',
25
+ 'Brook',
26
+ 'Jinbe',
27
+ ];
28
+ </script>
@@ -0,0 +1,53 @@
1
+ <template>
2
+ <AGHeadlessSelect v-bind="props" ref="$select" as="div">
3
+ <AGHeadlessSelectLabel class="block text-sm font-medium leading-6 text-gray-900" />
4
+ <div class="relative" :class="{ 'mt-2': $select?.label }">
5
+ <AGHeadlessSelectButton
6
+ class="relative w-full cursor-default bg-white py-1.5 pl-3 pr-10 text-left text-gray-900 ring-1 ring-inset ring-gray-300 focus:outline-none focus:ring-2 focus:ring-indigo-600"
7
+ text-class="block truncate"
8
+ :class="{
9
+ 'ring-1 ring-red-500': $select?.errors,
10
+ }"
11
+ >
12
+ <template #icon>
13
+ <span class="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-2">
14
+ <IconCheveronDown class="h-5 w-5 text-gray-400" />
15
+ </span>
16
+ </template>
17
+ </AGHeadlessSelectButton>
18
+ <AGHeadlessSelectOptions
19
+ class="absolute z-10 mt-1 max-h-60 w-full overflow-auto border bg-white py-1 text-base ring-1 ring-black ring-opacity-5 focus:outline-none"
20
+ >
21
+ <AGHeadlessSelectOption
22
+ v-for="(option, index) in $select?.options ?? []"
23
+ :key="index"
24
+ :value="option.value"
25
+ class="relative block cursor-default select-none truncate py-2 pl-3 pr-9"
26
+ selected-class="font-semibold"
27
+ unselected-class="font-normal"
28
+ active-class="bg-indigo-600 text-white"
29
+ inactive-class="text-gray-900"
30
+ />
31
+ </AGHeadlessSelectOptions>
32
+ </div>
33
+ <AGHeadlessSelectError class="mt-2 text-sm text-red-600" />
34
+ </AGHeadlessSelect>
35
+ </template>
36
+
37
+ <script setup lang="ts">
38
+ import IconCheveronDown from '~icons/zondicons/cheveron-down';
39
+
40
+ import { componentRef } from '@/utils/vue';
41
+ import { useSelectProps } from '@/components/headless/forms/AGHeadlessSelect';
42
+ import type { IAGHeadlessSelect } from '@/components/headless/forms/AGHeadlessSelect';
43
+
44
+ import AGHeadlessSelect from '../headless/forms/AGHeadlessSelect.vue';
45
+ import AGHeadlessSelectButton from '../headless/forms/AGHeadlessSelectButton.vue';
46
+ import AGHeadlessSelectError from '../headless/forms/AGHeadlessSelectError.vue';
47
+ import AGHeadlessSelectLabel from '../headless/forms/AGHeadlessSelectLabel.vue';
48
+ import AGHeadlessSelectOption from '../headless/forms/AGHeadlessSelectOption.vue';
49
+ import AGHeadlessSelectOptions from '../headless/forms/AGHeadlessSelectOptions';
50
+
51
+ const props = defineProps(useSelectProps());
52
+ const $select = componentRef<IAGHeadlessSelect>();
53
+ </script>
@@ -1,5 +1,5 @@
1
- import AGButton from './AGButton.vue';
2
- import AGForm from './AGForm.vue';
3
- import AGInput from './AGInput.vue';
4
-
5
- export { AGButton, AGForm, AGInput };
1
+ export { default as AGButton } from './AGButton.vue';
2
+ export { default as AGCheckbox } from './AGCheckbox.vue';
3
+ export { default as AGForm } from './AGForm.vue';
4
+ export { default as AGInput } from './AGInput.vue';
5
+ export { default as AGSelect } from './AGSelect.vue';
@@ -7,15 +7,15 @@
7
7
  <script setup lang="ts">
8
8
  import { computed } from 'vue';
9
9
  import { objectWithoutEmpty } from '@noeldemartin/utils';
10
- import type { LocationQuery, RouteLocation, RouteParams } from 'vue-router';
11
10
 
12
11
  import { booleanProp, objectProp, stringProp } from '@/utils/vue';
13
12
 
14
- const { url, route, routeParams, routeQuery, submit } = defineProps({
13
+ const { href, url, route, routeParams, routeQuery, submit } = defineProps({
14
+ href: stringProp(),
15
15
  url: stringProp(),
16
16
  route: stringProp(),
17
- routeParams: objectProp<RouteParams>(() => ({})),
18
- routeQuery: objectProp<LocationQuery>(() => ({})),
17
+ routeParams: objectProp(() => ({})),
18
+ routeQuery: objectProp(() => ({})),
19
19
  submit: booleanProp(),
20
20
  });
21
21
 
@@ -24,7 +24,7 @@ const component = computed(() => {
24
24
  return {
25
25
  tag: 'router-link',
26
26
  props: {
27
- to: objectWithoutEmpty<Partial<RouteLocation>>({
27
+ to: objectWithoutEmpty({
28
28
  name: route,
29
29
  params: routeParams,
30
30
  query: routeQuery,
@@ -33,12 +33,12 @@ const component = computed(() => {
33
33
  };
34
34
  }
35
35
 
36
- if (url) {
36
+ if (href || url) {
37
37
  return {
38
38
  tag: 'a',
39
39
  props: {
40
40
  target: '_blank',
41
- href: url,
41
+ href: href || url,
42
42
  },
43
43
  };
44
44
  }
@@ -2,7 +2,7 @@ import type { ComputedRef, DeepReadonly, Ref } from 'vue';
2
2
 
3
3
  export interface IAGHeadlessInput {
4
4
  id: string;
5
- value: ComputedRef<string | number | null>;
5
+ value: ComputedRef<string | number | boolean | null>;
6
6
  errors: DeepReadonly<Ref<string[] | null>>;
7
- update(value: string | number | null): void;
7
+ update(value: string | number | boolean | null): void;
8
8
  }
@@ -9,16 +9,16 @@
9
9
  import { computed, inject, provide, readonly } from 'vue';
10
10
  import { uuid } from '@noeldemartin/utils';
11
11
 
12
- import { stringProp } from '@/utils/vue';
12
+ import { mixedProp, stringProp } from '@/utils/vue';
13
13
  import type Form from '@/forms/Form';
14
14
 
15
15
  import type { IAGHeadlessInput } from './AGHeadlessInput';
16
16
 
17
17
  const emit = defineEmits(['update:modelValue']);
18
18
  const props = defineProps({
19
- as: stringProp(),
19
+ as: stringProp('div'),
20
20
  name: stringProp(),
21
- modelValue: stringProp(),
21
+ modelValue: mixedProp<string | number | boolean>([String, Number, Boolean]),
22
22
  });
23
23
  const errors = computed(() => {
24
24
  if (!form || !props.name) {
@@ -28,11 +28,11 @@ const errors = computed(() => {
28
28
  return form.errors[props.name] ?? null;
29
29
  });
30
30
  const form = inject<Form | null>('form', null);
31
- const publicApi: IAGHeadlessInput = {
31
+ const api: IAGHeadlessInput = {
32
32
  id: `input-${uuid()}`,
33
33
  value: computed(() => {
34
34
  if (form && props.name) {
35
- return form.getFieldValue(props.name);
35
+ return form.getFieldValue(props.name) as string | number | boolean | null;
36
36
  }
37
37
 
38
38
  return props.modelValue;
@@ -49,6 +49,6 @@ const publicApi: IAGHeadlessInput = {
49
49
  },
50
50
  };
51
51
 
52
- provide<IAGHeadlessInput>('input', publicApi);
53
- defineExpose<IAGHeadlessInput>(publicApi);
52
+ provide<IAGHeadlessInput>('input', api);
53
+ defineExpose<IAGHeadlessInput>(api);
54
54
  </script>
@@ -8,7 +8,7 @@
8
8
  import { computed } from 'vue';
9
9
 
10
10
  import { injectReactiveOrFail } from '@/utils/vue';
11
- import { translateWithDefault } from '@/lang';
11
+ import { translateWithDefault } from '@/lang/utils';
12
12
 
13
13
  import type { IAGHeadlessInput } from './AGHeadlessInput';
14
14
 
@@ -2,10 +2,11 @@
2
2
  <input
3
3
  :id="input.id"
4
4
  ref="$input"
5
- type="text"
5
+ :type="type"
6
6
  :value="value"
7
7
  :aria-invalid="input.errors ? 'true' : 'false'"
8
8
  :aria-describedby="input.errors ? `${input.id}-error` : undefined"
9
+ :checked="checked"
9
10
  @input="update"
10
11
  >
11
12
  </template>
@@ -13,21 +14,32 @@
13
14
  <script setup lang="ts">
14
15
  import { computed, ref } from 'vue';
15
16
 
16
- import { injectReactiveOrFail } from '@/utils';
17
+ import { injectReactiveOrFail, stringProp } from '@/utils';
17
18
  import type { IAGHeadlessInput } from '@/components/headless/forms/AGHeadlessInput';
18
19
 
20
+ const props = defineProps({
21
+ type: stringProp('text'),
22
+ });
23
+
19
24
  const $input = ref<HTMLInputElement>();
20
25
  const input = injectReactiveOrFail<IAGHeadlessInput>(
21
26
  'input',
22
27
  '<AGHeadlessInputInput> must be a child of a <AGHeadlessInput>',
23
28
  );
24
29
  const value = computed(() => input.value);
30
+ const checked = computed(() => {
31
+ if (props.type !== 'checkbox') {
32
+ return;
33
+ }
34
+
35
+ return !!value.value;
36
+ });
25
37
 
26
38
  function update() {
27
39
  if (!$input.value) {
28
40
  return;
29
41
  }
30
42
 
31
- input.update($input.value.value);
43
+ input.update(props.type === 'checkbox' ? $input.value.checked : $input.value.value);
32
44
  }
33
45
  </script>
@@ -0,0 +1,43 @@
1
+ import type { ComputedRef, DeepReadonly, Ref } from 'vue';
2
+
3
+ import { requiredArrayProp, stringProp } from '@/utils/vue';
4
+
5
+ export interface IAGSelectOption {
6
+ value: string | number | boolean | object | null;
7
+ text: string;
8
+ }
9
+
10
+ export type IAGSelectOptionValue = string | number | boolean | object | null;
11
+
12
+ export interface IAGHeadlessSelect {
13
+ id: string;
14
+ label: ComputedRef<string | null>;
15
+ noSelectionText: ComputedRef<string>;
16
+ buttonText: ComputedRef<string>;
17
+ selectedOption: ComputedRef<IAGSelectOption | undefined>;
18
+ options: ComputedRef<IAGSelectOption[]>;
19
+ errors: DeepReadonly<Ref<string[] | null>>;
20
+ update(value: IAGSelectOptionValue): void;
21
+ }
22
+
23
+ export const selectProps = {
24
+ options: requiredArrayProp<IAGSelectOptionValue>(),
25
+ label: stringProp(),
26
+ noSelectionText: stringProp(),
27
+ };
28
+
29
+ export function useSelectProps(): typeof selectProps {
30
+ return selectProps;
31
+ }
32
+
33
+ export function extractSelectProps<T extends Record<keyof typeof selectProps, unknown>>(
34
+ componentProps: T,
35
+ ): Pick<T, keyof typeof selectProps> {
36
+ return Object.keys(selectProps).reduce((extractedProps, selectProp) => {
37
+ const prop = selectProp as keyof typeof selectProps;
38
+
39
+ extractedProps[prop] = componentProps[prop];
40
+
41
+ return extractedProps;
42
+ }, {} as Pick<T, keyof typeof selectProps>);
43
+ }
@@ -0,0 +1,77 @@
1
+ <template>
2
+ <Listbox
3
+ v-slot="{ value, open, disabled }: ComponentProps"
4
+ :model-value="selectedOption?.value"
5
+ @update:model-value="update($event)"
6
+ >
7
+ <slot :value="value" :open="open" :disabled="disabled" />
8
+ </Listbox>
9
+ </template>
10
+
11
+ <script setup lang="ts">
12
+ import { computed, inject, provide } from 'vue';
13
+ import { isObject, toString, uuid } from '@noeldemartin/utils';
14
+ import { Listbox } from '@headlessui/vue';
15
+
16
+ import { mixedProp, stringProp } from '@/utils/vue';
17
+ import { translateWithDefault } from '@/lang/utils';
18
+ import type Form from '@/forms/Form';
19
+ import type { ComponentProps } from '@/utils/vue';
20
+
21
+ import { useSelectProps } from './AGHeadlessSelect';
22
+ import type { IAGHeadlessSelect, IAGSelectOption, IAGSelectOptionValue } from './AGHeadlessSelect';
23
+
24
+ const emit = defineEmits(['update:modelValue']);
25
+ const props = defineProps({
26
+ name: stringProp(),
27
+ modelValue: mixedProp<IAGSelectOptionValue>(),
28
+ ...useSelectProps(),
29
+ });
30
+ const form = inject<Form | null>('form', null);
31
+ const noSelectionText = computed(() => props.noSelectionText ?? translateWithDefault('select.noSelection', '-'));
32
+ const options = computed(() =>
33
+ props.options.map((value) => {
34
+ const option: IAGSelectOption = {
35
+ value,
36
+ text: toString(isObject(value) && 'text' in value ? value.text : value),
37
+ };
38
+
39
+ return option;
40
+ }));
41
+ const selectedOption = computed(() => {
42
+ const selectedOptionValue = form && props.name ? form.getFieldValue(props.name) : props.modelValue;
43
+
44
+ return options.value.find((option) => option.value === selectedOptionValue);
45
+ });
46
+ const errors = computed(() => {
47
+ if (!form || !props.name) {
48
+ return null;
49
+ }
50
+
51
+ return form.errors[props.name] ?? null;
52
+ });
53
+
54
+ function update(value: IAGSelectOptionValue) {
55
+ if (form && props.name) {
56
+ form.setFieldValue(props.name, value);
57
+
58
+ return;
59
+ }
60
+
61
+ emit('update:modelValue', value);
62
+ }
63
+
64
+ const api: IAGHeadlessSelect = {
65
+ id: `select-${uuid()}`,
66
+ options,
67
+ noSelectionText,
68
+ selectedOption,
69
+ errors,
70
+ label: computed(() => props.label),
71
+ buttonText: computed(() => selectedOption.value?.text ?? noSelectionText.value),
72
+ update,
73
+ };
74
+
75
+ provide<IAGHeadlessSelect>('select', api);
76
+ defineExpose<IAGHeadlessSelect>(api);
77
+ </script>