2.2. Create Default Parameters for Component
To create the default parameters for our custom button we need to create a new file in /EZEXTEND_ROOT/ui/src/Components/Widgets/Defaults named CustomButton.js.
In this file, we need to create a single exported variable, a JSON object that contains information describing the default state of the component when it is initially dragged onto the dashboard:
1export const CustomButtonData = {
2 size: {
3 width: 200,
4 height: 40
5 },
6
7 data: {
8 label: 'Action'
9 }
10}
Here we have defined the initial size of the element and the string text to be displayed on the button when it is first created. Now, we can navigate to the file /EZEXTEND_ROOT/ui/src/Components/Widgets/Defaults/index.js to import and apply the default data we have just created. First we will import the JSON object we just created:
1...
2import { ChartData } from './Chart';
3import { ButtonData } from './Button';
4import { CustomButtonData } from './CustomButton';
5import { MarkdownData } from './Markdown';
6...
Currently as you can see, all of the custom data is spread across multiple files. This index file will serve as a one-stop-shop to access all of these default data objects. To achieve this, all of the defaults that are imported are promptly exported from index.js so that they can be called elsewhere by importing this index file. We will include our imported custom button data as an available export:
Attention
More info needs to be added here as why this is necessary given that this list of exports is not used elsewhere in the code.
1export {
2 ChartData,
3 ButtonData,
4 CustomButtonData,
5 MarkdwonData,
6 ...
7};
Lastly, we need to add some logic to the default function WidgetDefaultsProvider(type) so that when it is called with the argument WidgetsMapping.INPUTS.CUSTOM_BUTTON it returns the appropriate data:
1export default function WidgetDefaultsProvider(type) {
2 switch (type) {
3 case WidgetsMapping.CHARTS.COMBO:
4 return ChartData;
5 case WidgetsMapping.INPUTS.BUTTON:
6 return ButtonData;
7 case WidgetsMapping.INPUTS.CUSTOM_BUTTON:
8 return CustomButtonData;
9 case WidgetsMapping.INPUTS.RADIO:
10 return RadioData;
11 ...
12 }
13}