@volverjs/form-vue 1.0.0-beta.5 → 1.0.0-beta.7

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.
package/src/VvForm.ts CHANGED
@@ -9,12 +9,13 @@ import {
9
9
  defineComponent,
10
10
  ref,
11
11
  provide,
12
- readonly,
12
+ readonly as makeReadonly,
13
13
  watch,
14
14
  h,
15
15
  toRaw,
16
16
  isProxy,
17
17
  computed,
18
+ onMounted,
18
19
  } from 'vue'
19
20
  import {
20
21
  watchIgnorable,
@@ -41,8 +42,12 @@ export const defineForm = <Schema extends FormSchema>(
41
42
  const status = ref<FormStatus | undefined>()
42
43
  const invalid = computed(() => status.value === FormStatus.invalid)
43
44
  const formData = ref<Partial<z.infer<Schema> | undefined>>()
45
+ const readonly = ref<boolean>(false)
44
46
 
45
47
  const validate = async (value = formData.value) => {
48
+ if (readonly.value) {
49
+ return true
50
+ }
46
51
  const parseResult = await schema.safeParseAsync(value)
47
52
  if (!parseResult.success) {
48
53
  errors.value = parseResult.error.format() as ZodFormattedError<
@@ -58,6 +63,9 @@ export const defineForm = <Schema extends FormSchema>(
58
63
  }
59
64
 
60
65
  const submit = async () => {
66
+ if (readonly.value) {
67
+ return false
68
+ }
61
69
  if (!(await validate())) {
62
70
  return false
63
71
  }
@@ -87,6 +95,10 @@ export const defineForm = <Schema extends FormSchema>(
87
95
  type: Object,
88
96
  default: () => ({}),
89
97
  },
98
+ readonly: {
99
+ type: Boolean,
100
+ default: options?.readonly ?? false,
101
+ },
90
102
  tag: {
91
103
  type: String,
92
104
  default: 'form',
@@ -96,8 +108,22 @@ export const defineForm = <Schema extends FormSchema>(
96
108
  default: undefined,
97
109
  },
98
110
  },
99
- emits: ['invalid', 'valid', 'submit', 'update:modelValue'],
100
- expose: ['submit', 'validate', 'errors', 'status', 'valid', 'invalid'],
111
+ emits: [
112
+ 'invalid',
113
+ 'valid',
114
+ 'submit',
115
+ 'update:modelValue',
116
+ 'update:readonly',
117
+ ],
118
+ expose: [
119
+ 'submit',
120
+ 'validate',
121
+ 'errors',
122
+ 'status',
123
+ 'valid',
124
+ 'invalid',
125
+ 'readonly',
126
+ ],
101
127
  setup(props, { emit }) {
102
128
  formData.value = defaultObjectBySchema(
103
129
  schema,
@@ -111,6 +137,14 @@ export const defineForm = <Schema extends FormSchema>(
111
137
  const original = isProxy(newValue)
112
138
  ? toRaw(newValue)
113
139
  : newValue
140
+
141
+ if (
142
+ JSON.stringify(original) ===
143
+ JSON.stringify(toRaw(formData.value))
144
+ ) {
145
+ return
146
+ }
147
+
114
148
  formData.value =
115
149
  typeof original?.clone === 'function'
116
150
  ? original.clone()
@@ -168,15 +202,34 @@ export const defineForm = <Schema extends FormSchema>(
168
202
  }
169
203
  })
170
204
 
205
+ onMounted(() => {
206
+ if (props.readonly && !readonly.value) {
207
+ readonly.value = props.readonly
208
+ }
209
+ })
210
+
211
+ watch(
212
+ () => props.readonly,
213
+ (newValue) => {
214
+ readonly.value = newValue
215
+ },
216
+ )
217
+ watch(readonly, (newValue) => {
218
+ if (newValue !== props.readonly) {
219
+ emit('update:readonly', readonly.value)
220
+ }
221
+ })
222
+
171
223
  provide(provideKey, {
172
224
  formData,
173
225
  submit,
174
226
  validate,
175
227
  ignoreUpdates,
176
228
  stopUpdatesWatch,
177
- errors: readonly(errors),
178
- status: readonly(status),
229
+ errors: makeReadonly(errors),
230
+ status: makeReadonly(status),
179
231
  invalid,
232
+ readonly,
180
233
  })
181
234
 
182
235
  return {
@@ -185,9 +238,10 @@ export const defineForm = <Schema extends FormSchema>(
185
238
  validate,
186
239
  ignoreUpdates,
187
240
  stopUpdatesWatch,
188
- errors: readonly(errors),
189
- status: readonly(status),
241
+ errors: makeReadonly(errors),
242
+ status: makeReadonly(status),
190
243
  invalid,
244
+ isReadonly: readonly,
191
245
  }
192
246
  },
193
247
  render() {
@@ -201,6 +255,7 @@ export const defineForm = <Schema extends FormSchema>(
201
255
  errors: this.errors,
202
256
  status: this.status,
203
257
  invalid: this.invalid,
258
+ readonly: this.isReadonly,
204
259
  }) ?? this.$slots.default
205
260
  return h(
206
261
  this.tag,
@@ -229,6 +284,7 @@ export const defineForm = <Schema extends FormSchema>(
229
284
  errors,
230
285
  status,
231
286
  invalid,
287
+ readonly,
232
288
  formData,
233
289
  validate,
234
290
  submit,
@@ -255,6 +311,7 @@ export const defineForm = <Schema extends FormSchema>(
255
311
  >
256
312
  status: Ref<DeepReadonly<`${FormStatus}` | undefined>>
257
313
  invalid: Ref<DeepReadonly<boolean>>
314
+ readonly: Ref<boolean>
258
315
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
259
316
  }) => any
260
317
  }
@@ -81,6 +81,10 @@ export const defineFormField = <Schema extends FormSchema>(
81
81
  type: Boolean,
82
82
  default: false,
83
83
  },
84
+ readonly: {
85
+ type: Boolean,
86
+ default: undefined,
87
+ },
84
88
  },
85
89
  emits: ['invalid', 'valid', 'update:formData', 'update:modelValue'],
86
90
  expose: ['invalid', 'invalidLabel', 'errors'],
@@ -186,6 +190,15 @@ export const defineFormField = <Schema extends FormSchema>(
186
190
  {},
187
191
  )
188
192
  })
193
+ const isReadonly = computed(() => {
194
+ const fieldReadonly =
195
+ hasFieldProps.value.readonly ?? props.readonly
196
+ if (fieldReadonly === undefined) {
197
+ return injectedFormData?.readonly.value
198
+ }
199
+ return fieldReadonly
200
+ })
201
+
189
202
  const hasProps = computed(() => ({
190
203
  ...hasFieldProps.value,
191
204
  name: hasFieldProps.value.name ?? props.name,
@@ -217,6 +230,7 @@ export const defineFormField = <Schema extends FormSchema>(
217
230
  })(props.type as FormFieldType),
218
231
  invalidLabel: invalidLabel.value,
219
232
  modelValue: modelValue.value,
233
+ readonly: isReadonly.value,
220
234
  'onUpdate:modelValue': onUpdate,
221
235
  }))
222
236
 
@@ -240,6 +254,7 @@ export const defineFormField = <Schema extends FormSchema>(
240
254
  formData: injectedFormData?.formData.value,
241
255
  formErrors: injectedFormData?.errors.value,
242
256
  errors: errors.value,
257
+ readonly: isReadonly.value,
243
258
  }) ?? slots.defalut
244
259
  )
