Skip to content

Commit c655a78

Browse files
karwostsCopilot
andauthored
Describe use of getConfigForm (#2751)
Co-authored-by: Copilot <[email protected]>
1 parent 0679806 commit c655a78

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

docs/frontend/custom-ui/custom-card.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,76 @@ window.customCards.push({
319319
"https://developers.home-assistant.io/docs/frontend/custom-ui/custom-card", // Adds a help link in the frontend card editor
320320
});
321321
```
322+
323+
### Using the built-in form editor
324+
While one way to configure a graphical editor is to supply a custom editor element, another option for cards with relatively simple configuration requirements is to use the built-in frontend form editor. This is done by defining a static `getConfigForm` function in your card class, that returns a form schema defining the shape of your configuration form.
325+
326+
Example:
327+
```js
328+
static getConfigForm() {
329+
return {
330+
schema: [
331+
{ name: "label", selector: { label: {} } },
332+
{ name: "entity", required: true, selector: { entity: {} } },
333+
{
334+
type: "grid",
335+
name: "",
336+
schema: [
337+
{ name: "name", selector: { text: {} } },
338+
{
339+
name: "icon",
340+
selector: {
341+
icon: {},
342+
},
343+
context: {
344+
icon_entity: "entity",
345+
},
346+
},
347+
{
348+
name: "attribute",
349+
selector: {
350+
attribute: {},
351+
},
352+
context: {
353+
filter_entity: "entity",
354+
},
355+
},
356+
{ name: "unit", selector: { text: {} } },
357+
{ name: "theme", selector: { theme: {} } },
358+
{ name: "state_color", selector: { boolean: {} } },
359+
],
360+
},
361+
],
362+
computeLabel: (schema) => {
363+
if (schema.name === "icon") return "Special Icon";
364+
return undefined;
365+
},
366+
computeHelper: (schema) => {
367+
switch (schema.name) {
368+
case "entity":
369+
return "This text describes the function of the entity selector";
370+
case "unit":
371+
return "The unit of measurement for this card";
372+
}
373+
return undefined;
374+
},
375+
assertConfig: (config) => {
376+
if (config.other_option) {
377+
throw new Error("'other_option' is unexpected.");
378+
}
379+
},
380+
};
381+
}
382+
```
383+
From this function, you should return an object with up to 4 keys:
384+
385+
- `schema` _(required)_: This is a list of schema objects, one per form field, defining various properties of the field, like the name and selector.
386+
- `computeLabel` _(optional)_: This callback function will be called per form field, allowing the card to define the label that will be displayed for the field. If `undefined`, Home Assistant may apply a known translation for generic field names like `entity`, or you can supply your own translations.
387+
- `computeHelper` _(optional)_: This callback function will be called per form field, allowing you to define longer helper text for the field, which will be displayed below the field.
388+
- `assertConfig` _(optional)_: On each update of the configuration, the user's config will be passed to this callback function. If you throw an `Error` during this callback, the visual editor will be disabled. This can be used to disable the visual editor when the user enters incompatible data, like entering an object in yaml for a selector that expects a string. If a subsequent execution of this callback does not throw an error, the visual editor will be re-enabled.
389+
390+
This example then results in the following config form:
391+
![Screenshot of the config form](/img/en/frontend/dashboard-custom-card-config-form.png)
392+
393+
394+
18 KB
Loading

0 commit comments

Comments
 (0)