Implementation Of Office UI Grid, Accordion, Datepicker, RichText Controls In SPFx

Introduction

In this article, we will learn about how to leverage the office UI fabric grid in our SPFx component and also the implementation of accordion, Datepicker, and richtext controls in our component.

OfficeUI Grid

As similar to bootstrap grid breakpoint, OfficeUI also has its own breakpoints.

Grid Layout

Breakpoint Range
small (SM) 320px - 479px
medium (MD) 480px - 639px
large (LG) 1023px
extra large (XL) 1365px
extra extra large (XXL) 1366px - 1919px
extra extra extra large (XXXL) 1920px and so on...

The screen is divided into 12 columns, here it's only available via Fabric Core CSS.

Import the below package in your TSX file, to use fabricui layout,

import 'office-ui-fabric-react/dist/css/fabric.css';

To use the grid in your component, you must use the below classes,

class="ms-Grid" --> this should be in top parent class

class="ms-Grid-row" --> this should be in adjacent parent class

eg,

<div className="ms-Grid-row">
  <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
    <TextField label="Disabled" disabled defaultValue="I am disabled" />
    </div>
    <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
    <TextField label="Standard" />
    </div>
    <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
    <TextField label="Required " required />
    </div>
  </div>

Here the number is the columns, which range from 1 - 12.

Push and pull

This is used to change the order,

Push--> push to right(left to right)

Pull--> pull from the right(right to left)

eg,

div class="ms-Grid-col ms-md4 ms-mdPush8">First in code</div>
<div class="ms-Grid-col ms-md8 ms-mdPull4">Second in code</div>

Accordion

This control is used to implement a collapsible segment in our SPFx component, this can be done by using @pnp/spfx-controls-react

 In npm installation,

npm install @pnp/spfx-controls-react --save --save-exact

Import statement,

import { Accordion } from "@pnp/spfx-controls-react/lib/Accordion";

eg,

<Accordion title="Section 1" defaultCollapsed={false}  key='1'>
    <div >
        <div className="ms-Grid-row">
            <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
                <TextField label="Disabled" disabled defaultValue="I am disabled" />
            </div>
        </div> 
    </div>
</Accordion>

Properties

title string
defaultCollapsed boolean
className string

Date picker

This control is used to implement datepicker in our SPFx component.

Npm installation,

npm install @fluentui/react

 Import statement,

import {
  DatePicker,
  DayOfWeek,
  Dropdown,
  IDropdownOption,
  mergeStyles,
  defaultDatePickerStrings,
} from '@fluentui/react';

To know more about all the properties visit here.

eg,

<DatePicker        
    placeholder="Select a date..."
    ariaLabel="Select a date"
    // DatePicker uses English strings by default. For localized apps, you must override this prop.
    strings={defaultDatePickerStrings}
/>

Rich text

Npm installation,

npm install @pnp/spfx-controls-react --save --save-exact

Import statements,

import { RichText } from "@pnp/spfx-controls-react/lib/RichText";

To know more about all the properties visit here.

eg,

<RichText     
    onChange={(text)=>this.onTextChange(text)}
/>

Steps

Open a command prompt and create a directory for the SPFx solution.

md spfx-MyAccordian

Navigate to the above-created directory.

cd spfx-MyAccordian

Run the Yeoman SharePoint Generator to create the solution.

yo @microsoft/sharepoint

Solution Name

Hit enter for the default name (spfx-MyAccordian in this case) or type in any other name for your solution.

Selected choice - Hit Enter

Target for the component

Here, we can select the target environment where we are planning to deploy the client web part; i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).

Selected choice - SharePoint Online only (latest).

Place of files

We may choose to use the same folder or create a subfolder for our solution.

Selected choice - same folder.

Deployment option

Selecting Y will allow the app to be deployed instantly to all sites and be accessible everywhere.

Selected choice - N (install on each site explicitly).

Permissions to access web APIs

Choose if the components in the solution require permission to access web APIs that are unique and not shared with other components in the tenant.

Selected choice - N (solution contains unique permissions)

Type of client-side component to create

We can choose to create a client-side web part or an extension. Choose the web part option.

Selected choice - WebPart

Web part name

Hit Enter to select the default name or type in any other name.

Selected choice - MyAccordian

Web part description

Hit Enter to select the default description or type in any other value.

Framework to use

Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.

Selected choice - React

The Yeoman generator will perform a scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.

Once the scaffolding process is completed, lock down the version of project dependencies by running the below command,

npm shrinkwrap

In the command prompt, type the below command to open the solution in the code editor of your choice.

npm install @pnp/spfx-controls-react --save --save-exact
npm install @fluentui/react

in MyAccordian.tsx,

