Sub topic 8
Property Types and Attributes: default, required, min, max, datasource
This chapter assumes you can already build a basic Lightning web component and read its .js-meta.xml file. It also assumes you know how targetConfigs and @api properties work. Here you will learn which types a <property> element supports and which attributes control how App Builder shows and validates it.
How a property element is built
Each <property> in a targetConfig has a name and a type. The name must match a public @api property in the component's JavaScript class. App Builder shows the property in the property panel and passes the admin's value to that @api property.
<property name="maxItems" type="Integer" label="Maximum items" min="1" max="10" default="5" />Common types
| Type | Property panel control | Typical use |
|---|---|---|
| String | Text box, or picklist with `datasource` | Header text, size choice |
| Integer | Number field | Row limits, counts |
| Boolean | Checkbox | Show or hide a section |
Match the JavaScript default to the declared type. A maxItems property declared as Integer should be a number in the class (@api maxItems = 5;), not a string.
Attributes that shape the property
| Attribute | Applies to | Effect |
|---|---|---|
| `label` | All | Text admins see in the panel. Without it, the panel shows the property name. |
| `default` | All | Value used until the admin changes it. |
| `required` | All | Admin must supply a value before saving the page. |
| `min`, `max` | Integer | Lowest and highest value the panel accepts. |
| `datasource` | String | Comma-separated list that turns the field into a picklist. |
| `description` | All | Help text shown beside the field. |
default
A default in the XML sets the value in the property panel. A default in the JavaScript, such as @api headerText = 'Summary', applies when the component runs without a configured value. Keep the two the same so the component behaves the same in both places.
required
Use required="true" when the component cannot work without a value. Do not combine it with a default unless you want to force the admin to confirm the value.
min and max
These attributes limit what an admin can enter for an Integer. They do not change what your JavaScript receives from other sources, so a component that is also used from a parent component should still validate its input.
datasource
The datasource attribute gives a String property a fixed set of choices:
<property name="size" type="String" label="Card size" datasource="small,medium,large" default="medium" />The default should be one of the listed values. Admins pick from the list, so your code can rely on receiving one of those strings.
Putting it together
This targetConfig uses several attributes at once.
<targetConfig targets="lightning__RecordPage">
<objects>
<object>Account</object>
</objects>
<property name="headerText" type="String" label="Header text" default="Summary" />
<property name="size" type="String" label="Card size" datasource="small,medium,large" default="medium" />
<property name="maxItems" type="Integer" label="Maximum items" min="1" max="10" default="5" />
<property name="showDetails" type="Boolean" label="Show details" default="false" />
</targetConfig>The matching JavaScript declares each property as public:
import { LightningElement, api } from 'lwc';
export default class AccountSummaryCard extends LightningElement {
@api headerText = 'Summary';
@api size = 'medium';
@api maxItems = 5;
@api showDetails = false;
}Common errors
- Property missing from the panel. The
namedoes not match the@apiproperty, or the property is listed in atargetConfigthat does not include the target you are viewing. - Deployment fails on the metadata. A
minormaxis used on a non-Integer type, or thetypevalue is misspelled. - Picklist shows no selected value. The
defaultis not one of thedatasourcevalues. - Boolean arrives as text. The JavaScript code compares against the string
'true'instead of the Boolean value.
Exercise
An admin must choose between "compact" and "full" layouts, and the component should start as "compact". Write the property element and the matching @api declaration.
Show answer
XML: <property name="layout" type="String" label="Layout" datasource="compact,full" default="compact" />. JavaScript: @api layout = 'compact';. The datasource makes the panel show a picklist, and the default is one of the listed values.
Exercise
You declare <property name="maxItems" type="Integer" min="1" max="10" default="15" />. What problem does this create, and how do you fix it?
Show answer
The default lies outside the allowed range, so the panel starts with an invalid value. Change the default to a number from 1 to 10, for example 5, and keep the @api default the same.