@ederzeel/nuxt-schema-form-nightly 0.1.0-29022487.1f5312c → 0.1.0-29027236.c108247

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.
@@ -1,5 +1,5 @@
1
1
  <script lang="ts" setup>
2
- import { computed } from 'vue'
2
+ import { computed, defineEmits } from 'vue'
3
3
 
4
4
  type Properties = {
5
5
  properties?: { [key: string]: unknown }
@@ -23,26 +23,26 @@ export type SComponentProps = {
23
23
 
24
24
  const props = withDefaults(defineProps<SComponentProps>(), { id: '', jsonSchemaPath: 'test' })
25
25
  const value = defineModel<unknown>({ required: true, default: '' })
26
+ const emit = defineEmits(['submit'])
26
27
 
27
28
  const options = computed(() => ({
28
29
  ...props,
29
- jsonSchemaPath: `${props.id}${props.jsonSchemaPath}`
30
+ jsonSchemaPath: `${props.jsonSchemaPath}.${props.id}`
30
31
  }))
32
+ const onSubmit = () => {
33
+ emit('submit')
34
+ }
31
35
  </script>
32
36
 
33
37
  <template>
34
38
  <div class="s-form-group">
35
- <component
36
- v-bind="options"
37
- :is="typeof renderer === 'string' ? renderer : undefined"
38
- v-if="renderer"
39
- v-model="value"
40
- />
39
+ <component v-if="renderer" v-bind="options" v-model="value"
40
+ :is="typeof renderer === 'string' ? renderer : undefined" @submit="onSubmit" />
41
41
 
42
- <SDate v-else-if="type === 'string' && format === 'date-time'" v-bind="options" v-model="value" />
42
+ <SDate v-else-if="type === 'string' && format === 'full-date'" v-bind="options" v-model="value" />
43
43
  <SInputField v-else-if="type === 'string'" v-bind="options" v-model="value" />
44
44
  <SToggle v-else-if="type === 'boolean'" v-bind="options" v-model="value" />
45
- <SObject v-else-if="type === 'object'" v-bind="options" v-model="value" />
45
+ <SObject v-else-if="type === 'object'" v-bind="options" v-model="value" @submit="onSubmit" />
46
46
  <div v-else>else</div>
47
47
  </div>
48
48
  </template>
@@ -1,7 +1,8 @@
1
1
  <script lang="ts" setup>
2
2
  import { format } from 'date-fns'
3
- import { computed, ref } from 'vue'
3
+ import { ref, watch } from 'vue'
4
4
  import DatePicker from './DatePicker.vue'
5
+
5
6
  const props = defineProps<{
6
7
  id: string
7
8
  title?: string
@@ -12,6 +13,9 @@ const props = defineProps<{
12
13
 
13
14
  const value = defineModel<string>({ required: true })
14
15
  const date = ref(new Date(value.value))
16
+ watch(date, () => {
17
+ value.value = date.value.toISOString()
18
+ })
15
19
  </script>
16
20
 
17
21
  <template>
@@ -4,7 +4,7 @@ import convertToYup from 'json-schema-yup-transformer'
4
4
  import type { JsonSchema } from 'json-schema-library'
5
5
  import { computed, ref } from 'vue'
6
6
 
7
- const props = defineProps<{ schema: JsonSchema }>()
7
+ const props = defineProps<{ schema: JsonSchema; customValidation?: CustomValidation[] }>()
8
8
 
9
9
  const value = defineModel<Record<string, unknown>>({ required: true })
10
10
  const formRef = ref()
@@ -17,6 +17,12 @@ async function onSubmit(_event: unknown) {
17
17
 
18
18
  const formValidationSchema = computed(() => convertToYup(props.schema))
19
19
 
20
+ export type CustomValidation = {
21
+ field: string
22
+ message: string
23
+ validate(state: any): boolean
24
+ }
25
+
20
26
  const validateFields = async (fields: string[]) => {
21
27
  let allValid = true
22
28
  for (const field of fields) {
@@ -25,14 +31,31 @@ const validateFields = async (fields: string[]) => {
25
31
  }
26
32
  }
27
33
 
34
+ if (props.customValidation) {
35
+ for (const { field, message, validate } of props.customValidation) {
36
+ if (!fields.includes(field)) continue
37
+ if (!validate(value.value)) {
38
+ const errors = [{ path: field, message }]
39
+ formRef.value.setErrors(errors, field)
40
+ allValid = false
41
+ }
42
+ }
43
+ }
44
+
28
45
  return allValid
29
46
  }
47
+
48
+ const submit = () => {
49
+ formRef.value.submit()
50
+ }
30
51
  </script>
31
52
 
32
53
  <template>
33
54
  <div>
55
+ <!-- {{ formRef ? formRef.errors : undefined }} -->
34
56
  <UForm ref="formRef" :state="value" :schema="formValidationSchema" autocomplete="on" @submit="onSubmit">
35
- <SComponent v-model="value" v-bind="schema" :validate-fields="validateFields" />
57
+ <SComponent v-model="value" v-bind="schema" :validate-fields="validateFields" @submit="submit"
58
+ :isRequired="true" />
36
59
  <slot />
37
60
 
38
61
  <slot name="submit">
@@ -15,7 +15,7 @@ const props = defineProps<{
15
15
  type PropertiesType = {
16
16
  [key: string]: unknown
17
17
  }
18
- const emit = defineEmits(['update:modelValue'])
18
+ const emit = defineEmits(['update:modelValue', 'submit'])
19
19
  const onInput = (value: unknown, key: string) => {
20
20
  emit('update:modelValue', { ...props.modelValue, [key]: value })
21
21
  }
@@ -36,6 +36,7 @@ const onInput = (value: unknown, key: string) => {
36
36
  :isRequired="required.includes(key)"
37
37
  class="mt-4"
38
38
  @update:model-value="event => onInput(event, key)"
39
+ @submit="() => emit('submit')"
39
40
  />
40
41
  </div>
41
42
  </template>
@@ -8,6 +8,8 @@ const props = defineProps<{
8
8
  jsonSchemaPath: string
9
9
  properties: PropertiesType
10
10
  modelValue: Record<string, unknown>
11
+ required: string[]
12
+ isRequired: boolean
11
13
  }>()
12
14
 
13
15
  type PropertiesType = {
@@ -31,6 +33,7 @@ const onInput = (value: unknown, key: string) => {
31
33
  :model-value="modelValue[key]"
32
34
  :json-schema-path="jsonSchemaPath?.length <= 0 ? `properties.${key}` : `${jsonSchemaPath}.properties.${key}`"
33
35
  v-bind="options"
36
+ :isRequired="required.includes(key)"
34
37
  class="flex flex-col flex-1"
35
38
  @update:model-value="event => onInput(event, key)"
36
39
  />
@@ -6,7 +6,7 @@ const props = defineProps<{
6
6
  title?: string
7
7
  description?: string
8
8
  enum: string[]
9
- enum_titles: string[]
9
+ enum_titles?: string[]
10
10
  isRequired: boolean
11
11
  }>()
12
12
 
@@ -17,7 +17,7 @@ const value = defineModel<string | number>({ required: true })
17
17
  :description="props.description"
18
18
  :name="props.id"
19
19
  >
20
- <UTextarea v-model="value" v-bind="properties" />
20
+ <UTextarea v-model="value" />
21
21
  </UFormGroup>
22
22
  </div>
23
23
  </template>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ederzeel/nuxt-schema-form-nightly",
3
- "version": "0.1.0-29022487.1f5312c",
3
+ "version": "0.1.0-29027236.c108247",
4
4
  "description": "A runtime form generator for nuxt",
5
5
  "type": "module",
6
6
  "exports": {
@@ -46,6 +46,7 @@
46
46
  "@nuxt/eslint-config": "^1.0.0",
47
47
  "@nuxt/kit": "^3.15.4",
48
48
  "@nuxt/module-builder": "^0.8.4",
49
+ "@nuxt/ui": "^2.21.0",
49
50
  "@types/eslint": "^9.6.1",
50
51
  "eslint": "^9.19.0",
51
52
  "eslint-config-prettier": "^10.0.1",