@aerogel/core 0.0.0-next.88c59e62f64db70aedfbc4c31b5bbc287be44483 → 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.
@@ -0,0 +1,7 @@
1
+ import { defineConfig } from 'histoire';
2
+ import { HstVue } from '@histoire/plugin-vue';
3
+
4
+ export default defineConfig({
5
+ setupFile: '/src/main.histoire.ts',
6
+ plugins: [HstVue()],
7
+ });
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.88c59e62f64db70aedfbc4c31b5bbc287be44483",
4
+ "version": "0.0.0-next.9487bb13082b9d479112445804d906125ded5cbc",
5
5
  "main": "dist/aerogel-core.cjs.js",
6
6
  "module": "dist/aerogel-core.esm.js",
7
7
  "types": "dist/aerogel-core.d.ts",
@@ -10,6 +10,8 @@
10
10
  "build": "rm dist -rf && npm run build:js && npm run build:types",
11
11
  "build:js": "noeldemartin-build-javascript",
12
12
  "build:types": "noeldemartin-build-types && cp src/types/virtual.d.ts dist",
13
+ "histoire:dev": "histoire dev",
14
+ "histoire:prod": "histoire build",
13
15
  "lint": "noeldemartin-lint src",
14
16
  "publish-next": "noeldemartin-publish-next",
15
17
  "test": "vitest --run"
@@ -42,6 +44,7 @@
42
44
  "devDependencies": {
43
45
  "@aerogel/vite": "*",
44
46
  "@types/dompurify": "^3.0.2",
45
- "@types/marked": "^5.0.0"
47
+ "@types/marked": "^5.0.0",
48
+ "tailwindcss": "^3.3.5"
46
49
  }
47
50
  }
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ plugins: {
3
+ tailwindcss: {},
4
+ autoprefixer: {},
5
+ },
6
+ };
@@ -0,0 +1,3 @@
1
+ @tailwind base;
2
+ @tailwind components;
3
+ @tailwind utilities;
@@ -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>
@@ -2,3 +2,4 @@ export { default as AGButton } from './AGButton.vue';
2
2
  export { default as AGCheckbox } from './AGCheckbox.vue';
3
3
  export { default as AGForm } from './AGForm.vue';
4
4
  export { default as AGInput } from './AGInput.vue';
