Friday, April 26, 2019

Which React Dorpdown or Selection component you should use in SharePoint framework SPFx React project?

When we use Office Fabric UI Dropdown component, we run into a issue that the value in the SharePoint list item value could not be displayed. As a result, we are looking for alternative for multiple dropdown component. Here is the list of the React dowpdown components we evaluated and my suggestion.

1. React-dropdown is a good candidate and it's extremely easy to configured. However, the issue is this is only support single selection. If you need multiple selection, this is not for you.

2. React-muti-select is another good example. However, the selected options will be displayed in another window as below picture that makes the UI not friendly.



3. Multi-select-react seems to be a nice component. However, there is an error when running this with SPFx React. This might be the version compatibility issue.

Error - [tsc] C:/Harry/Projects/Pubs/POC/Support/LookUp/node_modules/typescript/lib/tsc.js:55776
                throw e;
                ^
Error: Debug Failure.
    at typeToString (C:/Harry/Projects/Pubs/POC/Support/LookUp/node_modules/typescript/lib/tsc.js:23526:22)
    at reportRelationError (C:/Harry/Projects/Pubs/POC/Support/LookUp/node_modules/typescript/lib/tsc.js:28855:34)
    at isRelatedTo (C:/Harry/Projects/Pubs/POC/Support/LookUp/node_modules/typescript/lib/tsc.js:28972:21)
    at propertiesRelatedTo (C:/Harry/Projects/Pubs/POC/Support/LookUp/node_modules/typescript/lib/tsc.js:29382:43)
    at structuredTypeRelatedTo (C:/Harry/Projects/Pubs/POC/Support/LookUp/node_modules/typescript/lib/tsc.js:29275:38)
    at recursiveTypeRelatedTo (C:/Harry/Projects/Pubs/POC/Support/LookUp/node_modules/typescript/lib/tsc.js:29172:53) 

4. React-select-box also has an error as below.


sp-webpart-workbench-assembly_en-us_1a783b02ca664f8c65a409008ba8cbb9.js:204 Uncaught (in promise) Invariant Violation: Minified React error #130; visit https://reactjs.org/docs/error-decoder.html?invariant=130&args[]=undefined&args[]= for the full message or use the non-minified dev environment for full errors and additional helpful warnings.


5. One of the best options is from Semantic-Org. This package includes many components and css. There are many examples to use this in React project. Here is the dropdown component.


The issue I'm facing is the instructions to incorporate in the SPFx project. I have difficult to install and use the component. The css does not seem to play well with other React component. I would like to revisit when I have time.

6. The final one is the react-select component. The installation and example given are simple. One issue is the multiple selection is not user friendly. User has to select one option at a time unlike other React Drowdown you can click multiple values at one time.



It only take about 10 minutes and here is the dropdown I put together in a POC project. I've verified that this component indeed could display the SharePoint values from list item. Hope I can replace this component after Microsoft resolved the Office Fabric UI dropdown bug.


Thursday, April 25, 2019

Cannot display selected office fabric ui dropdown value from SharePoint framework

We are using SharePoint framework with office fabric UI component. The current project is to use SPFx to display an list item that has a Dropdown field. The issue is we are getting the value all options as an array from the SharePoint but cannot display the option value already set in the list.

After few days debugging the issue, we have isolated to the SharePoint framework REACT how to pass the pros from parent to child component. Here is the simple code to reproduce this issue.

1. Hard coded the array like this below in parent component named LoopUp.

private defaultOptionList: IDropdownOption[] = [
{
key: '0',
text: 'None',
isSelected: false
},
{
key: '1',
text: 'Encore/Review Publication',
isSelected: true
},
{
key: '2',
text: 'Non-Scientific',
isSelected: false
},
{
key: '3',
text: 'Assay Method',
isSelected: false
},
{
key: '4',
text: 'Research Data',
isSelected: false
},
{
key: '5',
text: 'PK/Tox Data',
isSelected: false
},
{
key: '6',
text: 'Clinical Data',
isSelected: false
},
{
key: '7',
text: 'Clinical Study Design',
isSelected: false
},
{
key: '8',
text: 'Clintrials.gov Posting',
isSelected: false
}

];

