From 22cda514b426081821af1165fed6d7c9dcfcd616 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Mon, 5 Aug 2024 16:53:10 +0100 Subject: [PATCH] Update component settings bar to respect dependsOn metadata --- .../Component/ComponentSettingsSection.svelte | 46 ++----------------- .../src/components/preview/SettingsBar.svelte | 15 ++++-- packages/frontend-core/src/utils/index.js | 1 + packages/frontend-core/src/utils/settings.js | 43 +++++++++++++++++ 4 files changed, 57 insertions(+), 48 deletions(-) create mode 100644 packages/frontend-core/src/utils/settings.js diff --git a/packages/builder/src/pages/builder/app/[application]/design/[screenId]/[componentId]/_components/Component/ComponentSettingsSection.svelte b/packages/builder/src/pages/builder/app/[application]/design/[screenId]/[componentId]/_components/Component/ComponentSettingsSection.svelte index 54241ea1cc..d5a696c6bf 100644 --- a/packages/builder/src/pages/builder/app/[application]/design/[screenId]/[componentId]/_components/Component/ComponentSettingsSection.svelte +++ b/packages/builder/src/pages/builder/app/[application]/design/[screenId]/[componentId]/_components/Component/ComponentSettingsSection.svelte @@ -1,5 +1,4 @@ diff --git a/packages/client/src/components/preview/SettingsBar.svelte b/packages/client/src/components/preview/SettingsBar.svelte index de234d20ea..52f5c04538 100644 --- a/packages/client/src/components/preview/SettingsBar.svelte +++ b/packages/client/src/components/preview/SettingsBar.svelte @@ -5,7 +5,7 @@ import SettingsColorPicker from "./SettingsColorPicker.svelte" import SettingsPicker from "./SettingsPicker.svelte" import { builderStore, componentStore, dndIsDragging } from "stores" - import { Utils } from "@budibase/frontend-core" + import { Utils, shouldDisplaySetting } from "@budibase/frontend-core" import { findComponentParent } from "utils/components" import { getGridVar, GridParams } from "utils/grid" @@ -39,7 +39,7 @@ measured = false } } - $: settings = getBarSettings(definition) + $: settings = getBarSettings(component, definition) $: isRoot = componentId === $builderStore.screen?.props?._id $: insideGrid = parent?._component.endsWith("/container") && parent.layout === "grid" @@ -51,16 +51,21 @@ $: gridHAlignVar = getGridVar(device, GridParams.HAlign) $: gridVAlignVar = getGridVar(device, GridParams.VAlign) - const getBarSettings = definition => { + const getBarSettings = (component, definition) => { let allSettings = [] definition?.settings?.forEach(setting => { - if (setting.section) { + if (setting.section && shouldDisplaySetting(component, setting)) { allSettings = allSettings.concat(setting.settings || []) } else { allSettings.push(setting) } }) - return allSettings.filter(setting => setting.showInBar && !setting.hidden) + return allSettings.filter( + setting => + setting.showInBar && + !setting.hidden && + shouldDisplaySetting(component, setting) + ) } const updatePosition = () => { diff --git a/packages/frontend-core/src/utils/index.js b/packages/frontend-core/src/utils/index.js index 9eb7206012..5f21e7db99 100644 --- a/packages/frontend-core/src/utils/index.js +++ b/packages/frontend-core/src/utils/index.js @@ -9,3 +9,4 @@ export { memo, derivedMemo } from "./memo" export { createWebsocket } from "./websocket" export * from "./download" export * from "./theme" +export * from "./settings" diff --git a/packages/frontend-core/src/utils/settings.js b/packages/frontend-core/src/utils/settings.js new file mode 100644 index 0000000000..0e312c70e6 --- /dev/null +++ b/packages/frontend-core/src/utils/settings.js @@ -0,0 +1,43 @@ +import { helpers } from "@budibase/shared-core" + +// Util to check if a setting can be rendered for a certain instance, based on +// the "dependsOn" metadata in the manifest +export const shouldDisplaySetting = (instance, setting) => { + let dependsOn = setting.dependsOn + if (dependsOn && !Array.isArray(dependsOn)) { + dependsOn = [dependsOn] + } + if (!dependsOn?.length) { + return true + } + + // Ensure all conditions are met + return dependsOn.every(condition => { + let dependantSetting = condition + let dependantValues = null + let invert = !!condition.invert + if (typeof condition === "object") { + dependantSetting = condition.setting + dependantValues = condition.value + } + if (!dependantSetting) { + return false + } + + // Ensure values is an array + if (!Array.isArray(dependantValues)) { + dependantValues = [dependantValues] + } + + // If inverting, we want to ensure that we don't have any matches. + // If not inverting, we want to ensure that we do have any matches. + const currentVal = helpers.deepGet(instance, dependantSetting) + const anyMatches = dependantValues.some(dependantVal => { + if (dependantVal == null) { + return currentVal != null && currentVal !== false && currentVal !== "" + } + return dependantVal === currentVal + }) + return anyMatches !== invert + }) +}