form-components.mdx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. ---
  2. title: "FormComponents"
  3. generated: true
  4. ---
  5. <GenerationInfo sourceFile="packages/dashboard/src/lib/framework/extension-api/types/form-components.ts" sourceLine="11" packageName="@vendure/dashboard" since="3.4.0" />
  6. Allows you to define custom form components for custom fields in the dashboard.
  7. ```ts title="Signature"
  8. interface DashboardCustomFormComponent {
  9. id: string;
  10. component: DashboardFormComponent;
  11. }
  12. ```
  13. <div className="members-wrapper">
  14. ### id
  15. <MemberInfo kind="property" type={`string`} />
  16. A unique identifier for the custom form component. It is a good practice to namespace
  17. these IDs to avoid naming collisions, for example `"my-plugin.markdown-editor"`.
  18. ### component
  19. <MemberInfo kind="property" type={`<a href='/reference/dashboard/extensions-api/form-components#dashboardformcomponent'>DashboardFormComponent</a>`} />
  20. The React component that will be rendered as the custom form input.
  21. </div>
  22. <GenerationInfo sourceFile="packages/dashboard/src/lib/framework/extension-api/types/form-components.ts" sourceLine="34" packageName="@vendure/dashboard" since="3.4.0" />
  23. Interface for registering custom field components in the dashboard.
  24. For input and display components, use the co-located approach with detailForms.
  25. ```ts title="Signature"
  26. interface DashboardCustomFormComponents {
  27. customFields?: DashboardCustomFormComponent[];
  28. }
  29. ```
  30. <div className="members-wrapper">
  31. ### customFields
  32. <MemberInfo kind="property" type={`<a href='/reference/dashboard/extensions-api/form-components#dashboardcustomformcomponent'>DashboardCustomFormComponent</a>[]`} />
  33. Custom form components for custom fields. These are used when rendering
  34. custom fields in forms.
  35. </div>
  36. <GenerationInfo sourceFile="packages/dashboard/src/lib/framework/form-engine/form-engine-types.ts" sourceLine="92" packageName="@vendure/dashboard" since="3.4.0" />
  37. Props that get passed to all form input components. They are based on the
  38. controller props used by the underlying `react-hook-form`, i.e.:
  39. ```ts
  40. export type ControllerRenderProps = {
  41. onChange: (event: any) => void;
  42. onBlur: () => void;
  43. value: any;
  44. disabled?: boolean;
  45. name: string;
  46. ref: RefCallBack;
  47. };
  48. ```
  49. in addition, they can optionally be passed a `fieldDef` prop if the
  50. component is used in the context of a custom field or configurable operation arg.
  51. The `fieldDef` arg, when present, has the following shape:
  52. ```ts
  53. export type ConfigurableArgDef = {
  54. defaultValue: any
  55. description: string | null
  56. label: string | null
  57. list: boolean
  58. name: string
  59. required: boolean
  60. type: string
  61. ui: any
  62. }
  63. ```
  64. ```ts title="Signature"
  65. type DashboardFormComponentProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = ControllerRenderProps<TFieldValues, TName> & {
  66. fieldDef?: ConfigurableFieldDef;
  67. }
  68. ```
  69. <GenerationInfo sourceFile="packages/dashboard/src/lib/framework/form-engine/form-engine-types.ts" sourceLine="124" packageName="@vendure/dashboard" since="3.4.0" />
  70. Metadata which can be defined on a [DashboardFormComponent](/reference/dashboard/extensions-api/form-components#dashboardformcomponent) which
  71. provides additional information about how the dashboard should render the
  72. component.
  73. The metadata is defined by adding the static property on the component:
  74. *Example*
  75. ```ts
  76. export const MyCustomInput: DashboardFormComponent = props => {
  77. // implementation omitted
  78. }
  79. // highlight-start
  80. MyCustomInput.metadata = {
  81. isListInput: true
  82. }
  83. // highlight-end
  84. ```
  85. ```ts title="Signature"
  86. type DashboardFormComponentMetadata = {
  87. isListInput?: boolean | 'dynamic';
  88. isFullWidth?: boolean;
  89. }
  90. ```
  91. <div className="members-wrapper">
  92. ### isListInput
  93. <MemberInfo kind="property" type={`boolean | 'dynamic'`} />
  94. Defines whether this form component is designed to handle list inputs.
  95. If set to `'dynamic'`, it means the component has internal logic that can
  96. handle both lists and single values.
  97. ### isFullWidth
  98. <MemberInfo kind="property" type={`boolean`} />
  99. TODO: not currently implemented
  100. </div>
  101. <GenerationInfo sourceFile="packages/dashboard/src/lib/framework/form-engine/form-engine-types.ts" sourceLine="167" packageName="@vendure/dashboard" since="3.4.0" />
  102. This is the common type for all custom form components registered for:
  103. - custom fields
  104. - configurable operation args
  105. - detail page fields
  106. Here's a simple example:
  107. ```ts
  108. import { DashboardFormComponent, Input } from '@vendure/dashboard';
  109. const MyComponent: DashboardFormComponent = (props) => {
  110. return <Input value={props.value}
  111. onChange={props.onChange}
  112. onBlur={props.onBlur}
  113. name={props.name}
  114. disabled={props.disabled}
  115. ref={props.ref}
  116. />;
  117. };
  118. ```
  119. ```ts title="Signature"
  120. type DashboardFormComponent = React.ComponentType<DashboardFormComponentProps> & {
  121. metadata?: DashboardFormComponentMetadata;
  122. }
  123. ```