@duffcloudservices/site-forms 0.1.0

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 (51) hide show
  1. package/README.md +213 -0
  2. package/dist/DcsForm.vue.d.ts +67 -0
  3. package/dist/composables/useDcsForm.d.ts +36 -0
  4. package/dist/composables/useFormSubmission.d.ts +18 -0
  5. package/dist/composables/useFormValidation.d.ts +19 -0
  6. package/dist/fields/DcsFormCheckbox.vue.d.ts +12 -0
  7. package/dist/fields/DcsFormCheckboxGroup.vue.d.ts +12 -0
  8. package/dist/fields/DcsFormDate.vue.d.ts +14 -0
  9. package/dist/fields/DcsFormFieldWrapper.vue.d.ts +27 -0
  10. package/dist/fields/DcsFormFile.vue.d.ts +12 -0
  11. package/dist/fields/DcsFormHidden.vue.d.ts +7 -0
  12. package/dist/fields/DcsFormHtmlBlock.vue.d.ts +6 -0
  13. package/dist/fields/DcsFormRadio.vue.d.ts +12 -0
  14. package/dist/fields/DcsFormSection.vue.d.ts +21 -0
  15. package/dist/fields/DcsFormSelect.vue.d.ts +35 -0
  16. package/dist/fields/DcsFormText.vue.d.ts +34 -0
  17. package/dist/fields/DcsFormTextarea.vue.d.ts +34 -0
  18. package/dist/index.d.ts +22 -0
  19. package/dist/index.js +918 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/loaders/yaml.d.ts +12 -0
  22. package/dist/schema/validate.d.ts +9 -0
  23. package/dist/types.d.ts +106 -0
  24. package/package.json +73 -0
  25. package/src/DcsForm.vue +299 -0
  26. package/src/__tests__/fields.test.ts +82 -0
  27. package/src/__tests__/multi-step.test.ts +46 -0
  28. package/src/__tests__/schema.test.ts +42 -0
  29. package/src/__tests__/submission.test.ts +77 -0
  30. package/src/__tests__/visible-if.test.ts +111 -0
  31. package/src/composables/useDcsForm.ts +201 -0
  32. package/src/composables/useFormSubmission.ts +113 -0
  33. package/src/composables/useFormValidation.ts +127 -0
  34. package/src/fields/DcsFormCheckbox.vue +35 -0
  35. package/src/fields/DcsFormCheckboxGroup.vue +52 -0
  36. package/src/fields/DcsFormDate.vue +34 -0
  37. package/src/fields/DcsFormFieldWrapper.vue +39 -0
  38. package/src/fields/DcsFormFile.vue +38 -0
  39. package/src/fields/DcsFormHidden.vue +17 -0
  40. package/src/fields/DcsFormHtmlBlock.vue +19 -0
  41. package/src/fields/DcsFormRadio.vue +45 -0
  42. package/src/fields/DcsFormSection.vue +19 -0
  43. package/src/fields/DcsFormSelect.vue +62 -0
  44. package/src/fields/DcsFormText.vue +54 -0
  45. package/src/fields/DcsFormTextarea.vue +43 -0
  46. package/src/index.ts +51 -0
  47. package/src/loaders/yaml.ts +51 -0
  48. package/src/schema/form-definition.schema.json +633 -0
  49. package/src/schema/validate.ts +58 -0
  50. package/src/shims.d.ts +10 -0
  51. package/src/types.ts +140 -0
