@aerogel/core 0.0.0-next.b85327579d32f21c6a9fa21142f0165cdd320d7e → 0.0.0-next.d547095ca85c86c1e41f5c7fa170d0ba856c3f4d

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 (106) 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 +1371 -257
  4. package/dist/aerogel-core.esm.js +1 -1
  5. package/dist/aerogel-core.esm.js.map +1 -1
  6. package/histoire.config.ts +7 -0
  7. package/noeldemartin.config.js +4 -1
  8. package/package.json +13 -4
  9. package/postcss.config.js +6 -0
  10. package/src/assets/histoire.css +3 -0
  11. package/src/bootstrap/bootstrap.test.ts +4 -3
  12. package/src/bootstrap/index.ts +25 -5
  13. package/src/bootstrap/options.ts +3 -0
  14. package/src/components/AGAppLayout.vue +7 -2
  15. package/src/components/AGAppModals.vue +15 -0
  16. package/src/components/AGAppOverlays.vue +10 -8
  17. package/src/components/AGAppSnackbars.vue +13 -0
  18. package/src/components/constants.ts +8 -0
  19. package/src/components/forms/AGButton.vue +25 -15
  20. package/src/components/forms/AGCheckbox.vue +7 -1
  21. package/src/components/forms/AGInput.vue +8 -6
  22. package/src/components/forms/AGSelect.story.vue +46 -0
  23. package/src/components/forms/AGSelect.vue +60 -0
  24. package/src/components/forms/index.ts +5 -6
  25. package/src/components/headless/forms/AGHeadlessButton.vue +9 -8
  26. package/src/components/headless/forms/AGHeadlessInput.ts +21 -1
  27. package/src/components/headless/forms/AGHeadlessInput.vue +8 -5
  28. package/src/components/headless/forms/AGHeadlessInputInput.vue +2 -0
  29. package/src/components/headless/forms/AGHeadlessInputLabel.vue +8 -2
  30. package/src/components/headless/forms/AGHeadlessSelect.ts +42 -0
  31. package/src/components/headless/forms/AGHeadlessSelect.vue +77 -0
  32. package/src/components/headless/forms/AGHeadlessSelectButton.vue +24 -0
  33. package/src/components/headless/forms/AGHeadlessSelectError.vue +26 -0
  34. package/src/components/headless/forms/AGHeadlessSelectLabel.vue +24 -0
  35. package/src/components/headless/forms/AGHeadlessSelectOption.ts +4 -0
  36. package/src/components/headless/forms/AGHeadlessSelectOption.vue +39 -0
  37. package/src/components/headless/forms/AGHeadlessSelectOptions.ts +3 -0
  38. package/src/components/headless/forms/index.ts +9 -1
  39. package/src/components/headless/index.ts +1 -0
  40. package/src/components/headless/modals/AGHeadlessModal.ts +27 -0
  41. package/src/components/headless/modals/AGHeadlessModal.vue +3 -5
  42. package/src/components/headless/modals/index.ts +4 -6
  43. package/src/components/headless/snackbars/AGHeadlessSnackbar.vue +10 -0
  44. package/src/components/headless/snackbars/index.ts +40 -0
  45. package/src/components/index.ts +3 -1
  46. package/src/components/lib/AGErrorMessage.vue +16 -0
  47. package/src/components/lib/AGLink.vue +9 -0
  48. package/src/components/{basic → lib}/AGMarkdown.vue +7 -6
  49. package/src/components/lib/AGMeasured.vue +15 -0
  50. package/src/components/lib/AGStartupCrash.vue +31 -0
  51. package/src/components/lib/index.ts +5 -0
  52. package/src/components/modals/AGAlertModal.ts +15 -0
  53. package/src/components/modals/AGAlertModal.vue +4 -16
  54. package/src/components/modals/AGConfirmModal.ts +27 -0
  55. package/src/components/modals/AGConfirmModal.vue +8 -12
  56. package/src/components/modals/AGErrorReportModal.ts +46 -0
  57. package/src/components/modals/AGErrorReportModal.vue +54 -0
  58. package/src/components/modals/AGErrorReportModalButtons.vue +111 -0
  59. package/src/components/modals/AGErrorReportModalTitle.vue +25 -0
  60. package/src/components/modals/AGLoadingModal.ts +23 -0
  61. package/src/components/modals/AGLoadingModal.vue +4 -8
  62. package/src/components/modals/AGModal.ts +1 -1
  63. package/src/components/modals/AGModal.vue +16 -13
  64. package/src/components/modals/AGModalTitle.vue +9 -0
  65. package/src/components/modals/AGPromptModal.ts +30 -0
  66. package/src/components/modals/AGPromptModal.vue +34 -0
  67. package/src/components/modals/index.ts +16 -7
  68. package/src/components/snackbars/AGSnackbar.vue +36 -0
  69. package/src/components/snackbars/index.ts +3 -0
  70. package/src/components/utils.ts +10 -0
  71. package/src/directives/index.ts +20 -3
  72. package/src/directives/measure.ts +21 -0
  73. package/src/errors/Errors.ts +65 -12
  74. package/src/errors/index.ts +26 -1
  75. package/src/errors/utils.ts +19 -0
  76. package/src/forms/Form.ts +15 -6
  77. package/src/jobs/Job.ts +5 -0
  78. package/src/jobs/index.ts +7 -0
  79. package/src/lang/Lang.ts +12 -24
  80. package/src/main.histoire.ts +1 -0
  81. package/src/main.ts +3 -2
  82. package/src/plugins/Plugin.ts +1 -0
  83. package/src/plugins/index.ts +19 -0
  84. package/src/services/App.state.ts +8 -4
  85. package/src/services/App.ts +35 -5
  86. package/src/services/Events.test.ts +39 -0
  87. package/src/services/Events.ts +100 -30
  88. package/src/services/Service.ts +55 -16
  89. package/src/services/index.ts +8 -4
  90. package/src/services/store.ts +8 -5
  91. package/src/testing/index.ts +25 -0
  92. package/src/ui/UI.state.ts +10 -1
  93. package/src/ui/UI.ts +160 -26
  94. package/src/ui/index.ts +12 -3
  95. package/src/utils/composition/events.ts +1 -0
  96. package/src/utils/index.ts +1 -0
  97. package/src/utils/markdown.ts +11 -2
  98. package/src/utils/tailwindcss.test.ts +26 -0
  99. package/src/utils/tailwindcss.ts +7 -0
  100. package/src/utils/vue.ts +15 -4
  101. package/tailwind.config.js +4 -0
  102. package/tsconfig.json +1 -0
  103. package/vite.config.ts +2 -1
  104. package/.eslintrc.js +0 -3
  105. package/src/components/basic/index.ts +0 -3
  106. package/src/globals.ts +0 -6
