前言
在使用Spfx开发扩展部件时,结合react+antd实现topPlaceholder自定义内容,如下为具体实现步骤
效果
实现步骤
1、创建项目可参考【SharePoint Online】01 通过Spfx创建扩展部件(SPFx Application Customizer)
2、安装antd组件
在终端运行如下命令安装antd组件(UI+样式)
cnpm install antd --save
3、按照目录结构创建文件src\Component\ReactFoot.tsx(可通用组件)
import * as React from "react";
import { Popover, Button } from 'antd';
import 'antd/dist/antd.css';
export interface IReactFooterProps { }
const text = <span>Title</span>;
const content = (
<div>
<p>Content</p>
<p>Content</p>
</div>
);
const buttonWidth = 70;
export default class ReactFooter extends React.Component<IReactFooterProps> {
constructor(props: IReactFooterProps) {
super(props);
}
public handleShowForm = () => {
console.log('11');
}
public render(): React.ReactElement<IReactFooterProps> {
return (
<div>
<Popover placement="bottom" title={text} content={content} trigger="click">
<Button>Bottom</Button>
</Popover>
</div>
);
}
}
如下为重要代码说明
4、修改文件src\extensions\helloWorld\HelloWorldApplicationCustomizer.ts
import { override } from '@microsoft/decorators';
import { Log } from '@microsoft/sp-core-library';
import {
BaseApplicationCustomizer,
PlaceholderContent,
PlaceholderName
} from '@microsoft/sp-application-base';
import { Dialog } from '@microsoft/sp-dialog';
import * as React from "react";
import * as ReactDOM from "react-dom";
//import ReactFooter, { IReactFooterProps } from "../../Component/ReactFooter";
import ReactFoot ,{ IReactFooterProps } from '../../Component/ReactFoot';
import * as strings from 'HelloWorldApplicationCustomizerStrings';
const LOG_SOURCE: string = 'HelloWorldApplicationCustomizer';
/**
* If your command set uses the ClientSideComponentProperties JSON input,
* it will be deserialized into the BaseExtension.properties object.
* You can define an interface to describe it.
*/
export interface IHelloWorldApplicationCustomizerProperties {
// This is an example; replace with your own property
testMessage: string;
Top: string;
Bottom: string;
}
/** A Custom Action which can be run during execution of a Client Side Application */
export default class HelloWorldApplicationCustomizer
extends BaseApplicationCustomizer<IHelloWorldApplicationCustomizerProperties> {
// These have been added
private _topPlaceholder: PlaceholderContent | undefined;
private _bottomPlaceholder: PlaceholderContent | undefined;
@override
public onInit(): Promise<void> {
Log.info(LOG_SOURCE, `Initialized ${strings.Title}`);
// Wait for the placeholders to be created (or handle them being changed) and then
// render.
this.context.placeholderProvider.changedEvent.add(this, this._renderPlaceHolders);
return Promise.resolve<void>();
}
private _renderPlaceHolders(): void {
console.log("HelloWorldApplicationCustomizer._renderPlaceHolders()");
console.log(
"Available placeholders: ",
this.context.placeholderProvider.placeholderNames
.map(name => PlaceholderName[name])
.join(", ")
);
// Handling the top placeholder
if (!this._topPlaceholder) {
this._topPlaceholder = this.context.placeholderProvider.tryCreateContent(
PlaceholderName.Top,
{ onDispose: this._onDispose }
);
// The extension should not assume that the expected placeholder is available.
if (!this._topPlaceholder) {
console.error("The expected placeholder (Top) was not found.");
return;
}
if (this.properties) {
/*let topString: string = this.properties.Top;
if (!topString) {
topString = "(Top property was not defined.)";
}
if (this._topPlaceholder.domElement) {
this._topPlaceholder.domElement.innerHTML = `
<div class="${styles.app}">
<div class="${styles.top}">
<i class="ms-Icon ms-Icon--Info" aria-hidden="true"></i> ${escape(
topString
)}
</div>
</div>`;
}*/
const elem: React.ReactElement<IReactFooterProps> = React.createElement(ReactFoot);
ReactDOM.render(elem, this._topPlaceholder.domElement);
}
}
// Handling the bottom placeholder
if (!this._bottomPlaceholder) {
this._bottomPlaceholder = this.context.placeholderProvider.tryCreateContent(
PlaceholderName.Bottom,
{ onDispose: this._onDispose }
);
// The extension should not assume that the expected placeholder is available.
if (!this._bottomPlaceholder) {
console.error("The expected placeholder (Bottom) was not found.");
return;
}
if (this.properties) {
/*let bottomString: string = this.properties.Bottom;
if (!bottomString) {
bottomString = "(Bottom property was not defined.)";
}
if (this._bottomPlaceholder.domElement) {
this._bottomPlaceholder.domElement.innerHTML = `
<div class="${styles.app}">
<div class="${styles.bottom}">
<i class="ms-Icon ms-Icon--Info" aria-hidden="true"></i> ${escape(
bottomString
)}
</div>
</div>`;
}*/
}
}
}
private _onDispose(): void {
console.log('[HelloWorldApplicationCustomizer._onDispose] Disposed custom top and bottom placeholders.');
}
}
如下为重要代码说明
5、最后在终端执行gulp serve查看效果