Sample web part for Taxonomy Picker control - Jose Quinto (blog.josequinto.com) (#158)
This commit is contained in:
parent
fbe9d65a83
commit
80d07452a6
|
@ -0,0 +1,25 @@
|
|||
# EditorConfig helps developers define and maintain consistent
|
||||
# coding styles between different editors and IDEs
|
||||
# editorconfig.org
|
||||
|
||||
root = true
|
||||
|
||||
|
||||
[*]
|
||||
|
||||
# change these settings to your own preference
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
# we recommend you to keep these unchanged
|
||||
end_of_line = lf
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
|
||||
[*.md]
|
||||
trim_trailing_whitespace = false
|
||||
|
||||
[{package,bower}.json]
|
||||
indent_style = space
|
||||
indent_size = 2
|
|
@ -0,0 +1 @@
|
|||
* text=auto
|
|
@ -0,0 +1,32 @@
|
|||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
|
||||
# Dependency directories
|
||||
node_modules
|
||||
|
||||
# Build generated files
|
||||
dist
|
||||
lib
|
||||
solution
|
||||
temp
|
||||
*.sppkg
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
|
||||
# OSX
|
||||
.DS_Store
|
||||
|
||||
# Visual Studio files
|
||||
.ntvs_analysis.dat
|
||||
.vs
|
||||
bin
|
||||
obj
|
||||
|
||||
# Resx Generated Code
|
||||
*.resx.ts
|
||||
|
||||
# Styles Generated Code
|
||||
*.scss.ts
|
|
@ -0,0 +1,14 @@
|
|||
# Folders
|
||||
.vscode
|
||||
coverage
|
||||
node_modules
|
||||
sharepoint
|
||||
src
|
||||
temp
|
||||
|
||||
# Files
|
||||
*.csproj
|
||||
.git*
|
||||
.yo-rc.json
|
||||
gulpfile.js
|
||||
tsconfig.json
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"@microsoft/generator-sharepoint": {
|
||||
"libraryName": "react-taxonomypicker-sample-spfx",
|
||||
"framework": "react",
|
||||
"version": "1.0.0",
|
||||
"libraryId": "90551413-582d-4e75-b811-2459984d62d8"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,110 @@
|
|||
# Taxonomy Picker (React)
|
||||
|
||||
## Summary
|
||||
A Taxonomy Picker control built with [React](https://facebook.github.io/react) based on [react-taxonomypicker](https://www.npmjs.com/package/react-taxonomypicker) for use with SharePoint Framework client-side web parts (SPFx).
|
||||
|
||||
![React-Taxonomy-Picker-gif](./assets/react-taxonomy-picker-spfx.gif)
|
||||
|
||||
|
||||
## Used SharePoint Framework Version
|
||||
![drop](https://img.shields.io/badge/drop-GA-green.svg)
|
||||
|
||||
|
||||
## Applies to
|
||||
* [SharePoint Framework](http://dev.office.com/sharepoint/docs/spfx/sharepoint-framework-overview)
|
||||
* [Office 365 developer tenant](http://dev.office.com/sharepoint/docs/spfx/set-up-your-developer-tenant)
|
||||
|
||||
## Solution
|
||||
|
||||
Solution|Author(s)
|
||||
--------|---------
|
||||
spfx-react-taxonomypicker | Jose Quinto ([@jquintozamora](https://twitter.com/jquintozamora) , [blog.josequinto.com](https://blog.josequinto.com))
|
||||
|
||||
## Version history
|
||||
|
||||
Version|Date|Comments
|
||||
-------|----|--------
|
||||
1.0|March 14, 2017|Initial release
|
||||
|
||||
## Disclaimer
|
||||
**THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.**
|
||||
|
||||
---
|
||||
|
||||
## Minimal Path to Awesome
|
||||
- clone this repo
|
||||
- `$ npm i`
|
||||
- `$ gulp trust-dev-cert`
|
||||
- `$ gulp serve `
|
||||
|
||||
#### Local Mode
|
||||
A browser in local mode (localhost) will be opened.
|
||||
https://localhost:4321/temp/workbench.html
|
||||
|
||||
#### SharePoint Mode
|
||||
If you want to try on a real environment, open:
|
||||
https://your-domain.sharepoint.com/_layouts/15/workbench.aspx
|
||||
|
||||
## Usage
|
||||
Go to src/webparts/taxonomyPickerSample/TaxonomyPickerSampleWebPart.ts and configure the control.
|
||||
#### Mock Data configuration by using defaultOptions configuration
|
||||
Access from https://localhost:4321/temp/workbench.html
|
||||
Configuration:
|
||||
```js
|
||||
PropertyPaneTaxonomyPicker("Language", {
|
||||
key: "Language_Field",
|
||||
name: "Language",
|
||||
displayName: "Language",
|
||||
multi: true,
|
||||
defaultOptions: [
|
||||
{ label: "English", value: "f50249b6-310d-43b6-aaa6-f0cb46d851bf" },
|
||||
{ label: "Spanish", value: "237ca323-1ed8-4199-a49b-a9f7ce4256bf" },
|
||||
{ label: "German", value: "44024c7e-f738-4755-90e1-15866327c806" },
|
||||
{ label: "Italian", value: "65f67491-bdca-491a-84fa-f6fd913f40fa" },
|
||||
],
|
||||
onPickerChange: this._updateTaxonomyPicker
|
||||
})
|
||||
```
|
||||
#### Real Data configuration by using termSetGuid configuration
|
||||
Access from https://your-domain.sharepoint.com/_layouts/15/workbench.aspx
|
||||
Configuration:
|
||||
```js
|
||||
PropertyPaneTaxonomyPicker("Language", {
|
||||
key: "Language_Field",
|
||||
name: "Language",
|
||||
displayName: "Language",
|
||||
multi: true,
|
||||
termSetGuid: "<your-term-set-id>",
|
||||
termSetName: "Language",
|
||||
termSetCountMaxSwapToAsync: 100,
|
||||
onPickerChange: this._updateTaxonomyPicker
|
||||
})
|
||||
```
|
||||
|
||||
## Features
|
||||
- Use [React](https://facebook.github.io/react) for using [react-taxonomypicker](https://github.com/jquintozamora/react-taxonomypicker) control inside a custom property pane (it can be used in the webpart itself too).
|
||||
- Use [TypeScript](https://www.typescriptlang.org) to create the custom property pane control containing the taxonomy picker control.
|
||||
- **Reacting to web part property changes**
|
||||
- Loading data for use in **custom property pane controls asynchronously** without blocking the web part
|
||||
- **Retrieve Terms** from a Term Set by Term Set GUID.
|
||||
- **Sync / Async modes**
|
||||
- Sync mode is used for medium / small Term Sets and loads **asynchronously** all Terms and store them in React State and SessionStorage cache.
|
||||
- Async mode is used for large Term Sets and it **doesn't load any data initially**, but it loads the Terms upon user input in batches of 10 items.
|
||||
- Sync / Async mode configurable via **termSetCountMaxSwapToAsync** property
|
||||
- The control will fetch the number of terms and decide which mode to use depends on termSetCountMaxSwapToAsync value.
|
||||
- It Uses and depends on **SP.Taxonomy.js** (the webpart uses a wrapper to load all the SP.*.js dependencies)
|
||||
- Use **Promise** (polyfill it if needed IE)
|
||||
- **onPickerChange** event handler exposed
|
||||
- [react-select](https://github.com/JedWatson/react-select) properties exposed (extends them)
|
||||
- defaultOptions array exposed to enable input **mock data** when no termSetGuid configured
|
||||
- More configuration options: [react-taxonomypicker](https://www.npmjs.com/package/react-taxonomypicker)
|
||||
|
||||
## Scenarios supported
|
||||
- SharePoint Web Part using **Script Editor** or **Content Editor Web Part**
|
||||
- Consume it from [ES6 project](https://github.com/jquintozamora/react-taxonomypicker-consume-es6)
|
||||
- Consume it from [TypeScript project](https://github.com/jquintozamora/react-taxonomypicker-consume-typescript)
|
||||
- **SharePoint Framework Web Part (SPFx)**
|
||||
|
||||
|
||||
<img src="https://telemetry.sharepointpnp.com/sp-dev-fx-webparts/samples/spfx-react-taxonomypicker" />
|
||||
|
Binary file not shown.
After Width: | Height: | Size: 494 KiB |
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"entries": [
|
||||
{
|
||||
"entry": "./lib/webparts/taxonomyPickerSample/TaxonomyPickerSampleWebPart.js",
|
||||
"manifest": "./src/webparts/taxonomyPickerSample/TaxonomyPickerSampleWebPart.manifest.json",
|
||||
"outputPath": "./dist/taxonomy-picker-sample.bundle.js"
|
||||
}
|
||||
],
|
||||
"externals": {},
|
||||
"localizedResources": {
|
||||
"taxonomyPickerSampleStrings": "webparts/taxonomyPickerSample/loc/{locale}.js"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"deployCdnPath": "temp/deploy"
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"workingDir": "./temp/deploy/",
|
||||
"account": "<!-- STORAGE ACCOUNT NAME -->",
|
||||
"container": "react-taxonomypicker-sample-spfx",
|
||||
"accessKey": "<!-- ACCESS KEY -->"
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"solution": {
|
||||
"name": "react-taxonomypicker-sample-spfx-client-side-solution",
|
||||
"id": "90551413-582d-4e75-b811-2459984d62d8",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
"paths": {
|
||||
"zippedPackage": "solution/react-taxonomypicker-sample-spfx.sppkg"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"port": 4321,
|
||||
"initialPage": "https://localhost:5432/workbench",
|
||||
"https": true,
|
||||
"api": {
|
||||
"port": 5432,
|
||||
"entryPath": "node_modules/@microsoft/sp-webpart-workbench/lib/api/"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
{
|
||||
// Display errors as warnings
|
||||
"displayAsWarning": true,
|
||||
// The TSLint task may have been configured with several custom lint rules
|
||||
// before this config file is read (for example lint rules from the tslint-microsoft-contrib
|
||||
// project). If true, this flag will deactivate any of these rules.
|
||||
"removeExistingRules": true,
|
||||
// When true, the TSLint task is configured with some default TSLint "rules.":
|
||||
"useDefaultConfigAsBase": false,
|
||||
// Since removeExistingRules=true and useDefaultConfigAsBase=false, there will be no lint rules
|
||||
// which are active, other than the list of rules below.
|
||||
"lintConfig": {
|
||||
// Opt-in to Lint rules which help to eliminate bugs in JavaScript
|
||||
"rules": {
|
||||
"class-name": false,
|
||||
"export-name": false,
|
||||
"forin": false,
|
||||
"label-position": false,
|
||||
"member-access": true,
|
||||
"no-arg": false,
|
||||
"no-console": false,
|
||||
"no-construct": false,
|
||||
"no-duplicate-case": true,
|
||||
"no-duplicate-variable": true,
|
||||
"no-eval": false,
|
||||
"no-function-expression": true,
|
||||
"no-internal-module": true,
|
||||
"no-shadowed-variable": true,
|
||||
"no-switch-case-fall-through": true,
|
||||
"no-unnecessary-semicolons": true,
|
||||
"no-unused-expression": true,
|
||||
"no-unused-imports": true,
|
||||
"no-use-before-declare": true,
|
||||
"no-with-statement": true,
|
||||
"semicolon": true,
|
||||
"trailing-comma": false,
|
||||
"typedef": false,
|
||||
"typedef-whitespace": false,
|
||||
"use-named-parameter": true,
|
||||
"valid-typeof": true,
|
||||
"variable-name": false,
|
||||
"whitespace": false,
|
||||
"prefer-const": true
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"cdnBasePath": "<!-- PATH TO CDN -->"
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
const gulp = require('gulp');
|
||||
const build = require('@microsoft/sp-build-web');
|
||||
|
||||
build.initialize(gulp);
|
|
@ -0,0 +1,37 @@
|
|||
{
|
||||
"name": "react-taxonomypicker-spfx",
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@microsoft/sp-client-base": "~1.0.0",
|
||||
"@microsoft/sp-core-library": "~1.0.0",
|
||||
"@microsoft/sp-webpart-base": "~1.0.0",
|
||||
"@types/react": "0.14.46",
|
||||
"@types/react-addons-shallow-compare": "0.14.17",
|
||||
"@types/react-addons-test-utils": "0.14.15",
|
||||
"@types/react-addons-update": "0.14.14",
|
||||
"@types/react-dom": "0.14.18",
|
||||
"@types/webpack-env": ">=1.12.1 <1.14.0",
|
||||
"react": "15.4.2",
|
||||
"react-dom": "15.4.2",
|
||||
"react-taxonomypicker": "0.0.26"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@microsoft/sp-build-web": "~1.0.0",
|
||||
"@microsoft/sp-module-interfaces": "~1.0.0",
|
||||
"@microsoft/sp-webpart-workbench": "~1.0.0",
|
||||
"@types/chai": ">=3.4.34 <3.6.0",
|
||||
"@types/microsoft-ajax": "0.0.31",
|
||||
"@types/mocha": ">=2.2.33 <2.6.0",
|
||||
"@types/sharepoint": "^2013.1.2",
|
||||
"gulp": "~3.9.1"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "gulp bundle",
|
||||
"clean": "gulp clean",
|
||||
"test": "gulp test"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
import { IPropertyPaneTaxonomyPickerProps } from './IPropertyPaneTaxonomyPickerProps';
|
||||
|
||||
export interface IPropertyPaneTaxonomyPickerInternalProps extends IPropertyPaneTaxonomyPickerProps {
|
||||
onRender(elem: HTMLElement): void;
|
||||
onDispose(elem: HTMLElement): void;
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
import { ITaxonomyPickerLoaderProps } from "./components/ITaxonomyPickerLoaderProps";
|
||||
export interface IPropertyPaneTaxonomyPickerProps extends ITaxonomyPickerLoaderProps {
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
import * as React from 'react';
|
||||
import * as ReactDom from 'react-dom';
|
||||
|
||||
import {
|
||||
IPropertyPaneField,
|
||||
PropertyPaneFieldType
|
||||
} from '@microsoft/sp-webpart-base';
|
||||
|
||||
import { IPropertyPaneTaxonomyPickerProps } from './IPropertyPaneTaxonomyPickerProps';
|
||||
import { IPropertyPaneTaxonomyPickerInternalProps } from './IPropertyPaneTaxonomyPickerInternalProps';
|
||||
|
||||
import TaxonomyPickerLoader from "./components/TaxonomyPickerLoader";
|
||||
import { ITaxonomyPickerLoaderProps } from "./components/ITaxonomyPickerLoaderProps";
|
||||
|
||||
|
||||
class PropertyPaneTaxonomyPickerBuilder implements IPropertyPaneField<IPropertyPaneTaxonomyPickerInternalProps> {
|
||||
public type: PropertyPaneFieldType = PropertyPaneFieldType.Custom;
|
||||
public targetProperty: string;
|
||||
public properties: IPropertyPaneTaxonomyPickerInternalProps;
|
||||
|
||||
public constructor(targetProperty: string, properties: IPropertyPaneTaxonomyPickerProps) {
|
||||
this.properties = {
|
||||
...properties,
|
||||
onRender: this.render,
|
||||
onDispose: this.dispose
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @function
|
||||
* Render the logging element
|
||||
*/
|
||||
public render = (elem: HTMLElement): void => {
|
||||
const element: React.ReactElement<ITaxonomyPickerLoaderProps> = React.createElement(TaxonomyPickerLoader, {
|
||||
...this.properties,
|
||||
|
||||
});
|
||||
ReactDom.render(element, elem);
|
||||
}
|
||||
|
||||
private dispose(elem: HTMLElement): void { }
|
||||
|
||||
|
||||
}
|
||||
|
||||
export function PropertyPaneTaxonomyPicker(targetProperty: string, properties: IPropertyPaneTaxonomyPickerProps): IPropertyPaneField<IPropertyPaneTaxonomyPickerInternalProps> {
|
||||
// create an internal properties object from the given properties
|
||||
var newProperties: IPropertyPaneTaxonomyPickerInternalProps = {
|
||||
...properties,
|
||||
onDispose: null,
|
||||
onRender: null
|
||||
};
|
||||
|
||||
// calles the PropertyPaneLoggingField builder object
|
||||
// this object will simulate a PropertyFieldCustom to manage his rendering process
|
||||
return new PropertyPaneTaxonomyPickerBuilder(targetProperty, newProperties);
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
import { ITaxonomyPickerProps } from "react-taxonomypicker";
|
||||
export interface ITaxonomyPickerLoaderProps extends ITaxonomyPickerProps {
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
export interface ITaxonomyPickerLoaderState {
|
||||
loadingScripts: boolean;
|
||||
errors?: string[];
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
import * as React from 'react';
|
||||
import { ITaxonomyPickerLoaderProps } from './ITaxonomyPickerLoaderProps';
|
||||
import { ITaxonomyPickerLoaderState } from './ITaxonomyPickerLoaderState';
|
||||
import { SPComponentLoader } from '@microsoft/sp-loader';
|
||||
import { Utils } from "../../../utils/Utils";
|
||||
|
||||
import { ServiceScope, Environment, EnvironmentType } from '@microsoft/sp-core-library';
|
||||
|
||||
import TaxonomyPicker, { ITaxonomyPickerProps } from "react-taxonomypicker";
|
||||
import "react-taxonomypicker/dist/React.TaxonomyPicker.css";
|
||||
|
||||
export default class TaxonomyPickerLoader extends React.Component<ITaxonomyPickerLoaderProps, ITaxonomyPickerLoaderState> {
|
||||
constructor(props: ITaxonomyPickerLoaderProps, state: ITaxonomyPickerLoaderState) {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
loadingScripts: true,
|
||||
errors: []
|
||||
};
|
||||
}
|
||||
|
||||
public componentDidMount(): void {
|
||||
// based on the type of environment, return the correct instance of the IUserProfileService interface
|
||||
if (Environment.type === EnvironmentType.SharePoint || Environment.type === EnvironmentType.ClassicSharePoint) {
|
||||
this._loadSPJSOMScripts();
|
||||
} else {
|
||||
this.setState({ loadingScripts: false, errors: [...this.state.errors, "You are on localhost mode (EnvironmentType.Local), be sure you disable termSetGuid and enable defaultOptions configuration in PropertyPaneTaxonomyPicker."] });
|
||||
}
|
||||
}
|
||||
|
||||
public render(): JSX.Element {
|
||||
return (
|
||||
<div>
|
||||
{this.state.loadingScripts === false ?
|
||||
<TaxonomyPicker
|
||||
{...this.props}
|
||||
isLoading={this.state.loadingScripts}
|
||||
/>
|
||||
:
|
||||
null
|
||||
}
|
||||
{this.state.errors.length > 0 ? this.renderErrorMessage() : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
private _loadSPJSOMScripts() {
|
||||
const siteColUrl = Utils.getSiteCollectionUrl();
|
||||
try {
|
||||
SPComponentLoader.loadScript(siteColUrl + '/_layouts/15/init.js', {
|
||||
globalExportsName: '$_global_init'
|
||||
})
|
||||
.then((): Promise<{}> => {
|
||||
return SPComponentLoader.loadScript(siteColUrl + '/_layouts/15/MicrosoftAjax.js', {
|
||||
globalExportsName: 'Sys'
|
||||
});
|
||||
})
|
||||
.then((): Promise<{}> => {
|
||||
return SPComponentLoader.loadScript(siteColUrl + '/_layouts/15/SP.Runtime.js', {
|
||||
globalExportsName: 'SP'
|
||||
});
|
||||
})
|
||||
.then((): Promise<{}> => {
|
||||
return SPComponentLoader.loadScript(siteColUrl + '/_layouts/15/SP.js', {
|
||||
globalExportsName: 'SP'
|
||||
});
|
||||
})
|
||||
.then((): Promise<{}> => {
|
||||
return SPComponentLoader.loadScript(siteColUrl + '/_layouts/15/SP.taxonomy.js', {
|
||||
globalExportsName: 'SP'
|
||||
});
|
||||
})
|
||||
.then((): void => {
|
||||
this.setState({ loadingScripts: false });
|
||||
})
|
||||
.catch((reason: any) => {
|
||||
this.setState({ loadingScripts: false, errors: [...this.state.errors, reason] });
|
||||
});
|
||||
} catch (error) {
|
||||
this.setState({ loadingScripts: false, errors: [...this.state.errors, error] });
|
||||
}
|
||||
}
|
||||
|
||||
private renderErrorMessage() {
|
||||
return (
|
||||
<div>
|
||||
{this.state.errors}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
export class Utils {
|
||||
/*
|
||||
* Function to get Site Collection URL
|
||||
* Samples:
|
||||
* "https://domain.sharepoint.com/sites/intranet"
|
||||
*/
|
||||
public static getSiteCollectionUrl(): string {
|
||||
let baseUrl = window.location.protocol + "//" + window.location.host;
|
||||
const pathname = window.location.pathname;
|
||||
const siteCollectionDetector = "/sites/";
|
||||
if (pathname.indexOf(siteCollectionDetector) >= 0) {
|
||||
baseUrl += pathname.substring(0, pathname.indexOf("/", siteCollectionDetector.length));
|
||||
}
|
||||
return baseUrl;
|
||||
}
|
||||
|
||||
/*
|
||||
* Function to get Current Site Url
|
||||
* Samples:
|
||||
* "https://domain.sharepoint.com/sites/intranet/subsite/Pages/Home.aspx"
|
||||
*/
|
||||
public static getCurrentAbsoluteSiteUrl(): string {
|
||||
if (window
|
||||
&& window.hasOwnProperty("location")
|
||||
&& window.location.hasOwnProperty("protocol")
|
||||
&& window.location.hasOwnProperty("host")
|
||||
&& window.location.hasOwnProperty("pathname")) {
|
||||
return window.location.protocol + "//" + window.location.host + window.location.pathname;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Function to get Current Site Url
|
||||
* Samples:
|
||||
* "/sites/intranet"
|
||||
*/
|
||||
public static getWebServerRelativeUrl(): string {
|
||||
if (window
|
||||
&& window.hasOwnProperty("location")
|
||||
&& window.location.hasOwnProperty("pathname")) {
|
||||
return window.location.pathname.replace(/\/$/, "");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Function to get Layout Page Url
|
||||
* Replacement in SPFx for SP.Utilities.Utility.getLayoutsPageUrl('sp.js')
|
||||
* Samples:
|
||||
* getLayoutsPageUrl('sp.js')
|
||||
* "/sites/intranet/_layouts/15/sp.js"
|
||||
*/
|
||||
public static getLayoutsPageUrl(libraryName: string): string {
|
||||
if (window
|
||||
&& window.hasOwnProperty("location")
|
||||
&& window.location.hasOwnProperty("pathname")
|
||||
&& libraryName !== "") {
|
||||
return window.location.pathname.replace(/\/$/, "") + "/_layouts/15/" + libraryName;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Function to get Current Domain Url
|
||||
* Samples:
|
||||
* "https://domain.sharepoint.com"
|
||||
*/
|
||||
public static getAbsoluteDomainUrl(): string {
|
||||
if (window
|
||||
&& window.hasOwnProperty("location")
|
||||
&& window.location.hasOwnProperty("protocol")
|
||||
&& window.location.hasOwnProperty("host")) {
|
||||
return window.location.protocol + "//" + window.location.host;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
export interface ITaxonomyPickerSampleWebPartProps {
|
||||
pickerValue: string | string[];
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"$schema": "../../../node_modules/@microsoft/sp-module-interfaces/lib/manifestSchemas/jsonSchemas/clientSideComponentManifestSchema.json",
|
||||
|
||||
"id": "fbbd4504-22be-4b32-808f-adcfa956cf4a",
|
||||
"alias": "TaxonomyPickerSampleWebPart",
|
||||
"componentType": "WebPart",
|
||||
"version": "0.0.1",
|
||||
"manifestVersion": 2,
|
||||
|
||||
"preconfiguredEntries": [{
|
||||
"groupId": "fbbd4504-22be-4b32-808f-adcfa956cf4a",
|
||||
"group": { "default": "Under Development" },
|
||||
"title": { "default": "TaxonomyPickerSample" },
|
||||
"description": { "default": "TaxonomyPickerSample description" },
|
||||
"officeFabricIconFontName": "Page",
|
||||
"properties": {
|
||||
"description": "TaxonomyPickerSample"
|
||||
}
|
||||
}]
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
import * as React from 'react';
|
||||
import * as ReactDom from 'react-dom';
|
||||
import { Version } from '@microsoft/sp-core-library';
|
||||
import {
|
||||
BaseClientSideWebPart,
|
||||
IPropertyPaneConfiguration
|
||||
} from '@microsoft/sp-webpart-base';
|
||||
|
||||
import * as strings from 'taxonomyPickerSampleStrings';
|
||||
import TaxonomyPickerSample from './components/TaxonomyPickerSample';
|
||||
import { ITaxonomyPickerSampleProps } from './components/ITaxonomyPickerSampleProps';
|
||||
import { ITaxonomyPickerSampleWebPartProps } from './ITaxonomyPickerSampleWebPartProps';
|
||||
|
||||
import { PropertyPaneTaxonomyPicker } from '../../controls/PropertyPaneTaxonomyPicker/PropertyPaneTaxonomyPicker';
|
||||
|
||||
import { Log } from "@microsoft/sp-core-library";
|
||||
|
||||
export default class TaxonomyPickerSampleWebPart extends BaseClientSideWebPart<ITaxonomyPickerSampleWebPartProps> {
|
||||
|
||||
|
||||
public render(): void {
|
||||
const element: React.ReactElement<ITaxonomyPickerSampleProps> = React.createElement(
|
||||
TaxonomyPickerSample,
|
||||
{
|
||||
description: "Open Pane Property panel to pick some languages...",
|
||||
pickerValue: this.properties.pickerValue
|
||||
}
|
||||
);
|
||||
Log.verbose("TaxonomyPickerSampleWebPart", "In render of TaxonomyPickerSampleWebPart");
|
||||
ReactDom.render(element, this.domElement);
|
||||
}
|
||||
|
||||
protected get dataVersion(): Version {
|
||||
return Version.parse('1.0');
|
||||
}
|
||||
|
||||
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
|
||||
return {
|
||||
pages: [
|
||||
{
|
||||
header: {
|
||||
description: strings.PropertyPaneDescription
|
||||
},
|
||||
groups: [
|
||||
{
|
||||
groupName: strings.BasicGroupName,
|
||||
groupFields: [
|
||||
PropertyPaneTaxonomyPicker("Language", {
|
||||
key: "Language_Field",
|
||||
name: "Language",
|
||||
displayName: "Language",
|
||||
multi: true,
|
||||
// termSetGuid: "26ebf149-101a-4996-9df2-8179a537350d",
|
||||
// termSetName: "Language",
|
||||
termSetCountMaxSwapToAsync: 100,
|
||||
defaultOptions: [
|
||||
{ label: "English", value: "f50249b6-310d-43b6-aaa6-f0cb46d851bf" },
|
||||
{ label: "Spanish", value: "237ca323-1ed8-4199-a49b-a9f7ce4256bf" },
|
||||
{ label: "German", value: "44024c7e-f738-4755-90e1-15866327c806" },
|
||||
{ label: "Italian", value: "65f67491-bdca-491a-84fa-f6fd913f40fa" },
|
||||
],
|
||||
onPickerChange: this._updateTaxonomyPicker
|
||||
})
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
private _updateTaxonomyPicker = (name, value) => {
|
||||
console.log(name + ": ");
|
||||
console.log(value);
|
||||
if (value !== null && value !== undefined) {
|
||||
if (value.hasOwnProperty("length")) {
|
||||
this.properties.pickerValue = value.map((item) => item.label).join(", ");
|
||||
} else {
|
||||
this.properties.pickerValue = value.toString();
|
||||
}
|
||||
this.render();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
export interface ITaxonomyPickerSampleProps {
|
||||
description: string;
|
||||
pickerValue: string | string[];
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
.helloWorld {
|
||||
.container {
|
||||
max-width: 700px;
|
||||
margin: 0px auto;
|
||||
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), 0 25px 50px 0 rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.row {
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.listItem {
|
||||
max-width: 715px;
|
||||
margin: 5px auto 5px auto;
|
||||
box-shadow: 0 0 4px 0 rgba(0, 0, 0, 0.2), 0 25px 50px 0 rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.button {
|
||||
// Our button
|
||||
text-decoration: none;
|
||||
height: 32px;
|
||||
|
||||
// Primary Button
|
||||
min-width: 80px;
|
||||
background-color: #0078d7;
|
||||
border-color: #0078d7;
|
||||
color: #ffffff;
|
||||
|
||||
// Basic Button
|
||||
outline: transparent;
|
||||
position: relative;
|
||||
font-family: "Segoe UI WestEuropean","Segoe UI",-apple-system,BlinkMacSystemFont,Roboto,"Helvetica Neue",sans-serif;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
font-size: 14px;
|
||||
font-weight: 400;
|
||||
border-width: 0;
|
||||
text-align: center;
|
||||
cursor: pointer;
|
||||
display: inline-block;
|
||||
padding: 0 16px;
|
||||
|
||||
.label {
|
||||
font-weight: 600;
|
||||
font-size: 14px;
|
||||
height: 32px;
|
||||
line-height: 32px;
|
||||
margin: 0 4px;
|
||||
vertical-align: top;
|
||||
display: inline-block;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
import * as React from 'react';
|
||||
import styles from './TaxonomyPickerSample.module.scss';
|
||||
import { ITaxonomyPickerSampleProps } from './ITaxonomyPickerSampleProps';
|
||||
import { escape } from '@microsoft/sp-lodash-subset';
|
||||
|
||||
export default class TaxonomyPickerSample extends React.Component<ITaxonomyPickerSampleProps, void> {
|
||||
|
||||
public render(): React.ReactElement<ITaxonomyPickerSampleProps> {
|
||||
return (
|
||||
<div className={styles.helloWorld}>
|
||||
<div className={styles.container}>
|
||||
<div className={`ms-Grid-row ms-bgColor-themeDark ms-fontColor-white ${styles.row}`}>
|
||||
<div className="ms-Grid-col ms-u-lg10 ms-u-xl8 ms-u-xlPush2 ms-u-lgPush1">
|
||||
<span className="ms-font-xl ms-fontColor-white">Welcome to Taxonomy Picker demo webpart!</span>
|
||||
<p className="ms-font-l ms-fontColor-white">{escape(this.props.description)}</p>
|
||||
<p className="ms-font-l ms-fontColor-white">
|
||||
<strong>Selected Languages: {this.props.pickerValue || 'none selected'}</strong>
|
||||
</p>
|
||||
<a href="https://aka.ms/spfx" className={styles.button}>
|
||||
<span className={styles.label}>Learn more</span>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
define([], function() {
|
||||
return {
|
||||
"PropertyPaneDescription": "Description",
|
||||
"BasicGroupName": "Group Name",
|
||||
"DescriptionFieldLabel": "Description Field"
|
||||
}
|
||||
});
|
10
samples/react-taxonomypicker/src/webparts/taxonomyPickerSample/loc/mystrings.d.ts
vendored
Normal file
10
samples/react-taxonomypicker/src/webparts/taxonomyPickerSample/loc/mystrings.d.ts
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
declare interface ITaxonomyPickerSampleStrings {
|
||||
PropertyPaneDescription: string;
|
||||
BasicGroupName: string;
|
||||
DescriptionFieldLabel: string;
|
||||
}
|
||||
|
||||
declare module 'taxonomyPickerSampleStrings' {
|
||||
const strings: ITaxonomyPickerSampleStrings;
|
||||
export = strings;
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
/// <reference types="mocha" />
|
||||
|
||||
import { assert } from 'chai';
|
||||
|
||||
describe('TaxonomyPickerSampleWebPart', () => {
|
||||
it('should do something', () => {
|
||||
assert.ok(true);
|
||||
});
|
||||
});
|
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es5",
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"module": "commonjs",
|
||||
"jsx": "react",
|
||||
"declaration": true,
|
||||
"sourceMap": true,
|
||||
"types": [
|
||||
"es6-promise",
|
||||
"es6-collections",
|
||||
"webpack-env",
|
||||
"microsoft-ajax",
|
||||
"sharepoint"
|
||||
]
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
// Type definitions for Microsoft ODSP projects
|
||||
// Project: ODSP
|
||||
|
||||
/* Global definition for UNIT_TEST builds
|
||||
Code that is wrapped inside an if(UNIT_TEST) {...}
|
||||
block will not be included in the final bundle when the
|
||||
--ship flag is specified */
|
||||
declare const UNIT_TEST: boolean;
|
|
@ -0,0 +1 @@
|
|||
/// <reference path="@ms/odsp.d.ts" />
|
Loading…
Reference in New Issue