245
260
  },
package/src/index.ts CHANGED
@@ -53,6 +53,7 @@ const _formFactory = <Schema extends FormSchema>(
53
53
  errors,
54
54
  status,
55
55
  invalid,
56
+ readonly,
56
57
  formData,
57
58
  validate,
58
59
  submit,
@@ -71,6 +72,7 @@ const _formFactory = <Schema extends FormSchema>(
71
72
  errors,
72
73
  status,
73
74
  invalid,
75
+ readonly,
74
76
  formData,
75
77
  validate,
76
78
  submit,
package/src/types.ts CHANGED
@@ -16,6 +16,7 @@ export type FormFieldComponentOptions = {
16
16
  export type FormComponentOptions<Schema> = {
17
17
  updateThrottle?: number
18
18
  continuosValidation?: boolean
19
+ readonly?: boolean
19
20
  template?: Schema extends FormSchema ? FormTemplate<Schema> : never
20
21
  onUpdate?: Schema extends FormSchema
21
22
  ? (data: Partial<z.infer<Schema> | undefined>) => void
@@ -52,6 +53,7 @@ export type InjectedFormData<Schema extends FormSchema> = {
52
53
  stopUpdatesWatch: WatchStopHandle
53
54
  status: Readonly<Ref<FormStatus | undefined>>
54
55
  invalid: Readonly<Ref<boolean>>
56
+ readonly: Ref<boolean>
55
57
  }
56
58
 
57
59
  export type InjectedFormWrapperData<Schema extends FormSchema> = {
@@ -107,17 +109,17 @@ export type PathValue<T, TPath extends Path<T> | Path<T>[]> = T extends any
107
109
  : PathValue<T[K], R>
108
110
  : never
109
111
  : K extends `${number}`
110
- ? T extends readonly (infer V)[]
111
- ? PathValue<V, R & Path<V>>
112
- : never
113
- : never
112
+ ? T extends readonly (infer V)[]
113
+ ? PathValue<V, R & Path<V>>
114
+ : never
115
+ : never
114
116
  : TPath extends keyof T
115
- ? T[TPath]
116
- : TPath extends `${number}`
117
- ? T extends readonly (infer V)[]
118
- ? V
119
- : never
120
- : never
117
+ ? T[TPath]
118
+ : TPath extends `${number}`
119
+ ? T extends readonly (infer V)[]
120
+ ? V
121
+ : never
122
+ : never
121
123
  : never
122
124
 
123
125
  export type AnyBoolean<Schema extends FormSchema> =