5
+ export { default as AGSelect } from './AGSelect.vue';
@@ -28,7 +28,7 @@ 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) {
@@ -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>
@@ -1,17 +1,29 @@
1
- import type { ComputedRef } from 'vue';
1
+ import type { ComputedRef, DeepReadonly, Ref } from 'vue';
2
2
 
3
- import { requiredArrayProp } from '@/utils/vue';
3
+ import { requiredArrayProp, stringProp } from '@/utils/vue';
4
4
 
5
- export type SelectOptionValue = string | number | boolean | object | null;
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;
6
11
 
7
12
  export interface IAGHeadlessSelect {
8
- value: ComputedRef<SelectOptionValue | undefined>;
9
- options: SelectOptionValue[];
10
- update(value: string | number | boolean | null): void;
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;
11
21
  }
12
22
 
13
23
  export const selectProps = {
14
- options: requiredArrayProp<SelectOptionValue>(),
24
+ options: requiredArrayProp<IAGSelectOptionValue>(),
25
+ label: stringProp(),
26
+ noSelectionText: stringProp(),
15
27
  };
16
28
 
17
29
  export function useSelectProps(): typeof selectProps {
@@ -1,45 +1,77 @@
1
1
  <template>
2
- <Listbox :model-value="publicApi.value.value" @update:model-value="publicApi.update($event)">
3
- <slot />
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" />
4
8
  </Listbox>
5
9
  </template>
6
10
 
7
11
  <script setup lang="ts">
8
- import { computed, inject } from 'vue';
12
+ import { computed, inject, provide } from 'vue';
13
+ import { isObject, toString, uuid } from '@noeldemartin/utils';
9
14
  import { Listbox } from '@headlessui/vue';
10
15
 
11
16
  import { mixedProp, stringProp } from '@/utils/vue';
17
+ import { translateWithDefault } from '@/lang/utils';
12
18
  import type Form from '@/forms/Form';
19
+ import type { ComponentProps } from '@/utils/vue';
13
20
 
14
21
  import { useSelectProps } from './AGHeadlessSelect';
15
- import type { IAGHeadlessSelect, SelectOptionValue } from './AGHeadlessSelect';
22
+ import type { IAGHeadlessSelect, IAGSelectOption, IAGSelectOptionValue } from './AGHeadlessSelect';
16
23
 
17
24
  const emit = defineEmits(['update:modelValue']);
18
25
  const props = defineProps({
19
26
  name: stringProp(),
20
- modelValue: mixedProp<SelectOptionValue>(),
27
+ modelValue: mixedProp<IAGSelectOptionValue>(),
21
28
  ...useSelectProps(),
22
29
  });
23
30
  const form = inject<Form | null>('form', null);
24
- const publicApi: IAGHeadlessSelect = {
25
- value: computed(() => {
26
- if (form && props.name) {
27
- return form.getFieldValue(props.name);
28
- }
29
-
30
- return props.modelValue;
31
- }),
32
- options: props.options,
33
- update(value) {
34
- if (form && props.name) {
35
- form.setFieldValue(props.name, value);
36
-
37
- return;
38
- }
39
-
40
- emit('update:modelValue', value);
41
- },
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,
42
73
  };
43
74
 
44
- defineExpose<IAGHeadlessSelect>(publicApi);
75
+ provide<IAGHeadlessSelect>('select', api);
76
+ defineExpose<IAGHeadlessSelect>(api);
45
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,21 @@
1
+ <template>
2
+ <ListboxLabel v-if="select.label" 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 { ListboxLabel } from '@headlessui/vue';
11
+
12
+ import { injectReactiveOrFail } from '@/utils/vue';
13
+ import type { ComponentProps } from '@/utils/vue';
14
+
15
+ import type { IAGHeadlessSelect } from './AGHeadlessSelect';
16
+
17
+ const select = injectReactiveOrFail<IAGHeadlessSelect>(
18
+ 'select',
19
+ '<AGHeadlessSelectLabel> must be a child of a <AGHeadlessSelect>',
20
+ );
21
+ </script>
@@ -1,8 +1,4 @@
1
- import { ListboxOption } from '@headlessui/vue';
2
-
3
1
  export type IAGHeadlessSelectOptionSlotProps = {
4
2
  active: boolean;
5
3
  selected: boolean;
6
4
  };
7
-
8
- export default ListboxOption;
@@ -0,0 +1,39 @@
1
+ <template>
2
+ <ListboxOption v-slot="{ active, selected, disabled }: ComponentProps" :value="value" as="template">
3
+ <slot :active="active" :selected="selected" :disabled="disabled">
4
+ <li
5
+ :class="{
6
+ [activeClass ?? 'active']: active,
7
+ [inactiveClass ?? 'inactive']: !active,
8
+ [selectedClass ?? 'selected']: selected,
9
+ [unselectedClass ?? 'unselected']: !selected,
10
+ }"
11
+ >
12
+ {{ option?.text }}
13
+ </li>
14
+ </slot>
15
+ </ListboxOption>
16
+ </template>
17
+
18
+ <script setup lang="ts">
19
+ import { computed } from 'vue';
20
+ import { ListboxOption } from '@headlessui/vue';
21
+
22
+ import { injectReactiveOrFail, requiredMixedProp, stringProp } from '@/utils/vue';
23
+ import type { ComponentProps } from '@/utils/vue';
24
+
25
+ import type { IAGHeadlessSelect, IAGSelectOptionValue } from './AGHeadlessSelect';
26
+
27
+ const props = defineProps({
28
+ value: requiredMixedProp<IAGSelectOptionValue>(),
29
+ selectedClass: stringProp(),
30
+ unselectedClass: stringProp(),
31
+ activeClass: stringProp(),
32
+ inactiveClass: stringProp(),
33
+ });
34
+ const select = injectReactiveOrFail<IAGHeadlessSelect>(
35
+ 'select',
36
+ '<AGHeadlessSelectOption> must be a child of a <AGHeadlessSelect>',
37
+ );
38
+ const option = computed(() => select.options.find((selectOption) => selectOption.value === props.value));
39
+ </script>
@@ -1,12 +1,14 @@
1
1
  export * from './AGHeadlessInput';
2
2
  export * from './AGHeadlessSelect';
3
+ export * from './AGHeadlessSelectOption';
3
4
  export { default as AGHeadlessButton } from './AGHeadlessButton.vue';
4
5
  export { default as AGHeadlessInput } from './AGHeadlessInput.vue';
5
6
  export { default as AGHeadlessInputError } from './AGHeadlessInputError.vue';
6
7
  export { default as AGHeadlessInputInput } from './AGHeadlessInputInput.vue';
7
8
  export { default as AGHeadlessInputLabel } from './AGHeadlessInputLabel.vue';
8
9
  export { default as AGHeadlessSelect } from './AGHeadlessSelect.vue';
9
- export { default as AGHeadlessSelectButton } from './AGHeadlessSelectButton';
10
- export { default as AGHeadlessSelectLabel } from './AGHeadlessSelectLabel';
11
- export { default as AGHeadlessSelectOption, IAGHeadlessSelectOptionSlotProps } from './AGHeadlessSelectOption';
10
+ export { default as AGHeadlessSelectButton } from './AGHeadlessSelectButton.vue';
11
+ export { default as AGHeadlessSelectError } from './AGHeadlessSelectError.vue';
12
+ export { default as AGHeadlessSelectLabel } from './AGHeadlessSelectLabel.vue';
13
+ export { default as AGHeadlessSelectOption } from './AGHeadlessSelectOption.vue';
12
14
  export { default as AGHeadlessSelectOptions } from './AGHeadlessSelectOptions';
package/src/forms/Form.ts CHANGED
@@ -41,13 +41,14 @@ export type GetFormFieldValue<TType> = TType extends typeof FormFieldTypes.Strin
41
41
  ? object
42
42
  : never;
43
43
 
44
+ const validForms: WeakMap<Form, ComputedRef<boolean>> = new WeakMap();
45
+
44
46
  export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinitions> extends MagicObject {
45
47
 
46
48
  public errors: DeepReadonly<UnwrapNestedRefs<FormErrors<Fields>>>;
47
49
 
48
50
  private _fields: Fields;
49
51
  private _data: FormData<Fields>;
50
- private _valid: ComputedRef<boolean>;
51
52
  private _submitted: Ref<boolean>;
52
53
  private _errors: FormErrors<Fields>;
53
54
 
@@ -58,13 +59,17 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
58
59
  this._submitted = ref(false);
59
60
  this._data = this.getInitialData(fields);
60
61
  this._errors = this.getInitialErrors(fields);
61
- this._valid = computed(() => !Object.values(this._errors).some((error) => error !== null));
62
+
63
+ validForms.set(
64
+ this,
65
+ computed(() => !Object.values(this._errors).some((error) => error !== null)),
66
+ );
62
67
 
63
68
  this.errors = readonly(this._errors);
64
69
  }
65
70
 
66
71
  public get valid(): boolean {
67
- return this._valid.value;
72
+ return !!validForms.get(this)?.value;
68
73
  }
69
74
 
70
75
  public get submitted(): boolean {
@@ -0,0 +1 @@
1
+ import './assets/histoire.css';
package/src/utils/vue.ts CHANGED
@@ -108,7 +108,7 @@ export function requiredEnumProp<Enum extends Record<string, unknown>>(
108
108
  };
109
109
  }
110
110
 
111
- export function requiredMixedProp<T>(type: PropType<T>): RequiredProp<T> {
111
+ export function requiredMixedProp<T>(type?: PropType<T>): RequiredProp<T> {
112
112
  return {
113
113
  type,
114
114
  required: true,
@@ -0,0 +1,4 @@
1
+ /** @type {import('tailwindcss').Config} */
2
+ module.exports = {
3
+ content: ['./src/**/*.{vue,ts}'],
4
+ };
@@ -1,3 +0,0 @@
1
- import { ListboxButton } from '@headlessui/vue';
2
-
3
- export default ListboxButton;
@@ -1,3 +0,0 @@
1
- import { ListboxLabel } from '@headlessui/vue';
2
-
3
- export default ListboxLabel;