import * as React from 'react';
import styles from './MyAccordian.module.scss';
import { IMyAccordianProps } from './IMyAccordianProps';
import { escape } from '@microsoft/sp-lodash-subset';
import 'office-ui-fabric-react/dist/css/fabric.css';
import { Checkbox, Stack } from '@fluentui/react';
import { Accordion } from "@pnp/spfx-controls-react/lib/Accordion";
import { TextField, MaskedTextField } from '@fluentui/react/lib/TextField';
import { DropdownMenuItemType, IDropdownStyles } from '@fluentui/react/lib/Dropdown';
import "bootstrap/dist/css/bootstrap.min.css"; 
import './mystyle.css'
import { Toggle } from '@fluentui/react/lib/Toggle';
import {
  DatePicker,
  DayOfWeek,
  Dropdown,
  IDropdownOption,
  mergeStyles,
  defaultDatePickerStrings,
} from '@fluentui/react';
import { Label } from '@fluentui/react/lib/Label';
const borderimg: any = require('./border.png'); 
import { DefaultButton, PrimaryButton } from '@fluentui/react/lib/Button';
const options: IDropdownOption[] = [
  { key: 'fruitsHeader', text: 'Fruits', itemType: DropdownMenuItemType.Header },
  { key: 'apple', text: 'Apple' },
  { key: 'banana', text: 'Banana' },
  { key: 'orange', text: 'Orange', disabled: true },
  { key: 'grape', text: 'Grape' },
  { key: 'divider_1', text: '-', itemType: DropdownMenuItemType.Divider },
  { key: 'vegetablesHeader', text: 'Vegetables', itemType: DropdownMenuItemType.Header },
  { key: 'broccoli', text: 'Broccoli' },
  { key: 'carrot', text: 'Carrot' },
  { key: 'lettuce', text: 'Lettuce' },
];
import { RichText } from "@pnp/spfx-controls-react/lib/RichText";
var styles1 = {
 //border: "2px red solid",
 // padding: 20
 border: "10px solid transparent",
  padding: "15px",
  "border-image": `url(${borderimg}) 25% round`
}
export interface StyleOptions {
  showMore: true,showAlign:true
}
export default class MyAccordian extends React.Component<IMyAccordianProps, {}> {
  public render(): React.ReactElement<IMyAccordianProps> {
    return (
      <div className="container">
  <div className="ms-Grid" style={ styles1}>
    <Accordion title="Section 1" defaultCollapsed={false} key='1'>
      <div>
        <div className="ms-Grid-row">
          <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
            <TextField label="Disabled" disabled defaultValue="I am disabled" />
          </div>
          <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
            <TextField label="Standard" />
          </div>
          <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
            <TextField label="Required " required />
          </div>
        </div>
        <div className="ms-Grid-row">
          <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
            <TextField label="Disabled" disabled defaultValue="I am disabled" />
          </div>
          <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
            <TextField label="Standard" />
          </div>
          <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
            <TextField label="Required " required />
          </div>
        </div>
      </div>
    </Accordion>
    <Accordion title="Section 2" defaultCollapsed={true} key='2'>
      <div>
        <div className="ms-Grid-row">
          <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
            <Checkbox label="Disabled checkbox" disabled />
          </div>
          <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
            <Dropdown placeholder="Select an option" options={options} />
          </div>
          <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
            <DatePicker placeholder="Select a date..." ariaLabel="Select a date" // DatePicker uses English strings by default. For localized apps, you must override this prop. strings={defaultDatePickerStrings} />
          </div>
        </div>
        <div className="ms-Grid-row">
          <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
            <Checkbox label="checkbox" />
          </div>
          <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
            <Dropdown placeholder="Select an option" options={options} />
          </div>
          <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
            <DatePicker formatDate={this.onFormatDate} placeholder="Select a date..." ariaLabel="Select a date" // DatePicker uses English strings by default. For localized apps, you must override this prop. strings={defaultDatePickerStrings} />
          </div>
        </div>
      </div>
    </Accordion>
    <Accordion title="Section 3" defaultCollapsed={true} key='3'>
      <div>
        <div className="ms-Grid-row">
          <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
            <Toggle label="Enabled and checked" defaultChecked onText="On" offText="Off" />
          </div>
          <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
            <TextField label="Standard" />
          </div>
          <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
            <TextField label="Read-only" readOnly defaultValue="I am read-only" />
          </div>
        </div>
        <div className="ms-Grid-row">
          <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
            <Toggle label="Enabled and Unchecked" onText="On" offText="Off" />
          </div>
          <div className="ms-Grid-col ms-sm12 ms-md6 ms-lg4">
            <TextField label="Standard" />
          </div>
          <br></br>
          <br></br>
          <br></br>
          <br></br>
          <div className="ms-Grid-col ms-sm12 ms-md12 ms-lg6">
            <Label>Description 1</Label>
            <RichText onChange={(text)=>this.onTextChange(text)} />
          </div>
          <div className="ms-Grid-col ms-sm12 ms-md12 ms-lg6">
            <Label>Description 2</Label>
            <RichText styleOptions={{showAlign:false,showBold:false,showItalic:false,showLink:false
    ,showList:false,showMore:true,showImage:true,showStyles:false,showUnderline:false
  }} onChange={(text)=>this.onTextChange(text)} />
          </div>
        </div>
      </div>
    </Accordion>
    <br></br>
    <div className="ms-Grid-col ms-smPush2 ms-mdPush4 ms-lgPush8">
      <PrimaryButton text="Submit" onClick={this._alertClicked} allowDisabledFocus />
    </div>
  </div>
</div>
 );
}
private onFormatDate = (date ? : Date): string => {
    return !date ? '' : ((date.getMonth() > 8) ? (date.getMonth() + 1) : ('0' + (date.getMonth() + 1))) + '/' + ((date.getDate() > 9) ? date.getDate() : ('0' + date.getDate())) + '/' + date.getFullYear();
};
private onTextChange = (newText: string) => {
    console.log(newText)
    return newText;
}
private _alertClicked(): void {
    alert('Form Submited');
   }
}

Expected Output

PC view

Implementation of office ui grid

Mobile view

Datepicker

Implementation of office ui grid

RichText

Implementation of office ui grid

Conclusion

In this article, we learned how to implement OfficeUI grid, Accordion, Datepicker, Richtext in SPFx. I hope this helps someone. Happy coding :)

Recommended Ebook

SharePoint Framework (SPFx) A Developers Guide

Download Now!
Similar Articles