2. If we initialize the state in the constructor below and pass to child, child can display the selected option value.  The state we set up is named submissionContentListState2.

constructor(props) {

super(props);
this.state = {
submissionContentListState2: [] = this.defaultOptionList, // Set in constructor
submissionContentListState4: [] // Set in componentDidMount
};

}

3. If we set the same value from  componentDidMount  like below, child cannot display the selected value. The state we set up is named submissionContentListState4.

public componentDidMount() {
this.setState({
submissionContentListState4: this.defaultOptionList
});
}           

The code to pass to child is listed below.
public render(): React.ReactElement<ILoopUpProps> {

SPFacade.logOptions(this.state.submissionContentListState2, "Parent Rendor submissionContentListState2");
SPFacade.logOptions(this.state.submissionContentListState4, "Parent Rendor submissionContentListState4");
return (
<Child
defaultOptionListParent2={this.state.submissionContentListState2} // This is in constructor
defaultOptionListParent4={this.state.submissionContentListState4} // This is in componentDidMount
/>

);
}


The code in child is listed below.
import * as React from 'react';
import styles from './LoopUp.module.scss';
import { ILoopUpProps } from './ILoopUpProps';
import { Dropdown, IDropdownOption } from "office-ui-fabric-react";
import { SPFacade } from "./SPFacade";

export class Child extends React.Component<any,
{
}
> {
constructor(props) {
// Required step: always call the parent class' constructor
super(props);
}
public render(): React.ReactElement<ILoopUpProps> {

SPFacade.logOptions(this.props.submissionContentListState2, "Child Rendor submissionContentListState2");
SPFacade.logOptions(this.props.submissionContentListState4, "Child Rendor submissionContentListState4");
return (
<div className={ styles.loopUp }>
<div className={ styles.container }>

<div className={ styles.row }>
<div className={ styles.column }>
<Dropdown
placeHolder="Select a content type"
required={true}
label="Lokup values from parent props set in constructor - working"
options={this.props.defaultOptionListParent2}
/>
</div>
</div>

<div className={ styles.row }>
<div className={ styles.column }>
<Dropdown
placeHolder="Select a content type"
required={true}
label="Lokup values from parent props set in componentDidMount - not working"
options={this.props.defaultOptionListParent4}
/>
</div>
</div>

</div>
</div>
);
}

}


The result is show as below.

You can see the top DOM element did show the selected value named "Encore/Review Publications". However, the bottom DOM element could not display the selected value even all the option values are retrieved.

This is a show stopper for us at this point since we need to get the value from SharePoint list item and it should be done in componentDidMount function and cannot be in constructor.

If you know the solution or workaround, please let me know.

The complete parent code is listed below.

import * as React from 'react';
import { ILoopUpProps } from './ILoopUpProps';
import { IDropdownOption } from "office-ui-fabric-react";
import { Child } from "./Child";
import { SPFacade } from "./SPFacade";

