From ca28b9650536962afd9494adb0d89c8283de75ad Mon Sep 17 00:00:00 2001 From: Alban Mouton Date: Tue, 17 Oct 2023 10:24:34 +0200 Subject: [PATCH] feat: work on readOnly calculated values --- doc/examples/dev/_calculated-value.js | 28 +++++++++++++++++++++++++++ doc/examples/dev/index.js | 4 +++- lib/VJsfNoDeps.js | 10 +++++++++- package.json | 1 + 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 doc/examples/dev/_calculated-value.js diff --git a/doc/examples/dev/_calculated-value.js b/doc/examples/dev/_calculated-value.js new file mode 100644 index 00000000..a44dd86f --- /dev/null +++ b/doc/examples/dev/_calculated-value.js @@ -0,0 +1,28 @@ +const id = '_calculated-value' + +const title = 'Calculate the value of a property' + +const description = `` + +const schema = { + type: 'object', + properties: { + string1: { + title: `I'm a string whose value is used to calculate the next property`, + type: 'string' + }, + string2: { + title: `I'm a string whose value is calculated`, + type: 'string', + readOnly: true, + 'x-options': { evalMethod: 'evalExpr' }, + 'x-constExpr': '"Hello " || parent.value.string1 || " !"' + } + } +} + +const model = { + string1: 'world' +} + +export default { id, title, description, schema, model } diff --git a/doc/examples/dev/index.js b/doc/examples/dev/index.js index 026d151c..ef7f2317 100644 --- a/doc/examples/dev/index.js +++ b/doc/examples/dev/index.js @@ -18,6 +18,7 @@ import ArrayRichExpression from './_array_rich_expression' import ArrayOneOfTitle from './_array_oneof_title' import ReadonlyOptions from './_readonly-options' import StringDefault from './_string_default' +import CalculatedValue from './_calculated-value' const examplesGroup = { title: 'Development', @@ -42,7 +43,8 @@ const examplesGroup = { ArrayRichExpression, ArrayOneOfTitle, ReadonlyOptions, - StringDefault + StringDefault, + CalculatedValue ] } diff --git a/lib/VJsfNoDeps.js b/lib/VJsfNoDeps.js index 09f5335d..fb625d2b 100644 --- a/lib/VJsfNoDeps.js +++ b/lib/VJsfNoDeps.js @@ -511,7 +511,15 @@ export default { if (this.fullSchema.type === 'array') { value = this.value.filter(item => ![undefined, null].includes(item)) } - return this.input(this.fixProperties(value), true) + + // Case of a select based on an array somewhere in the data + if (this.fullSchema['x-constExpr']) { + this.$watch(() => this.getFromExpr(this.fullSchema['x-constExpr']), (val) => { + this.input(val, true) + }, { immediate: true }) + } else { + return this.input(this.fixProperties(value), true) + } } } } diff --git a/package.json b/package.json index d78d1074..2f817972 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "test-update": "jest --updateSnapshot", "doc-build": "(cd doc && TARGET=https://koumoul-dev.github.io/vuetify-jsonschema-form/latest/ nuxt generate)", "doc-master": "(cd doc && TARGET=https://koumoul-dev.github.io/vuetify-jsonschema-form/master/ nuxt generate) && gh-pages-multi deploy -v -s doc/dist -t master", + "doc-2x": "(cd doc && TARGET=https://koumoul-dev.github.io/vuetify-jsonschema-form/2.x/ nuxt generate) && gh-pages-multi deploy -v -s doc/dist -t 2.x", "analyze": "webpack --profile --json > dist/stats.json && webpack-bundle-analyzer dist/stats.json" }, "jest": {