Sub topic 13
Building a custom property editor
This chapter assumes you have finished the earlier chapters. You should know how meta.xml declares properties for the lightning__FlowScreen target, and how a component receives inputs and sends outputs with FlowAttributeChangeEvent. You also need to be comfortable dispatching CustomEvent objects in LWC.
Why replace the default property panel
When an admin selects your component on a screen, Flow Builder draws a generic form from the <property> elements in meta.xml. That form is enough for simple text, number, and boolean inputs. It breaks down when you need any of the following:
- Inputs that depend on each other, such as showing a field only when a checkbox is on.
- Pickers that match your domain, such as a list of the flow's variables or a color chooser.
- Validation of configuration, before the flow is saved with a bad value.
- Help text and grouping that fit your component.
A custom property editor (CPE) is a second Lightning web component that replaces the generic form for your component. The main component still declares its properties in meta.xml. The CPE only decides how the admin sets them.
Connect the editor to the component
Add a configurationEditor attribute to the targetConfig element. Its value is the editor's name in c-kebab-case form. This example extends the quantityPicker bundle from earlier chapters with a Boolean property named allowZero.
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Quantity Picker</masterLabel>
<targets>
<target>lightning__FlowScreen</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__FlowScreen" configurationEditor="c-quantity-picker-editor">
<property name="label" type="String" role="inputOnly" label="Field label" default="Quantity"/>
<property name="allowZero" type="Boolean" role="inputOnly" label="Allow zero" default="false"/>
<property name="quantity" type="Integer" label="Quantity"/>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>Keep these points in mind:
- Every value the editor sets must be declared as a
<property>. The editor cannot invent new properties. - Once
configurationEditoris set, the editor replaces the generic form for all properties of that target config. It does not add to the form. If you leave a property out of the editor, the admin can no longer set it. - The component needs a matching
@api allowZero = false;field to receive the value at runtime.
How Flow Builder talks to the editor
The editor is an ordinary LWC with no special base class. Flow Builder communicates with it through @api properties and DOM events.
| Direction | Name | Purpose |
|---|---|---|
| Builder to editor | `inputVariables` | Array of `{ name, value, valueDataType }` for the properties already configured |
| Builder to editor | `builderContext` | Flow context, including the flow's variables and other elements |
| Builder to editor | `genericTypeMappings` | The sObject type chosen for each generic type, if you use them |
| Editor to builder | `configuration_editor_input_value_changed` | Event that sets one property value |
| Editor to builder | `configuration_editor_generic_type_mapping_changed` | Event that sets a generic type |
| Builder calls editor | `validate()` | Method that returns configuration errors |
Write the editor
The editor needs three things. It must render the current values from inputVariables. It must dispatch an event whenever the admin changes a value. It must expose validate().
Start with the template:
<template>
<lightning-input
type="text"
label="Field label"
value={label}
data-name="label"
onchange={handleLabelChange}>
</lightning-input>
<lightning-input
type="checkbox"
label="Allow zero"
checked={allowZero}
onchange={handleAllowZeroChange}>
</lightning-input>
</template>Next, the JavaScript:
import { LightningElement, api } from 'lwc';
export default class QuantityPickerEditor extends LightningElement {
_inputVariables = [];
@api builderContext;
@api genericTypeMappings;
@api
get inputVariables() {
return this._inputVariables;
}
set inputVariables(value) {
this._inputVariables = value || [];
}
get label() {
const stored = this.getStoredValue('label');
return stored === undefined ? '' : stored;
}
get allowZero() {
const stored = this.getStoredValue('allowZero');
return stored === true || stored === 'true';
}
getStoredValue(name) {
const variable = this._inputVariables.find((v) => v.name === name);
return variable ? variable.value : undefined;
}
handleLabelChange(event) {
this.dispatchValueChange('label', event.detail.value, 'String');
}
handleAllowZeroChange(event) {
this.dispatchValueChange('allowZero', event.detail.checked, 'Boolean');
}
dispatchValueChange(name, newValue, newValueDataType) {
this.dispatchEvent(
new CustomEvent('configuration_editor_input_value_changed', {
bubbles: true,
cancelable: false,
composed: true,
detail: { name, newValue, newValueDataType }
})
);
}
@api
validate() {
const errors = [];
if (!this.label.trim()) {
errors.push({
key: 'label',
errorString: 'Enter a field label.'
});
}
return errors;
}
}The editor bundle also needs its own meta.xml. It declares no targets, so it never appears in any builder palette:
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
</LightningComponentBundle>The change event
The event detail has three fields:
nameis the property name frommeta.xml, spelled exactly the same.newValueis the new value.newValueDataTypetells Flow Builder how to store it. UseStringorBooleanfor literal values, orreferencewhen the value is a reference to a flow resource. A reference value is written as{!varName}.
The event must have bubbles: true and composed: true. Flow Builder listens above the editor's shadow root, so an event without both flags is never received.
Do not keep your own copy of the values
The editor above has no field that stores the label or checkbox state. Every render reads from inputVariables. Flow Builder owns the configuration. After each event it updates its record and passes back a new inputVariables array. If the editor caches values locally, the cache can drift from what Flow Builder will save, especially after undo or when the admin reopens the panel.
Validation
validate() is called when the admin tries to leave the configuration, for example by saving or closing the panel. It returns an array. An empty array means the configuration is valid. Each error object has a key, which identifies the field, and an errorString, which Flow Builder displays. Do not confuse this method with the validate() on the runtime component. The runtime method returns { isValid, errorMessage } and runs when a user clicks Next. The editor method returns an array and runs while an admin configures the flow.
Note that validate() reads the stored label, not a default. A brand-new component with no label set fails validation until the admin types one. If you would rather accept the meta.xml default, treat a missing value as valid.
Let admins pick flow variables
builderContext is what makes editors worth the effort. It carries the flow's variables, so you can offer a combobox of variable names instead of asking the admin to type {!myVar} by hand. A typical editor reads builderContext.variables, maps each entry to a combobox option, and on selection dispatches the change event with the data type set to reference. Inspect the object in the browser console on your first build, because the exact fields depend on the Flow Builder release.
Common errors
- The editor never loads. Check the
configurationEditorvalue. It must use thec-prefix, or your namespace, and kebab case. A typo does not raise an error. Flow Builder falls back to the generic form or shows an empty panel. - Edits are ignored. The event is missing
composed: true, ornamedoes not match a declared property. - A boolean always reads as false. Flow Builder may hand back
'true'as a string. Compare against both forms, as theallowZerogetter does. - A property vanished from the panel. You did not render a control for it in the editor. The generic form is no longer shown.
- The change event fires but the old value reappears. The editor kept its own copy of the value and rendered that. Render from
inputVariables.
Test the editor
Jest can test the editor's rendering and the events it dispatches. Set inputVariables on the element, simulate a change on a lightning-input, and assert on the dispatched event's detail. It cannot reproduce Flow Builder's behavior. To confirm the whole contract, deploy both components, open a screen flow, add the component to a screen, and edit its properties. Then save, reopen the flow, and check that the values persist.
Decide whether you need one
Build a CPE when the generic form causes real mistakes or cannot express the configuration. Skip it when a few typed properties with clear labels and descriptions are enough. An editor is a second component to maintain, version, and test, and it must be updated whenever a property changes.
Exercise
A teammate sets configurationEditor="c-quantity-picker-editor" and builds the editor with a text input for label. After deploying, admins can edit the label but can no longer set "Allow zero". The allowZero property is still declared in meta.xml. What is the cause, and what is the fix?
Show answer
Once a configuration editor is set, it replaces the generic property form for the whole target config, so only properties the editor renders can be edited. The editor has no control for allowZero. Add a checkbox that reads allowZero from inputVariables and dispatches configuration_editor_input_value_changed with name: 'allowZero', the new boolean as newValue, and newValueDataType: 'Boolean'.
Exercise
Your editor dispatches the change event with new CustomEvent('configuration_editor_input_value_changed', { detail }) and nothing happens in Flow Builder. What two options are missing, and why do they matter?
Show answer
bubbles: true and composed: true are missing. Flow Builder listens outside the editor's shadow root. The event must bubble and cross the shadow boundary to reach it.