@@ -1,21 +1,23 @@
1
1
  <template>
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>
2
+ <div
3
+ id="aerogel-overlays-backdrop"
4
+ ref="$backdrop"
5
+ class="pointer-events-none fixed inset-0 z-50 bg-black/30 opacity-0"
6
+ />
7
+ <AGAppModals />
8
+ <AGAppSnackbars />
6
9
  </template>
7
10
 
8
11
  <script setup lang="ts">
9
- import { computed, ref } from 'vue';
12
+ import { ref } from 'vue';
10
13
 
11
- import UI from '@/ui/UI';
12
14
  import { useEvent } from '@/utils/composition/events';
13
15
 
14
- import AGModalContext from './modals/AGModalContext.vue';
16
+ import AGAppModals from './AGAppModals.vue';
17
+ import AGAppSnackbars from './AGAppSnackbars.vue';
15
18
 
16
19
  const $backdrop = ref<HTMLElement | null>(null);
17
20
  const backdropHidden = ref(true);
18
- const modal = computed(() => UI.modals[0] ?? null);
19
21
 
20
22
  useEvent('show-overlays-backdrop', async () => {
21
23
  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-end space-y-4">
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,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];
@@ -7,28 +7,38 @@
7
7
  <script setup lang="ts">
8
8
  import { computed } from 'vue';
9
9
 
10
- import { booleanProp } from '@/utils';
10
+ import { enumProp } from '@/utils/vue';
11
+ import { Colors } from '@/components/constants';
11
12
 
12
13
  import AGHeadlessButton from '../headless/forms/AGHeadlessButton.vue';
13
14
 
14
15
  const props = defineProps({
15
- clear: booleanProp(),
16
- secondary: booleanProp(),
16
+ color: enumProp(Colors, Colors.Primary),
17
17
  });
18
18
 
19
19
  const colorClasses = computed(() => {
20
- if (props.secondary) {
21
- return 'text-white bg-gray-600 hover:bg-gray-500 focus-visible:outline-offset-2 focus-visible:outline-gray-600';
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(' ');
22
42
  }
23
-
24
- if (props.clear) {
25
- return 'hover:bg-gray-500/20 focus-visible:outline-gray-500/60';
26
- }
27
-
28
- return [
29
- 'text-white bg-indigo-600',
30
- 'hover:bg-indigo-500',
31
- 'focus-visible:outline-offset-2 focus-visible:outline-indigo-600',
32
- ].join(' ');
33
43
  });
34
44
  </script>
@@ -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>
@@ -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"
@@ -19,18 +20,19 @@
19
20
  </template>
20
21
 
21
22
  <script setup lang="ts">
22
- import { componentRef, stringProp } from '@/utils/vue';
23
-
24
- import { useInputAttrs } from '@/utils';
23
+ import { componentRef } from '@/utils/vue';
24
+ import { useInputAttrs } from '@/utils/composition/forms';
25
+ import { useInputProps } from '@/components/headless/forms/AGHeadlessInput';
25
26
  import type { IAGHeadlessInput } from '@/components/headless/forms/AGHeadlessInput';
26
27
 
27
28
  import AGHeadlessInput from '../headless/forms/AGHeadlessInput.vue';
28
- import AGHeadlessInputInput from '../headless/forms/AGHeadlessInputInput.vue';
29
29
  import AGHeadlessInputError from '../headless/forms/AGHeadlessInputError.vue';
30
+ import AGHeadlessInputInput from '../headless/forms/AGHeadlessInputInput.vue';
31
+ import AGHeadlessInputLabel from '../headless/forms/AGHeadlessInputLabel.vue';
30
32
 
31
- defineProps({ name: stringProp() });
32
33
  defineOptions({ inheritAttrs: false });
33
34
 
35
+ const props = defineProps(useInputProps());
34
36
  const $input = componentRef<IAGHeadlessInput>();
35
37
  const [attrs, className] = useInputAttrs();
36
38
  </script>
@@ -0,0 +1,46 @@
1
+ <template>
2
+ <Story>
3
+ <div class="h-96">
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
+ />
11
+ </div>
12
+ </Story>
13
+ </template>
14
+
15
+ <script setup lang="ts">
16
+ import { ref } from 'vue';
17
+
18
+ import AGSelect from './AGSelect.vue';
19
+
20
+ const bestMugiwara = ref(null);
21
+ const bestFood = ref(null);
22
+ const bestMugiwaraLabel = 'Who\'s the best Mugiwara?';
23
+ const bestFoodLabel = 'What\'s the best food?';
24
+ const bestMugiwaraOptions = [
25
+ 'Monkey D. Luffy',
26
+ 'Roronoa Zoro',
27
+ 'Nami',
28
+ 'Usopp',
29
+ 'Sanji',
30
+ 'Tony Tony Chopper',
31
+ 'Nico Robin',
32
+ 'Franky',
33
+ 'Brook',
34
+ 'Jinbe',
35
+ ];
36
+ const bestFoodOptions = [
37
+ {
38
+ id: 'ramen',
39
+ name: 'Ramen',
40
+ },
41
+ {
42
+ id: 'pizza',
43
+ name: 'Pizza',
44
+ },
45
+ ];
46
+ </script>
@@ -0,0 +1,60 @@
1
+ <template>
2
+ <AGHeadlessSelect
3
+ v-bind="props"
4
+ ref="$select"
5
+ as="div"
6
+ @update:model-value="$emit('update:modelValue', $event)"
7
+ >
8
+ <AGHeadlessSelectLabel class="block text-sm font-medium leading-6 text-gray-900" />
9
+ <div class="relative" :class="{ 'mt-2': $select?.label }">
10
+ <AGHeadlessSelectButton
11
+ 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"
12
+ text-class="block truncate"
13
+ :class="{
14
+ 'ring-1 ring-red-500': $select?.errors,
15
+ }"
16
+ >
17
+ <template #icon>
18
+ <span class="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-2">
19
+ <IconCheveronDown class="h-5 w-5 text-gray-400" />
20
+ </span>
21
+ </template>
22
+ </AGHeadlessSelectButton>
23
+ <AGHeadlessSelectOptions
24
+ 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"
25
+ >
26
+ <AGHeadlessSelectOption
27
+ v-for="(option, index) in $select?.options ?? []"
28
+ :key="index"
29
+ :value="option"
30
+ class="relative block cursor-default select-none truncate py-2 pl-3 pr-9"
31
+ selected-class="font-semibold"
32
+ unselected-class="font-normal"
33
+ active-class="bg-indigo-600 text-white"
34
+ inactive-class="text-gray-900"
35
+ />
36
+ </AGHeadlessSelectOptions>
37
+ </div>
38
+ <AGHeadlessSelectError class="mt-2 text-sm text-red-600" />
39
+ </AGHeadlessSelect>
40
+ </template>
41
+
42
+ <script setup lang="ts">
43
+ import IconCheveronDown from '~icons/zondicons/cheveron-down';
44
+
45
+ import { componentRef } from '@/utils/vue';
46
+ import { useSelectEmits, useSelectProps } from '@/components/headless/forms/AGHeadlessSelect';
47
+ import type { IAGHeadlessSelect } from '@/components/headless/forms/AGHeadlessSelect';
48
+
49
+ import AGHeadlessSelect from '../headless/forms/AGHeadlessSelect.vue';
50
+ import AGHeadlessSelectButton from '../headless/forms/AGHeadlessSelectButton.vue';
51
+ import AGHeadlessSelectError from '../headless/forms/AGHeadlessSelectError.vue';
52
+ import AGHeadlessSelectLabel from '../headless/forms/AGHeadlessSelectLabel.vue';
53
+ import AGHeadlessSelectOption from '../headless/forms/AGHeadlessSelectOption.vue';
54
+ import AGHeadlessSelectOptions from '../headless/forms/AGHeadlessSelectOptions';
55
+
56
+ defineEmits(useSelectEmits());
57
+
58
+ const props = defineProps(useSelectProps());
59
+ const $select = componentRef<IAGHeadlessSelect>();
60
+ </script>
@@ -1,6 +1,5 @@
1
- import AGButton from './AGButton.vue';
2
- import AGCheckbox from './AGCheckbox.vue';
3
- import AGForm from './AGForm.vue';
4
- import AGInput from './AGInput.vue';
5
-
6
- export { AGButton, AGCheckbox, 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';
@@ -10,7 +10,8 @@ import { objectWithoutEmpty } from '@noeldemartin/utils';
10
10
 
11
11
  import { booleanProp, objectProp, stringProp } from '@/utils/vue';
12
12
 
13
- const { url, route, routeParams, routeQuery, submit } = defineProps({
13
+ const props = defineProps({
14
+ href: stringProp(),
14
15
  url: stringProp(),
15
16
  route: stringProp(),
16
17
  routeParams: objectProp(() => ({})),
@@ -19,32 +20,32 @@ const { url, route, routeParams, routeQuery, submit } = defineProps({
19
20
  });
20
21
 
21
22
  const component = computed(() => {
22
- if (route) {
23
+ if (props.route) {
23
24
  return {
24
25
  tag: 'router-link',
25
26
  props: {
26
27
  to: objectWithoutEmpty({
27
- name: route,
28
- params: routeParams,
29
- query: routeQuery,
28
+ name: props.route,
29
+ params: props.routeParams,
30
+ query: props.routeQuery,
30
31
  }),
31
32
  },
32
33
  };
33
34
  }
34
35
 
35
- if (url) {
36
+ if (props.href || props.url) {
36
37
  return {
37
38
  tag: 'a',
38
39
  props: {
39
40
  target: '_blank',
40
- href: url,
41
+ href: props.href || props.url,
41
42
  },
42
43
  };
43
44
  }
44
45
 
45
46
  return {
46
47
  tag: 'button',
47
- props: { type: submit ? 'submit' : 'button' },
48
+ props: { type: props.submit ? 'submit' : 'button' },
48
49
  };
49
50
  });
50
51
  </script>
@@ -1,8 +1,28 @@
1
- import type { ComputedRef, DeepReadonly, Ref } from 'vue';
1
+ import type { ComputedRef, DeepReadonly, ExtractPropTypes, Ref } from 'vue';
2
+
3
+ import { stringProp } from '@/utils';
4
+ import { extractComponentProps } from '@/components/utils';
2
5
 
3
6
  export interface IAGHeadlessInput {
4
7
  id: string;
8
+ name: ComputedRef<string | null>;
9
+ label: ComputedRef<string | null>;
5
10
  value: ComputedRef<string | number | boolean | null>;
6
11
  errors: DeepReadonly<Ref<string[] | null>>;
7
12
  update(value: string | number | boolean | null): void;
8
13
  }
14
+
15
+ export const inputProps = {
16
+ name: stringProp(),
17
+ label: stringProp(),
18
+ };
19
+
20
+ export function useInputProps(): typeof inputProps {
21
+ return inputProps;
22
+ }
23
+
24
+ export function extractInputProps<T extends ExtractPropTypes<typeof inputProps>>(
25
+ props: T,
26
+ ): Pick<T, keyof typeof inputProps> {
27
+ return extractComponentProps(props, inputProps);
28
+ }
@@ -12,13 +12,14 @@ import { uuid } from '@noeldemartin/utils';
12
12
  import { mixedProp, stringProp } from '@/utils/vue';
13
13
  import type Form from '@/forms/Form';
14
14
 
15
+ import { useInputProps } from './AGHeadlessInput';
15
16
  import type { IAGHeadlessInput } from './AGHeadlessInput';
16
17
 
17
18
  const emit = defineEmits(['update:modelValue']);
18
19
  const props = defineProps({
19
20
  as: stringProp('div'),
20
- name: stringProp(),
21
21
  modelValue: mixedProp<string | number | boolean>([String, Number, Boolean]),
22
+ ...useInputProps(),
22
23
  });
23
24
  const errors = computed(() => {
24
25
  if (!form || !props.name) {
@@ -28,11 +29,13 @@ const errors = computed(() => {
28
29
  return form.errors[props.name] ?? null;
29
30
  });
30
31
  const form = inject<Form | null>('form', null);
31
- const publicApi: IAGHeadlessInput = {
32
+ const api: IAGHeadlessInput = {
32
33
  id: `input-${uuid()}`,
34
+ name: computed(() => props.name),
35
+ label: computed(() => props.label),
33
36
  value: computed(() => {
34
37
  if (form && props.name) {
35
- return form.getFieldValue(props.name);
38
+ return form.getFieldValue(props.name) as string | number | boolean | null;
36
39
  }
37
40
 
38
41
  return props.modelValue;
@@ -49,6 +52,6 @@ const publicApi: IAGHeadlessInput = {
49
52
  },
50
53
  };
51
54
 
52
- provide<IAGHeadlessInput>('input', publicApi);
53
- defineExpose<IAGHeadlessInput>(publicApi);
55
+ provide<IAGHeadlessInput>('input', api);
56
+ defineExpose<IAGHeadlessInput>(api);
54
57
  </script>
@@ -2,6 +2,7 @@
2
2
  <input
3
3
  :id="input.id"
4
4
  ref="$input"
5
+ :name="name"
5
6
  :type="type"
6
7
  :value="value"
7
8
  :aria-invalid="input.errors ? 'true' : 'false'"
@@ -26,6 +27,7 @@ const input = injectReactiveOrFail<IAGHeadlessInput>(
26
27
  'input',
27
28
  '<AGHeadlessInputInput> must be a child of a <AGHeadlessInput>',
28
29
  );
30
+ const name = computed(() => input.name ?? undefined);
29
31
  const value = computed(() => input.value);
30
32
  const checked = computed(() => {
31
33
  if (props.type !== 'checkbox') {
@@ -1,10 +1,14 @@
1
1
  <template>
2
- <label :for="input.id">
3
- <slot />
2
+ <label v-if="show" :for="input.id">
3
+ <slot>
4
+ {{ input.label }}
5
+ </slot>
4
6
  </label>
5
7
  </template>
6
8
 
7
9
  <script setup lang="ts">
10
+ import { computed, useSlots } from 'vue';
11
+
8
12
  import { injectReactiveOrFail } from '@/utils/vue';
9
13
 
10
14
  import type { IAGHeadlessInput } from './AGHeadlessInput';
@@ -13,4 +17,6 @@ const input = injectReactiveOrFail<IAGHeadlessInput>(
13
17
  'input',
14
18
  '<AGHeadlessInputLabel> must be a child of a <AGHeadlessInput>',
15
19
  );
20
+ const slots = useSlots();
21
+ const show = computed(() => !!(input.label || slots.default));
16
22
  </script>
@@ -0,0 +1,42 @@
1
+ import type { ComputedRef, DeepReadonly, ExtractPropTypes, Ref } from 'vue';
2
+ import type { Writable } from '@noeldemartin/utils';
3
+
4
+ import { mixedProp, requiredArrayProp, stringProp } from '@/utils/vue';
5
+ import { extractComponentProps } from '@/components/utils';
6
+ import type { FormFieldValue } from '@/forms/Form';
7
+
8
+ export interface IAGHeadlessSelect {
9
+ id: string;
10
+ label: ComputedRef<string | null>;
11
+ noSelectionText: ComputedRef<string>;
12
+ buttonText: ComputedRef<string>;
13
+ renderText: ComputedRef<(value: FormFieldValue) => string>;
14
+ selectedOption: ComputedRef<FormFieldValue | null>;
15
+ options: ComputedRef<FormFieldValue[]>;
16
+ errors: DeepReadonly<Ref<string[] | null>>;
17
+ update(value: FormFieldValue): void;
18
+ }
19
+
20
+ export const selectProps = {
21
+ name: stringProp(),
22
+ label: stringProp(),
23
+ options: requiredArrayProp<FormFieldValue>(),
24
+ noSelectionText: stringProp(),
25
+ optionsText: mixedProp<string | ((option: FormFieldValue) => string)>(),
26
+ };
27
+
28
+ export const selectEmits = ['update:modelValue'] as const;
29
+
30
+ export function useSelectProps(): typeof selectProps {
31
+ return selectProps;
32
+ }
33
+
34
+ export function useSelectEmits(): Writable<typeof selectEmits> {
35
+ return [...selectEmits];
36
+ }
37
+
38
+ export function extractSelectProps<T extends ExtractPropTypes<typeof selectProps>>(
39
+ props: T,
40
+ ): Pick<T, keyof typeof selectProps> {
41
+ return extractComponentProps(props, selectProps);
42
+ }
@@ -0,0 +1,77 @@
1
+ <template>
2
+ <Listbox
3
+ v-slot="{ value, open, disabled }: ComponentProps"
4
+ :model-value="selectedOption"
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 { toString, uuid } from '@noeldemartin/utils';
14
+ import { Listbox } from '@headlessui/vue';
15
+
16
+ import { mixedProp } from '@/utils/vue';
17
+ import { translateWithDefault } from '@/lang/utils';
18
+ import type Form from '@/forms/Form';
19
+ import type { FormFieldValue } from '@/forms/Form';
20
+ import type { ComponentProps } from '@/utils/vue';
21
+
22
+ import { useSelectEmits, useSelectProps } from './AGHeadlessSelect';
23
+ import type { IAGHeadlessSelect } from './AGHeadlessSelect';
24
+
25
+ const emit = defineEmits(useSelectEmits());
26
+ const props = defineProps({
27
+ modelValue: mixedProp<FormFieldValue>(),
28
+ ...useSelectProps(),
29
+ });
30
+ const renderText = computed(() => {
31
+ if (typeof props.optionsText === 'function') {
32
+ return props.optionsText;
33
+ }
34
+
35
+ if (typeof props.optionsText === 'string') {
36
+ return (option: FormFieldValue): string => toString(option[props.optionsText as keyof FormFieldValue]);
37
+ }
38
+
39
+ return (option: FormFieldValue) => toString(option);
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));
44
+ const errors = computed(() => {
45
+ if (!form || !props.name) {
46
+ return null;
47
+ }
48
+
49
+ return form.errors[props.name] ?? null;
50
+ });
51
+
52
+ function update(value: FormFieldValue) {
53
+ if (form && props.name) {
54
+ form.setFieldValue(props.name, value);
55
+
56
+ return;
57
+ }
58
+
59
+ emit('update:modelValue', value);
60
+ }
61
+
62
+ const api: IAGHeadlessSelect = {
63
+ id: `select-${uuid()}`,
64
+ noSelectionText,
65
+ selectedOption,
66
+ errors,
67
+ options: computed(() => props.options),
68
+ label: computed(() => props.label),
69
+ buttonText: computed(() =>
70
+ selectedOption.value === null ? noSelectionText.value : renderText.value(selectedOption.value)),
71
+ renderText,
72
+ update,
73
+ };
74
+
75
+ provide<IAGHeadlessSelect>('select', api);
76
+ defineExpose<IAGHeadlessSelect>(api);
77
+ </script>
@@ -0,0 +1,24 @@
1
+ <template>
2
+ <ListboxButton v-slot="{ value, open, disabled }: ComponentProps">
3
+ <slot :value="value" :open="open" :disabled="disabled">
4
+ <span :class="textClass">{{ select?.buttonText }}</span>
5
+ </slot>
6
+ <slot name="icon" />
7
+ </ListboxButton>
8
+ </template>
9
+
10
+ <script setup lang="ts">
11
+ import { ListboxButton } from '@headlessui/vue';
12
+
13
+ import { injectReactiveOrFail, stringProp } from '@/utils/vue';
14
+ import type { ComponentProps } from '@/utils/vue';
15
+
16
+ import type { IAGHeadlessSelect } from './AGHeadlessSelect';
17
+
18
+ defineProps({ textClass: stringProp() });
19
+
20
+ const select = injectReactiveOrFail<IAGHeadlessSelect>(
21
+ 'select',
22
+ '<AGHeadlessSelectButton> must be a child of a <AGHeadlessSelect>',
23
+ );
24
+ </script>
@@ -0,0 +1,26 @@
1
+ <template>
2
+ <p v-if="errorMessage" :id="`${select.id}-error`">
3
+ {{ errorMessage }}
4
+ </p>
5
+ </template>
6
+
7
+ <script setup lang="ts">
8
+ import { computed } from 'vue';
9
+
10
+ import { injectReactiveOrFail } from '@/utils/vue';
11
+ import { translateWithDefault } from '@/lang/utils';
12
+
13
+ import type { IAGHeadlessSelect } from './AGHeadlessSelect';
14
+
15
+ const select = injectReactiveOrFail<IAGHeadlessSelect>(
16
+ 'select',
17
+ '<AGHeadlessSelectError> must be a child of a <AGHeadlessSelect>',
18
+ );
19
+ const errorMessage = computed(() => {
20
+ if (!select.errors) {
21
+ return null;
22
+ }
23
+
24
+ return translateWithDefault(`errors.${select.errors[0]}`, `Error: ${select.errors[0]}`);
25
+ });
26
+ </script>
@@ -0,0 +1,24 @@
1
+ <template>
2
+ <ListboxLabel v-if="show" v-slot="{ open, disabled }: ComponentProps">
3
+ <slot :open="open" :disabled="disabled">
4
+ {{ select.label }}
5
+ </slot>
6
+ </ListboxLabel>
7
+ </template>
8
+
9
+ <script setup lang="ts">
10
+ import { computed, useSlots } from 'vue';
11
+ import { ListboxLabel } from '@headlessui/vue';
12
+
13
+ import { injectReactiveOrFail } from '@/utils/vue';
14
+ import type { ComponentProps } from '@/utils/vue';
15
+
16
+ import type { IAGHeadlessSelect } from './AGHeadlessSelect';
17
+
18
+ const select = injectReactiveOrFail<IAGHeadlessSelect>(
19
+ 'select',
20
+ '<AGHeadlessSelectLabel> must be a child of a <AGHeadlessSelect>',
21
+ );
22
+ const slots = useSlots();
23
+ const show = computed(() => !!(select.label || slots.default));
24
+ </script>
@@ -0,0 +1,4 @@
1
+ export type IAGHeadlessSelectOptionSlotProps = {
2
+ active: boolean;
3
+ selected: boolean;
4
+ };