fix: visible + enabled reactivity

This commit is contained in:
axolotle 2024-07-25 13:33:58 +02:00
parent 67ab4a2637
commit 5408b0553c
5 changed files with 15 additions and 15 deletions

View file

@ -1,6 +1,6 @@
<script setup lang="ts" generic="MV extends Obj, FFD extends FormFieldDict<MV>">
import { createReusableTemplate } from '@vueuse/core'
import { computed, reactive, toValue } from 'vue'
import { computed, toValue } from 'vue'
import { useI18n } from 'vue-i18n'
import type { FormValidation } from '@/composables/form'
@ -85,7 +85,7 @@ const sections = computed(() => {
if (!sections || !fields) return
return sections.map((section) => ({
...section,
fields: reactive(section.fields.map((id) => [id, fields[id]])) as {
fields: section.fields.map((id) => [id, fields[id]]) as {
[k in Extract<keyof FFD, string>]: [k, FFD[k]]
}[Extract<keyof FFD, string>][],
}))
@ -109,7 +109,7 @@ const Fields = createReusableTemplate<{
<template>
<Fields.define v-slot="{ fieldsProps }">
<template v-for="[k, field] in fieldsProps" :key="k">
<template v-if="field.visible ?? true">
<template v-if="toValue(field.visible) ?? true">
<slot
v-if="isWritableComponent<MV[typeof k]>(field)"
:name="`field:${k}`"
@ -174,7 +174,7 @@ const Fields = createReusableTemplate<{
<template v-for="section in sections" :key="section.id">
<Component
:is="section.name ? 'section' : 'div'"
v-if="section.visible"
v-if="toValue(section.visible)"
class="form-section"
>
<BCardTitle v-if="section.name" title-tag="h3">

View file

@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed } from 'vue'
import { computed, toValue } from 'vue'
import type { ButtonItemProps } from '@/types/form'
@ -30,7 +30,7 @@ const icon = computed(() => {
<BButton
:id="id"
:variant="type"
:disabled="!enabled"
:disabled="!toValue(enabled)"
class="d-block mb-3"
@click="emit('action', id)"
>

View file

@ -11,7 +11,7 @@ import { useRouter } from 'vue-router'
import { APIBadRequestError, APIError } from '@/api/errors'
import { deepSetErrors, useForm, type FormValidation } from '@/composables/form'
import { asUnreffed, isObjectLiteral } from '@/helpers/commons'
import { isObjectLiteral } from '@/helpers/commons'
import * as validators from '@/helpers/validators'
import { formatForm, formatI18nField } from '@/helpers/yunohostArguments'
import type { CustomRoute, KeyOfStr, MergeUnion, Obj } from '@/types/commons'
@ -324,14 +324,14 @@ export function formatConfigPanels<
function useExpression(
expression: JSExpression | undefined,
form: Ref<Obj>,
): boolean {
): boolean | ComputedRef<boolean> {
if (typeof expression === 'boolean') return expression
if (typeof expression === 'string') {
// FIXME normalize expression in core? ('', 'false', 'true') and rm next 2 lines
if (!expression || expression === 'true') return true
if (expression === 'false') return false
// FIXME remove asUnreffed and manage ref type?
return asUnreffed(useEvaluation(expression, form))
return useEvaluation(expression, form)
}
return true
}

View file

@ -1,4 +1,4 @@
import type { Ref } from 'vue'
import type { ComputedRef, Ref } from 'vue'
import type { Obj, KeyOfStr, CustomRoute } from '@/types/commons'
import type {
@ -56,7 +56,7 @@ export type ConfigSection<MV extends Obj, FFD extends FormFieldDict<MV>> = {
id: string
isActionSection: boolean
name?: string
visible: boolean
visible: boolean | ComputedRef<boolean>
}
export type ConfigPanel<

View file

@ -3,6 +3,7 @@ import type {
ValidationArgs,
ValidationRuleCollection,
} from '@vuelidate/core'
import type { ComputedRef } from 'vue'
import type { RouteLocationRaw } from 'vue-router'
import { isObjectLiteral } from '@/helpers/commons'
@ -19,7 +20,7 @@ type BaseDisplayItemProps = {
export type ButtonItemProps = BaseDisplayItemProps & {
// FIXME compute enabled JSExpression
enabled?: boolean
enabled?: boolean | ComputedRef<boolean>
icon?: string
type?: StateVariant
}
@ -239,8 +240,7 @@ type BaseFormField<C extends AnyItemComponents> = {
label?: string
props?: ItemComponentToItemProps[C]
readonly?: boolean
// FIXME compute visible JSExpression
visible?: boolean
visible?: boolean | ComputedRef<boolean>
}
export type FormField<
@ -274,7 +274,7 @@ export type FormFieldDisplay<
> = {
component: C
props: ItemComponentToItemProps[C]
visible?: boolean
visible?: boolean | ComputedRef<boolean>
hr?: boolean
readonly?: true
}