Files
speckle-server/packages/frontend-2/lib/form/composables/jsonRenderers.ts
T
Kristaps Fabians Geikins 83d8035dc2 chore: upgrade to eslint 9 (#2348)
* root + server

* frontend

* frontend-2

* dui3

* dui3

* tailwind theme

* ui-components

* preview service

* viewer

* viewer-sandbox

* fileimport-service

* webhook service

* objectloader

* shared

* ui-components-nuxt

* WIP full config

* WIP full linter

* eslint projectwide util

* minor fix

* removing redundant ci

* clean up test errors

* fixed prettier formatting

* CI improvements

* TSC lint fix

* 'buildBatch' needs to be async since some batch types (like Text) require it. Removed a disabled liniting rule from ObjLoader

* removed unnecessary void

---------

Co-authored-by: AlexandruPopovici <alexandrupopoviciioan@gmail.com>
2024-06-12 14:38:02 +03:00

90 lines
2.2 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
import type { useJsonFormsArrayControl, useJsonFormsControl } from '@jsonforms/vue'
import { useVanillaArrayControl } from '@jsonforms/vue-vanilla'
import { cloneDeep, merge } from 'lodash-es'
import { type RuleExpression } from 'vee-validate'
export function useJsonRendererBaseSetup<
I extends ReturnType<typeof useJsonFormsControl>
>(
input: I,
options?: {
onChangeValueConverter?: (val: any) => any
}
) {
const control = input.control as I['control']
const fieldName = computed(() => `/${control.value.path}`)
const label = computed(() => {
const base = control.value.label || fieldName.value
// If array field, just say "item"
if (/^\/?.+\.\d+$/i.exec(base)) {
return 'Item'
} else {
return base
}
})
const { onChangeValueConverter } = options || {}
const validator: RuleExpression<any> = () => {
if (control.value.errors) {
return `${label.value} ${control.value.errors}`
}
return true
}
const appliedOptions = computed(
() =>
merge(
{},
cloneDeep(control.value.config),
cloneDeep(control.value.uischema.options)
) as Record<string, any>
)
const isRequired = computed(() => !!control.value.required)
return {
label,
control,
handleChange: (value: any) => {
input.handleChange(
control.value.path,
onChangeValueConverter ? onChangeValueConverter(value) : value
)
},
validator,
appliedOptions,
fieldName,
validateOnValueUpdate: false,
isRequired
}
}
export function useJsonRendererArrayBaseSetup<
I extends ReturnType<typeof useJsonFormsArrayControl>
>(input: I) {
const control = input.control as I['control']
const fieldName = computed(() => `/${control.value.path}`)
const { appliedOptions, childUiSchema, childLabelForIndex } =
useVanillaArrayControl(input)
const isRequired = computed(() => !!control.value.required)
const error = computed(() =>
control.value.errors?.length
? `${control.value.label} ${control.value.errors}`
: null
)
return {
baseControl: input,
control,
appliedOptions,
fieldName,
childUiSchema,
childLabelForIndex,
isRequired,
error
}
}