@@ -0,0 +1,45 @@
1
+ <script setup lang="ts">
2
+ import { computed } from 'vue'
3
+ import type { PortalFormField } from '../types'
4
+ import DcsFormFieldWrapper from './DcsFormFieldWrapper.vue'
5
+
6
+ const props = defineProps<{
7
+ field: PortalFormField
8
+ modelValue: string | undefined
9
+ error?: string
10
+ }>()
11
+
12
+ const emit = defineEmits<{
13
+ 'update:modelValue': [value: string]
14
+ }>()
15
+
16
+ const inputId = computed(() => `field-${props.field.id}`)
17
+ const options = computed(() => props.field.options ?? [])
18
+ </script>
19
+
20
+ <template>
21
+ <DcsFormFieldWrapper :field="field" :error="error" :input-id="inputId">
22
+ <fieldset
23
+ class="dcs-form-radio-group"
24
+ role="radiogroup"
25
+ :aria-required="field.required"
26
+ :aria-invalid="!!error"
27
+ >
28
+ <legend class="sr-only">{{ field.label }}</legend>
29
+ <label
30
+ v-for="opt in options"
31
+ :key="opt.value"
32
+ class="dcs-form-radio"
33
+ >
34
+ <input
35
+ type="radio"
36
+ :name="field.id"
37
+ :value="opt.value"
38
+ :checked="modelValue === opt.value"
39
+ @change="emit('update:modelValue', opt.value)"
40
+ />
41
+ <span>{{ opt.label }}</span>
42
+ </label>
43
+ </fieldset>
44
+ </DcsFormFieldWrapper>
45
+ </template>
@@ -0,0 +1,19 @@
1
+ <script setup lang="ts">
2
+ import type { PortalFormField } from '../types'
3
+
4
+ defineProps<{
5
+ field: PortalFormField
6
+ }>()
7
+ </script>
8
+
9
+ <template>
10
+ <div
11
+ class="dcs-form-section"
12
+ :data-form-field-key="field.id"
13
+ >
14
+ <slot>
15
+ <h3 class="dcs-form-section__heading">{{ field.label }}</h3>
16
+ <p v-if="field.helpText" class="dcs-form-section__help">{{ field.helpText }}</p>
17
+ </slot>
18
+ </div>
19
+ </template>
@@ -0,0 +1,62 @@
1
+ <script setup lang="ts">
2
+ import { computed } from 'vue'
3
+ import type { PortalFormField } from '../types'
4
+ import DcsFormFieldWrapper from './DcsFormFieldWrapper.vue'
5
+
6
+ const props = defineProps<{
7
+ field: PortalFormField
8
+ modelValue: string | string[] | undefined
9
+ error?: string
10
+ }>()
11
+
12
+ const emit = defineEmits<{
13
+ 'update:modelValue': [value: string | string[]]
14
+ blur: []
15
+ }>()
16
+
17
+ const inputId = computed(() => `field-${props.field.id}`)
18
+ const isMulti = computed(() => props.field.type === 'multiselect')
19
+ const options = computed(() => props.field.options ?? [])
20
+
21
+ function onChange(e: Event): void {
22
+ const sel = e.target as HTMLSelectElement
23
+ if (isMulti.value) {
24
+ const values: string[] = []
25
+ for (const opt of Array.from(sel.selectedOptions)) values.push(opt.value)
26
+ emit('update:modelValue', values)
27
+ } else {
28
+ emit('update:modelValue', sel.value)
29
+ }
30
+ }
31
+ </script>
32
+
33
+ <template>
34
+ <DcsFormFieldWrapper :field="field" :error="error" :input-id="inputId">
35
+ <slot
36
+ name="input"
37
+ :id="inputId"
38
+ :value="modelValue"
39
+ :options="options"
40
+ :on-change="onChange"
41
+ >
42
+ <select
43
+ :id="inputId"
44
+ class="dcs-form-input dcs-form-select"
45
+ :name="field.id"
46
+ :required="field.required"
47
+ :multiple="isMulti"
48
+ :aria-invalid="!!error"
49
+ :value="modelValue ?? (isMulti ? [] : '')"
50
+ @change="onChange"
51
+ @blur="emit('blur')"
52
+ >
53
+ <option v-if="!isMulti" value="" disabled>
54
+ {{ field.placeholder ?? 'Select…' }}
55
+ </option>
56
+ <option v-for="opt in options" :key="opt.value" :value="opt.value">
57
+ {{ opt.label }}
58
+ </option>
59
+ </select>
60
+ </slot>
61
+ </DcsFormFieldWrapper>
62
+ </template>
@@ -0,0 +1,54 @@
1
+ <script setup lang="ts">
2
+ import { computed } from 'vue'
3
+ import type { PortalFormField } from '../types'
4
+ import DcsFormFieldWrapper from './DcsFormFieldWrapper.vue'
5
+
6
+ const props = defineProps<{
7
+ field: PortalFormField
8
+ modelValue: string | number | undefined
9
+ error?: string
10
+ }>()
11
+
12
+ const emit = defineEmits<{
13
+ 'update:modelValue': [value: string]
14
+ blur: []
15
+ }>()
16
+
17
+ const inputId = computed(() => `field-${props.field.id}`)
18
+ const inputType = computed(() => {
19
+ switch (props.field.type) {
20
+ case 'email':
21
+ return 'email'
22
+ case 'tel':
23
+ return 'tel'
24
+ default:
25
+ return 'text'
26
+ }
27
+ })
28
+ </script>
29
+
30
+ <template>
31
+ <DcsFormFieldWrapper :field="field" :error="error" :input-id="inputId">
32
+ <slot
33
+ name="input"
34
+ :id="inputId"
35
+ :value="modelValue"
36
+ :on-input="(e: Event) => emit('update:modelValue', (e.target as HTMLInputElement).value)"
37
+ :on-blur="() => emit('blur')"
38
+ >
39
+ <input
40
+ :id="inputId"
41
+ class="dcs-form-input"
42
+ :type="inputType"
43
+ :name="field.id"
44
+ :value="modelValue ?? ''"
45
+ :placeholder="field.placeholder"
46
+ :required="field.required"
47
+ :aria-invalid="!!error"
48
+ :aria-describedby="error ? `${inputId}-error` : undefined"
49
+ @input="emit('update:modelValue', ($event.target as HTMLInputElement).value)"
50
+ @blur="emit('blur')"
51
+ />
52
+ </slot>
53
+ </DcsFormFieldWrapper>
54
+ </template>
@@ -0,0 +1,43 @@
1
+ <script setup lang="ts">
2
+ import { computed } from 'vue'
3
+ import type { PortalFormField } from '../types'
4
+ import DcsFormFieldWrapper from './DcsFormFieldWrapper.vue'
5
+
6
+ const props = defineProps<{
7
+ field: PortalFormField
8
+ modelValue: string | undefined
9
+ error?: string
10
+ }>()
11
+
12
+ const emit = defineEmits<{
13
+ 'update:modelValue': [value: string]
14
+ blur: []
15
+ }>()
16
+
17
+ const inputId = computed(() => `field-${props.field.id}`)
18
+ </script>
19
+
20
+ <template>
21
+ <DcsFormFieldWrapper :field="field" :error="error" :input-id="inputId">
22
+ <slot
23
+ name="input"
24
+ :id="inputId"
25
+ :value="modelValue"
26
+ :on-input="(e: Event) => emit('update:modelValue', (e.target as HTMLTextAreaElement).value)"
27
+ :on-blur="() => emit('blur')"
28
+ >
29
+ <textarea
30
+ :id="inputId"
31
+ class="dcs-form-input dcs-form-textarea"
32
+ :name="field.id"
33
+ :placeholder="field.placeholder"
34
+ :required="field.required"
35
+ :aria-invalid="!!error"
36
+ rows="5"
37
+ :value="modelValue ?? ''"
38
+ @input="emit('update:modelValue', ($event.target as HTMLTextAreaElement).value)"
39
+ @blur="emit('blur')"
40
+ />
41
+ </slot>
42
+ </DcsFormFieldWrapper>
43
+ </template>
package/src/index.ts ADDED
@@ -0,0 +1,51 @@
1
+ export { default as DcsForm } from './DcsForm.vue'
2
+ export { default as DcsFormText } from './fields/DcsFormText.vue'
3
+ export { default as DcsFormTextarea } from './fields/DcsFormTextarea.vue'
4
+ export { default as DcsFormSelect } from './fields/DcsFormSelect.vue'
5
+ export { default as DcsFormRadio } from './fields/DcsFormRadio.vue'
6
+ export { default as DcsFormCheckboxGroup } from './fields/DcsFormCheckboxGroup.vue'
7
+ export { default as DcsFormCheckbox } from './fields/DcsFormCheckbox.vue'
8
+ export { default as DcsFormDate } from './fields/DcsFormDate.vue'
9
+ export { default as DcsFormFile } from './fields/DcsFormFile.vue'
10
+ export { default as DcsFormHidden } from './fields/DcsFormHidden.vue'
11
+ export { default as DcsFormSection } from './fields/DcsFormSection.vue'
12
+ export { default as DcsFormHtmlBlock } from './fields/DcsFormHtmlBlock.vue'
13
+ export { default as DcsFormFieldWrapper } from './fields/DcsFormFieldWrapper.vue'
14
+
15
+ export { useDcsForm } from './composables/useDcsForm'
16
+ export type { UseDcsFormOptions, UseDcsFormReturn } from './composables/useDcsForm'
17
+ export {
18
+ validateField,
19
+ validateForm,
20
+ isFieldVisible,
21
+ hasErrors,
22
+ } from './composables/useFormValidation'
23
+ export { submitFormValues } from './composables/useFormSubmission'
24
+ export type { SubmitOptions } from './composables/useFormSubmission'
25
+
26
+ export { loadFormDefinitions, parseFormYaml } from './loaders/yaml'
27
+ export {
28
+ validateFormDefinition,
29
+ warnIfInvalid,
30
+ } from './schema/validate'
31
+ export type { SchemaValidationResult } from './schema/validate'
32
+
33
+ export type {
34
+ DcsFormSubmitPayload,
35
+ DcsFormSubmitSuccess,
36
+ DcsFormSubmitError,
37
+ FormErrors,
38
+ FormValues,
39
+ PortalFormDefinition,
40
+ PortalFormField,
41
+ PortalFormFieldType,
42
+ PortalFormFieldOption,
43
+ PortalFormFieldValidation,
44
+ PortalFormFieldVisibleIf,
45
+ PortalFormFieldWidth,
46
+ PortalFormStep,
47
+ PortalFormSubmissionConfig,
48
+ PortalFormSubmissionLeadConfig,
49
+ PortalFormSubmissionEmailConfig,
50
+ PortalFormSubmissionWebhookConfig,
51
+ } from './types'
@@ -0,0 +1,51 @@
1
+ import yaml from 'js-yaml'
2
+ import type { PortalFormDefinition } from '../types'
3
+
4
+ /**
5
+ * Eagerly load every form YAML under `/.dcs/forms/*.yaml` from the
6
+ * consuming Vite app and parse them into PortalFormDefinition objects.
7
+ *
8
+ * Vite returns the modules as raw strings (when using `?raw` or the
9
+ * default text loader for unknown extensions) or as parsed objects
10
+ * (when `vite-plugin-yaml` is installed). This helper handles both.
11
+ */
12
+ export function loadFormDefinitions(
13
+ modules: Record<string, unknown>,
14
+ ): Record<string, PortalFormDefinition> {
15
+ const out: Record<string, PortalFormDefinition> = {}
16
+ for (const [path, mod] of Object.entries(modules)) {
17
+ const formId = extractFormId(path)
18
+ const def = parseModule(mod)
19
+ if (def) {
20
+ out[def.formId ?? formId] = def
21
+ }
22
+ }
23
+ return out
24
+ }
25
+
26
+ function extractFormId(path: string): string {
27
+ const file = path.split('/').pop() ?? path
28
+ return file.replace(/\.ya?ml$/i, '')
29
+ }
30
+
31
+ function parseModule(mod: unknown): PortalFormDefinition | null {
32
+ if (!mod) return null
33
+ if (typeof mod === 'string') {
34
+ return yaml.load(mod) as PortalFormDefinition
35
+ }
36
+ if (typeof mod === 'object') {
37
+ // vite-plugin-yaml returns parsed objects, possibly wrapped in
38
+ // `{ default: ... }` when imported via `import: 'default'`.
39
+ const maybeDefault = (mod as { default?: unknown }).default
40
+ if (maybeDefault && typeof maybeDefault === 'object') {
41
+ return maybeDefault as PortalFormDefinition
42
+ }
43
+ return mod as PortalFormDefinition
44
+ }
45
+ return null
46
+ }
47
+
48
+ /** Parse a single raw YAML string into a PortalFormDefinition. */
49
+ export function parseFormYaml(raw: string): PortalFormDefinition {
50
+ return yaml.load(raw) as PortalFormDefinition
51
+ }