|
| 1 | +--- |
| 2 | +title: InterpreterEntity |
| 3 | +nextjs: |
| 4 | + metadata: |
| 5 | + title: InterpreterEntity |
| 6 | + description: API Reference of InterpreterEntity. |
| 7 | +--- |
| 8 | + |
| 9 | +This React component renders a single entity from an [interpreter store](/docs/api/react/use-interpreter-store), including its children. |
| 10 | + |
| 11 | +## Reference |
| 12 | + |
| 13 | +### `<InterpreterEntity interpreterStore components children? />` |
| 14 | + |
| 15 | +Use the `InterpreterEntity` component to render a single entity, including its children. |
| 16 | + |
| 17 | +```tsx |
| 18 | +import { |
| 19 | + InterpreterEntity, |
| 20 | + useInterpreterStore, |
| 21 | +} from "@coltorapps/builder-react"; |
| 22 | + |
| 23 | +import { formBuilder } from "./form-builder"; |
| 24 | +import { TextFieldEntity } from "./text-field-entity"; |
| 25 | + |
| 26 | +const formSchema = { |
| 27 | + entities: { |
| 28 | + "51324b32-adc3-4d17-a90e-66b5453935bd": { |
| 29 | + type: "textField", |
| 30 | + attributes: { |
| 31 | + label: "First name", |
| 32 | + }, |
| 33 | + }, |
| 34 | + "a2971678-1e09-48dc-80e9-70f4fe75d4db": { |
| 35 | + type: "textField", |
| 36 | + attributes: { |
| 37 | + label: "Last name", |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + root: [ |
| 42 | + "51324b32-adc3-4d17-a90e-66b5453935bd", |
| 43 | + "a2971678-1e09-48dc-80e9-70f4fe75d4db", |
| 44 | + ], |
| 45 | +}; |
| 46 | + |
| 47 | +export function App() { |
| 48 | + const interpreterStore = useInterpreterStore(formBuilder); |
| 49 | + |
| 50 | + return ( |
| 51 | + <InterpreterEntity |
| 52 | + entityId="a2971678-1e09-48dc-80e9-70f4fe75d4db" |
| 53 | + interpreterStore={interpreterStore} |
| 54 | + components={{ textField: TextFieldEntity }} |
| 55 | + /> |
| 56 | + ); |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +In the example above, we've hardcoded the schema, but typically, you would retrieve it from a database, for instance. |
| 61 | + |
| 62 | +### Props |
| 63 | + |
| 64 | +The `InterpreterEntity` component accepts four props: |
| 65 | + |
| 66 | +| Prop | Type | Description {% class="api-description" %} | |
| 67 | +| ------------------ | --------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 68 | +| `entityId` | {% badge content="string" /%} | The ID of the entity to render, including its children. | |
| 69 | +| `interpreterStore` | {% badge content="object" /%} | The [interpreter store](/docs/api/react/use-interpreter-store). | |
| 70 | +| `components` | {% badge content="object" /%} | An object mapping of [entities components](/docs/api/react/create-entity-component) for each defined entity type in the builder. | |
| 71 | +| `children` | {% badge content="function" /%} {% badge content="optional" /%} | A function intended to wrap each rendered arbitrary entity with additional rendering. It receives both the rendered entity and the entity instance object. | |
| 72 | + |
| 73 | +### Returns |
| 74 | + |
| 75 | +The `InterpreterEntity` component essentially renders a single entity, including its children. |
| 76 | + |
| 77 | +### Render prop |
| 78 | + |
| 79 | +The `children` prop of the `InterpreterEntity` component must be a function, which is used to wrap each rendered arbitrary entity with additional rendering. |
| 80 | + |
| 81 | +```tsx |
| 82 | +import { |
| 83 | + InterpreterEntity, |
| 84 | + useInterpreterStore, |
| 85 | +} from "@coltorapps/builder-react"; |
| 86 | + |
| 87 | +import { formBuilder } from "./form-builder"; |
| 88 | +import { TextFieldEntity } from "./text-field-entity"; |
| 89 | + |
| 90 | +const formSchema = { |
| 91 | + entities: { |
| 92 | + "51324b32-adc3-4d17-a90e-66b5453935bd": { |
| 93 | + type: "textField", |
| 94 | + attributes: { |
| 95 | + label: "First name", |
| 96 | + }, |
| 97 | + }, |
| 98 | + "a2971678-1e09-48dc-80e9-70f4fe75d4db": { |
| 99 | + type: "textField", |
| 100 | + attributes: { |
| 101 | + label: "Last name", |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + root: [ |
| 106 | + "51324b32-adc3-4d17-a90e-66b5453935bd", |
| 107 | + "a2971678-1e09-48dc-80e9-70f4fe75d4db", |
| 108 | + ], |
| 109 | +}; |
| 110 | + |
| 111 | +export function App() { |
| 112 | + const interpreterStore = useInterpreterStore(formBuilder); |
| 113 | + |
| 114 | + return ( |
| 115 | + <InterpreterEntity |
| 116 | + entityId="a2971678-1e09-48dc-80e9-70f4fe75d4db" |
| 117 | + interpreterStore={interpreterStore} |
| 118 | + components={{ textField: TextFieldEntity }} |
| 119 | + > |
| 120 | + {(props) => ( |
| 121 | + <div> |
| 122 | + {/* This is the rendered entity. */} |
| 123 | + {props.children} |
| 124 | + </div> |
| 125 | + )} |
| 126 | + </InterpreterEntity> |
| 127 | + ); |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +In the example above, we've hardcoded the schema, but typically, you would retrieve it from a database, for instance. |
0 commit comments