export default class LoopUp extends React.Component<ILoopUpProps,
{
submissionContentListState2: IDropdownOption[];
submissionContentListState4: IDropdownOption[];
}
> {

private defaultOptionList: IDropdownOption[] = [
{
key: '0',
text: 'None',
isSelected: false
},
{
key: '1',
text: 'Encore/Review Publication',
isSelected: true
},
{
key: '2',
text: 'Non-Scientific',
isSelected: false
},
{
key: '3',
text: 'Assay Method',
isSelected: false
},
{
key: '4',
text: 'Research Data',
isSelected: false
},
{
key: '5',
text: 'PK/Tox Data',
isSelected: false
},
{
key: '6',
text: 'Clinical Data',
isSelected: false
},
{
key: '7',
text: 'Clinical Study Design',
isSelected: false
},
{
key: '8',
text: 'Clintrials.gov Posting',
isSelected: false
}

];

constructor(props) {

super(props);
this.state = {
submissionContentListState2: [] = this.defaultOptionList, // Set in constructor
submissionContentListState4: [] // Set in componentDidMount
};

}

// Get the itemId received form the parent and display attachment if itemId is not NaN
public componentDidMount() {
this.setState({
submissionContentListState4: this.defaultOptionList
});
}

public render(): React.ReactElement<ILoopUpProps> {

SPFacade.logOptions(this.state.submissionContentListState2, "Parent Rendor submissionContentListState2");
SPFacade.logOptions(this.state.submissionContentListState4, "Parent Rendor submissionContentListState4");
return (
<Child
defaultOptionListParent2={this.state.submissionContentListState2} // This is in constructor
defaultOptionListParent4={this.state.submissionContentListState4} // This is in componentDidMount
/>

);
}




}








Monday, April 15, 2019

Automate SPFx source code backup with gulp

As a developer working on SharePoint framework (SPFx) with Azure DevOps, it would be convenient to have a automation to back up the source code to local sometime. Here is the example to use gulp task. We would use backing up fiels for SPFx webparts or just components folder. You could modify for your own purpose.

To use it, you could run as the following commands.

gulp bakwebparts
gulp bakcom



gulp.task('bakcom', function() {
// copy all files in source/webparts/pubsWebPart/components to C:\PubsBackup\<year>-<month>-<date>-<hour>-<minute>\components
var backupRoot = "C://PubsBackup";

var d = new Date();
var m = Number(d.getMonth()) + 1;
var n = d.getFullYear() + '-' + m.toString() + '-' + d.getDate() + '-' + d.getHours() + '-' + d.getMinutes();

var pubsRoot = backupRoot + '/' + n;
var webparts = pubsRoot + '/webparts';
var pubsWebPart = webparts + '/pubsWebPart';
var component = pubsWebPart + '/components';
console.log("component" + component);
const folders = [
backupRoot,
pubsRoot,
webparts,
pubsWebPart,
component
];

folders.forEach(dir => {
if(!fs.existsSync(dir)) {
fs.mkdirSync(dir);
console.log('📁 folder created:', dir);
}
});

gulp.src('./src/webparts/pubsWebPart/components/*.*').pipe(gulp.dest(component));

});


gulp.task('bakwebparts', function() {
// copy all files in source/webparts/pubsWebPart/components to C:\PubsBackup\<year>-<month>-<date>-<hour>-<minute>\components
var backupRoot = "C://PubsBackup";

var d = new Date();
var m = Number(d.getMonth()) + 1;
var n = d.getFullYear() + '-' + m.toString() + '-' + d.getDate() + '-' + d.getHours() + '-' + d.getMinutes();

var pubsRoot = backupRoot + '/' + n;
var webparts = pubsRoot + '/webparts';
var pubsWebPart = webparts + '/pubsWebPart';
var component = pubsWebPart + '/components';
var loc = pubsWebPart + '/loc';
var services = pubsWebPart + '/services';
console.log("component" + component);
const folders = [
backupRoot,
pubsRoot,
webparts,
pubsWebPart,
component,
loc,
services
];

folders.forEach(dir => {
if(!fs.existsSync(dir)) {
fs.mkdirSync(dir);
console.log('📁 folder created:', dir);
}
});

gulp.src('./src/webparts/pubsWebPart/*.*').pipe(gulp.dest(pubsWebPart));
gulp.src('./src/webparts/pubsWebPart/components/*.*').pipe(gulp.dest(component));
gulp.src('./src/webparts/pubsWebPart/loc/*.*').pipe(gulp.dest(loc));
gulp.src('./src/webparts/pubsWebPart/services/*.*').pipe(gulp.dest(services));

});