Sub topic 8
Property types and attributes: default, required, min, max, datasource
This chapter assumes you can already write a targetConfigs block and know which @api properties your component exposes. If not, review the earlier chapters on targetConfigs and on public properties first. Here you will learn how each <property> element is typed and constrained, and how those settings affect what an admin sees and what your component receives.
What a property element describes
Each <property> element inside a targetConfig describes one input field in the App Builder property panel. It also links that field to an @api property in your JavaScript class through the name attribute.
<property name="maxItems" type="Integer" label="Maximum items" min="1" max="10" default="5" />The element has three jobs:
- It picks the input control. A
Stringgets a text box, anIntegergets a number box, and aBooleangets a checkbox. - It constrains the value an admin can enter.
- It sets the initial value when an admin first drops the component on a page.
The name value must match the @api property name exactly, including case. maxItems and maxitems are different names. A mismatch is one of the most common reasons a property appears in the panel but never reaches the component.
Property types
The type attribute sets the data type. For the Lightning page targets in this tutorial (lightning__AppPage, lightning__HomePage, lightning__RecordPage), use these three:
| Type | Panel control | Value your component receives |
|---|---|---|
| `String` | Text box, or a picklist when `datasource` is set | A JavaScript string |
| `Integer` | Number input | A JavaScript number |
| `Boolean` | Checkbox | `true` or `false` |
Other targets accept other types. Experience Builder and Flow screens, for example, support additional ones such as Color. Check the supported types for each target in the metadata reference before you use one on a Lightning page.
The common attributes
These attributes appear on most property elements:
| Attribute | Applies to | Effect |
|---|---|---|
| `name` | All | Must match the `@api` property name |
| `type` | All | Selects the data type and control |
| `label` | All | The text admins see next to the field |
| `description` | All | Help text shown in the panel |
| `default` | All | Initial value when the component is first placed |
| `required` | All | Blocks saving the page until the admin gives a value |
| `min`, `max` | `Integer` | Limits the allowed numeric range |
| `placeholder` | `String` | Hint text shown in an empty box |
| `datasource` | `String` | Turns the box into a picklist of fixed values |
If you omit label, the panel falls back to the property name. That is rarely what you want to show an admin, so set a label on every property.
default: the starting value
default is the value the panel shows when the admin first places the component. It is a string in XML, but App Builder converts it to match type.
<property name="headerText" type="String" label="Header text" default="Summary" />
<property name="showFooter" type="Boolean" label="Show footer" default="false" />There are two places a default can live: the XML default and the initializer in your class (@api headerText = 'Summary';). They do different things.
- The XML default fills in the panel. After the admin saves the page, that value is stored with the page.
- The JavaScript initializer applies whenever the component is created without a supplied value. This includes a component placed in another component, or a property you left out of
targetConfig.
Keep the two in sync. If the XML says default="5" and the class says maxItems = 10, behavior depends on how the component was created, and the mismatch is hard to debug.
required: forcing a value
Set required="true" when the component cannot work without an input.
<property name="listTitle" type="String" label="List title" required="true" />App Builder will not let the admin save the page until the field has a value. Pair required with a sensible default only if a default actually makes sense. A required property with a default is already filled in, so the admin never gets prompted.
required is enforced in the panel only. A component that is loaded some other way, for example from code, can still receive undefined. Write your component so it behaves sensibly when a "required" property is empty.
min and max: bounding an Integer
min and max limit an Integer property. In the panel, a value outside the range is flagged and cannot be saved.
<property name="maxItems" type="Integer" label="Maximum items" min="1" max="10" default="5" />This validation is a convenience for admins. It does not protect your JavaScript. Another page configuration, an API deployment of page metadata, or a parent component can supply any number. Clamp the value in the component:
import { LightningElement, api } from 'lwc';
const MIN_ITEMS = 1;
const MAX_ITEMS = 10;
export default class AccountSummaryCard extends LightningElement {
@api recordId;
@api objectApiName;
@api headerText = 'Summary';
@api showFooter = false;
_maxItems = 5;
@api
get maxItems() {
return this._maxItems;
}
set maxItems(value) {
const parsed = parseInt(value, 10);
this._maxItems = Number.isNaN(parsed)
? 5
: Math.min(MAX_ITEMS, Math.max(MIN_ITEMS, parsed));
}
}The getter and setter pair is still a public @api property, so a <property name="maxItems"> element binds to it as before. Putting the clamp in the setter means every route into the property is covered.
datasource: a picklist of fixed values
Add datasource to a String property to replace the text box with a picklist. Give it a comma-separated list of the allowed values.
<property name="size" type="String" label="Card size" datasource="small,medium,large" default="medium" />Points to remember:
- The admin sees each value as written and your component receives that same string. There is no separate display label.
defaultshould be one of the listed values. A default that is not in the list gives a confusing panel.- Do not rely on the picklist as the only check. Handle an unexpected value in your code, for example by falling back to
medium.
Use the value to choose a CSS class or a card layout:
import { LightningElement, api } from 'lwc';
export default class SizedCard extends LightningElement {
@api size = 'medium';
get cardClass() {
const allowed = ['small', 'medium', 'large'];
const safe = allowed.includes(this.size) ? this.size : 'medium';
return `card card_${safe}`;
}
}A complete targetConfig
Putting the attributes together, here is a record page configuration that uses every attribute from this chapter:
<targetConfig targets="lightning__RecordPage">
<objects>
<object>Account</object>
</objects>
<property name="headerText" type="String" label="Header text"
description="Title shown at the top of the card."
placeholder="Summary" required="true" default="Summary" />
<property name="maxItems" type="Integer" label="Maximum items"
min="1" max="10" default="5" />
<property name="showFooter" type="Boolean" label="Show footer" default="false" />
<property name="size" type="String" label="Card size"
datasource="small,medium,large" default="medium" />
</targetConfig>Every name here must have a matching @api property in the class. Deploy it with the same command used earlier:
sf project deploy start --source-dir force-app/main/default/lwc/accountSummaryCardCommon mistakes
- Name mismatch. The XML says
headerTextand the class saysheadertext. The panel shows the field, but the component never gets the value. - Property listed in the wrong
targetConfig. The panel only shows properties from thetargetConfigthat matches the page type. A property defined only forlightning__RecordPagewill not appear on a home page. - Wrong type for the control you want. A
Stringwith digits stays a string in JavaScript. UseIntegerif you need a number. - Using
minormaxon aString. These apply toInteger. Do not expect them to limit text length. - A default outside the allowed range. A
defaultof20withmax="10"gives a panel value that fails its own validation. - Changing a property after pages exist. If you rename or retype a property, pages that already saved the old value may lose it or fail to deploy. Treat property names as part of your component's public interface.
Exercises
Exercise
A property is declared as <property name="pageSize" type="Integer" label="Page size" min="5" max="50" default="100" />. What is wrong, and how do you fix it?
Show answer
The default of 100 is above the max of 50, so the initial panel value fails its own validation. Change the default to a value from 5 to 50, for example default="10", or raise max if 100 is a valid choice.
Exercise
You add datasource="red,green,blue" to a String property. An admin picks "green". What does your component receive, and why should you still validate it in JavaScript?
Show answer
It receives the string "green". You should still validate because the picklist only constrains the panel. The page configuration can be supplied by other routes, and a parent component can set any string, so the component should fall back to a safe value when it gets something unexpected.
Exercise
An admin reports that the "Maximum items" field shows in the property panel, but changing it has no effect on the rendered component. The XML has name="maxItems", and the class has @api maxitems = 5;. What is the cause?
Show answer
The names differ in case. The XML property maxItems does not match the class property maxitems, so the value is never delivered to the field the template reads. Rename the class property to maxItems (or change the XML to match) and redeploy.