Sample: Widget views
Overview
Use the View widget sample to create a custom widget, which can have its default view overridden. The widget utilizes the renderview
component that requires the following properties:
widgetKey
: The name of the widget
viewName
: The name of the currently selected view (SfViewName
), as defined in the views property in the widget registry
viewProps
: ViewPropsBase<T>
. It can be extended to include custom properties
- children: A default implementation of a view that will be rendered if nothing is found in the views property with the provided
viewName
PREREQUISITES: You must set up a Sitefinity renderer application and connect it to your Sitefinity CMS application. For more information, see Install Sitefinity in Next.js mode.
Building the component
First you need to create a default view that will be rendered if no view with the provided sfViewName
is found.
- Use the Next.js starter template that provides the boilerplate for communication with Sitefinity CMS. The template can be found in Sitefinity's Next.js samples GitHub repository.
- In your widget folder, under ~/src/app/widgets, create a folder for the widget files, for instance custom-views folder.
- Create a .tsx file to host our React component, for instance
widget-view-default-view.tsx
.
- Paste this code in the file:
- Save the file.
Then you need to create a file that hosts the React component:
- Navigate to the ~/custom-views folder.
- Create a
.tsx
file to host our React component, for instance widget-views.tsx
.
- Paste this code in the file:
- Save the file.
Building the widget designer
Next create an entity file, that would describe the widget designer.
- Create a
.ts
file, for instance widget-view-entity.ts
. This file holds the metadata for the widget designer to construct the designer.
- Paste this code in the file:
- Save the file.
Registration with the framework
The next step is to register the component implementation and the designer metadata with the Next.js renderer. The Sitefinity Next.js SDK will automatically generate the needed designer metadata based on the defined entity. The registry is used to find the component function reference for the widget from the response of the Page Layout service. It is also used when generating metadata for the widget when it is used in the WYSIWYG page editor – labels, visuals etc.
- Navigate to ...\src\app.
- Open the widget-registry.ts file.
- Add a new entry to the map:
Run the sample
This sample is available in Sitefinity’s GitHub repository. You can run and play with it.
To do this, perform the following:
- Go to Sitefinity’s GitHub repository Sitefinity Next.js samples.
- Expand Code and click Download ZIP.
- Extract the files on your computer.
- In the extracted folder, navigate to \nextjs-samples-main\nextjs-samples-main\src\widget-view folder.
- In the command console run npm install.
- Open the
.env.development
file.
- In section
PROXY SETTINGS
, change the SF_CMS_URL
property to the URL of your Sitefinity CMS site.
If you have deployed Sitefinity CMS on the IIS, point to https://localhost:<https_port>.
- If your Sitefinity CMS is hosted on the cloud, in
SF_LOCAL_VALIDATION_KEY=
add your validation key. For more information, see Set up the project for local development (using Next.js Renderer).
- In the command console run
npm run dev
.
- Log in to your Sitefinity CMS instance and place the widget on a page.