在创建一个webpart的时候,SPFx会使用一个默认图标,如下所示:
这个图标是在manifest文件中 ,通过officeFabricIconFontName属性指定的,默认的值是“Page”,所以我们看到的是一个页面的图标。
"preconfiguredEntries": [{
"groupId": "5c03119e-3074-46fd-976b-c60198311f70", // Other
"group": { "default": "Other" },
"title": { "default": "WebpartTips" },
"description": { "default": "WebpartTips description" },
"officeFabricIconFontName": "Page",
"properties": {
"description": "WebpartTips"
}
}]
可以修改这个属性的值,换成其他图标,例如“Calendar”,重新启动gulp serve,就可以看到新图标:
所有可以使用的fabric图标,可以参考:https://developer.microsoft.com/en-us/fabric#/styles/icons
如果希望使用自定义的图标,需要删除“officeFabricIconFontName”这个属性,添加另一个属性“iconImageUrl”,因为officeFabricIconFontName会覆盖iconImageUrl这个属性。iconImageUrl的值可以是图片的url,也可以是base64编码的图片:
"preconfiguredEntries": [{
"groupId": "5c03119e-3074-46fd-976b-c60198311f70", // Other
"group": { "default": "Other" },
"title": { "default": "WebpartTips" },
"description": { "default": "WebpartTips description" },
"iconImageUrl": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGOfPtRkwAAACBjSFJNAACHDwAAjA8AAP1SAACBQAAAfXkAAOmLAAA85QAAGcxzPIV3AAAKOWlDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAEjHnZZ3VFTXFofPvXd6oc0wAlKG3rvAANJ7k15FYZgZYCgDDjM0sSGiAhFFRJoiSFDEgNFQJFZEsRAUVLAHJAgoMRhFVCxvRtaLrqy89/Ly++Osb+2z97n77L3PWhcAkqcvl5cGSwGQyhPwgzyc6RGRUXTsAIABHmCAKQBMVka6X7B7CBDJy82FniFyAl8EAfB6WLwCcNPQM4BOB/+fpFnpfIHomAARm7M5GSwRF4g4JUuQLrbPipgalyxmGCVmvihBEcuJOWGRDT77LLKjmNmpPLaIxTmns1PZYu4V8bZMIUfEiK+ICzO5nCwR3xKxRoowlSviN+LYVA4zAwAUSWw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JtazE5sUdFCEEJ4Nc8oGo1CRyFEZTL8MaNvfpzOvHen8fEQgugD597zzjvne985l2eJwj8QM3uU8k6wSzHYjyEqUFyH7ZeQuQIfTsJ8I6zcgY1xkxAm2kLuOXy+CZeS+taDeBvkv8LiQ7iWA7vdJBqqAn/I9IvMaehgVcSJi8yr/07tY5dIYVnkjfo/hkxyQL0FKelyAN3TkD6vp5ehwYImDReysD8MV1PguX56jbrAXlr7nFVHK+JBiIp2p/7y2SUGRy5w79EK7uZtPeybSTgqcFqHZledemir4xVvd74wPJrElg1ONOZ5NjlNWS6ajNAXLIAO3I/k1S6nGBiaY/b1C1pbmonF4lhWjM5zZ5hKTlYrfOoCp3qg8y4s9fnTvv94i47WihbpzRaLeJ7nm84N13VxHMcvC1/jvg5oc0yvcgLH+kQ2u05b8y/tGQ5LFk0Nws9dwbZtEolEUFMVCFHIiHy/IZJGthdvyfWnIg9SIjOuSP8Tk3OEqECNvQWR0prv9g5UdK1ZmOMFDLndisy8L5unKP/xb/wr4Dc5T2zQ/iuExAAAAABJRU5ErkJggg==",
"properties": {
"description": "WebpartTips"
}
}]
效果如下:
在manifest文件中,通过指定hiddenFromToolbox的值为true,可以隐藏一个webpart:
这样用户在页面上添加一个webpart的时候,你的webpart不会被列出来:
很多情况下,一个webpart需要配置才可以使用。这里推荐一个pnp控件:Placeholder Control
可以在用户添加webpart之后,立即提示用户配置webpart:
首先安装pnp控件:
然后在webpart ts文件中导入react,react dom以及Placeholder控件:
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Placeholder } from "@pnp/spfx-controls-react/lib/Placeholder";
修改render方法,显示placeholder控件:
export default class WebpartTipsWebPart extends BaseClientSideWebPart {
public render(): void {
ReactDom.render(React.createElement(Placeholder, {
iconName:'Edit',
iconText:'请配置web part',
description:'点击按钮配置web part',
buttonLabel:'配置',
onConfigure: this.onConfigure.bind(this)
}), this.domElement);
}
private onConfigure() {
this.context.propertyPane.open();
}
。。。
默认情况下,webpart属性面板中的组Group是顺序展示的,例如Group 1,然后Group 2,每个组中包含配置项:
但是如果一个组中的配置项比较多,最好可以将组折叠起来就像这样:
这是在”getPropertyPaneConfiguration“方法中,指定“displayGroupsAsAccordion“属性来实现的,这个属性为true,表示组折叠起来展示,默认情况下所有的组都是折叠的。如果希望某个组默认是展开的,可以在组的配置中指定“isCollapsed”属性为false,上面的例子中,Group 1的“isCollapsed”属性为true,Group 2的“isCollapsed”属性为false
:
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
return {
pages: [
{
header: {
description: strings.PropertyPaneDescription
},
displayGroupsAsAccordion: true, //指定为true,折叠组的配置项
groups: [
{
groupName: 'Group 1',
groupFields: [
PropertyPaneTextField('description', {
label: 'Field in Group 1'
})
],
isCollapsed:true //指定组是否是折叠的
},
{
groupName: 'Group 2',
groupFields: [
PropertyPaneTextField('description', {
label: 'Field in Group 2'
})
],
isCollapsed: false
}
]
}
]
};
}
”getPropertyPaneConfiguration“方法返回值包含了pages数组,在pages数组中添加新的page,可以分页显示属性:
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
return {
pages: [
{
header: {
description: strings.PropertyPaneDescription
},
groups: [
{
groupName: strings.BasicGroupName,
groupFields: [
PropertyPaneTextField('description', {
label: strings.DescriptionFieldLabel
})
]
}
]
},
{ //这里添加了一个page
header: {
description: strings.PropertyPaneDescription
},
groups: [
{
groupName: strings.BasicGroupName,
groupFields: [
PropertyPaneTextField('description', {
label: strings.DescriptionFieldLabel
})
]
}
]
}
]
};
}
效果如图:
第一次打开属性编辑面板中默认显示初始值: