PnP React Controls

Patterns and Practices (PnP) provides a list of reusable React controls to developers for building solutions such as webparts and extensions using SharePoint Framework.
Refer to this link to get the list of React controls for SPFx.
You will see how to use PnP Date Time Picker control in SPFx webpart.

PnP Date Time Picker Control

This control allows you to select dates from a calendar and optionally the time of day using dropdown controls. You can configure the control to use a 12 or 24-hour clock. Refer to this link for more details.
PnP Date Time Picker Control In SharePoint Framework
In this article, you will see how to perform the following tasks,
Prerequisites

Create SPFx solution

Open Node.js command prompt.
Create a new folder.
>md spfx-pnpreact-datetimepicker
Navigate to the folder.
> cd spfx-pnpreact-datetimepicker
Execute the following command to create SPFx webpart.
>yo @microsoft/sharepoint
Enter all the required details to create a new solution. Yeoman generator will perform the scaffolding process and once it is completed, lock down the version of project dependencies by executing the following command.
>npm shrinkwrap
Execute the following command to open the solution in the code editor.
>code .

Implement Date Time Picker Control solution

Execute the following command to install the PnP React Controls NPM package.
>npm install @pnp/spfx-controls-react –save
Execute the following command to install the pnp sp library.
>npm install @pnp/sp –save
Create a new ts file named as “IDatetimepickercontrolState.ts” under Components folder (src\webparts\datetimepickercontrol\components\IDatetimepickercontrolState.ts) and update the code as shown below.
  1. import { MessageBarType } from 'office-ui-fabric-react';
  2. export interface IDatetimepickercontrolState{
  3. projectTitle: string;
  4. projectDescription: string;
  5. startDate: Date;
  6. endDate: Date;
  7. showMessageBar: boolean;
  8. messageType?: MessageBarType;
  9. message?: string;
  10. }
Open the props file (src\webparts\datetimepickercontrol\components\IDatetimepickercontrolProps.ts) and update the code as shown below.
  1. import {WebPartContext} from '@microsoft/sp-webpart-base';
  2. export interface IDatetimepickercontrolProps {
  3. description: string;
  4. context: WebPartContext;
  5. }
Open the webpart file “src\webparts\datetimepickercontrol\DatetimepickercontrolWebPart.ts” and update the render method.
  1. public render(): void {
  2. const element: React.ReactElement<IDatetimepickercontrolProps> = React.createElement(
  3. Datetimepickercontrol,
  4. {
  5. description: this.properties.description,
  6. context: this.context
  7. }
  8. );
  9. ReactDom.render(element, this.domElement);
  10. }
Open the component file “src\webparts\datetimepickercontrol\components\Datetimepickercontrol.tsx” and import the following modules.
  1. import { IDatetimepickercontrolState } from './IDatetimepickercontrolState';
  2. import { TextField } from 'office-ui-fabric-react/lib/TextField';
  3. import { MessageBar, MessageBarType, IStackProps, Stack } from 'office-ui-fabric-react';
  4. import { autobind } from 'office-ui-fabric-react';
  5. import { DateTimePicker, DateConvention, TimeConvention, TimeDisplayControlType } from '@pnp/spfx-controls-react/lib/dateTimePicker';
  6. import { sp } from "@pnp/sp";
  7. import "@pnp/sp/webs";
  8. import "@pnp/sp/lists";
  9. import "@pnp/sp/items";
Update the render method as shown below.
  1. public render(): React.ReactElement<IDatetimepickercontrolProps> {
  2. return (
  3. <div className={styles.row}>
  4. <h1>Create New Project</h1>
  5. {
  6. this.state.showMessageBar
  7. ?
  8. <div className="form-group">
  9. <Stack {...verticalStackProps}>
  10. <MessageBar messageBarType={this.state.messageType}>{this.state.message}</MessageBar>
  11. </Stack>
  12. </div>
  13. :
  14. null
  15. }
  16. <div className={styles.row}>
  17. <TextField label="Project Title" required onChanged={this.__onchangedTitle} />
  18. <TextField label="Project Description" required onChanged={this.__onchangedDescription} />
  19. <DateTimePicker label="Start Date"
  20. dateConvention={DateConvention.DateTime}
  21. timeConvention={TimeConvention.Hours12}
  22. timeDisplayControlType={TimeDisplayControlType.Dropdown}
  23. showLabels={false}
  24. value={this.state.startDate}
  25. onChange={this.__onchangedStartDate}
  26. />
  27. <DateTimePicker label="End Date"
  28. dateConvention={DateConvention.Date}
  29. timeConvention={TimeConvention.Hours12}
  30. timeDisplayControlType={TimeDisplayControlType.Dropdown}
  31. showLabels={false}
  32. value={this.state.endDate}
  33. onChange={this.__onchangedEndDate}
  34. />
  35. <div className={styles.button}>
  36. <button type="button" className="btn btn-primary" onClick={this.__createItem}>Submit</button>
  37. </div>
  38. </div>
  39. </div>
  40. );
  41. }
Create the constructor in the component file.
  1. export default class Datetimepickercontrol extends React.Component<IDatetimepickercontrolProps, IDatetimepickercontrolState> {
  2. constructor(props: IDatetimepickercontrolProps, state: IDatetimepickercontrolState) {
  3. super(props);
  4. sp.setup({
  5. spfxContext: this.props.context
  6. });
  7. this.state = {
  8. projectTitle: '',
  9. projectDescription: '',
  10. startDate: new Date(),
  11. endDate: new Date(),
  12. showMessageBar: false
  13. };
  14. }
Create the helper methods in the component file.
  1. @autobind
  2. private __onchangedTitle(title: any): void {
  3. this.setState({ projectTitle: title });
  4. }
  5. @autobind
  6. private __onchangedDescription(description: any): void {
  7. this.setState({ projectDescription: description });
  8. }
  9. @autobind
  10. private __onchangedStartDate(date: any): void {
  11. this.setState({ startDate: date });
  12. }
  13. @autobind
  14. private __onchangedEndDate(date: any): void {
  15. this.setState({ endDate: date });
  16. }
  17. @autobind
  18. private async __createItem() {
  19. try {
  20. await sp.web.lists.getByTitle('Project Details').items.add({
  21. Title: this.state.projectTitle,
  22. Description: this.state.projectDescription,
  23. StartDate: this.state.startDate,
  24. EndDate: this.state.endDate
  25. });
  26. this.setState({
  27. message: "Item: " + this.state.projectTitle + " - created successfully!",
  28. showMessageBar: true,
  29. messageType: MessageBarType.success
  30. });
  31. }
  32. catch (error) {
  33. this.setState({
  34. message: "Item " + this.state.projectTitle + " creation failed with error: " + error,
  35. showMessageBar: true,
  36. messageType: MessageBarType.error
  37. });
  38. }
  39. }
Updated React component (src\webparts\datetimepickercontrol\components\Datetimepickercontrol.tsx),
  1. import * as React from 'react';
  2. import styles from './Datetimepickercontrol.module.scss';
  3. import { IDatetimepickercontrolProps } from './IDatetimepickercontrolProps';
  4. import { IDatetimepickercontrolState } from './IDatetimepickercontrolState';
  5. import { escape } from '@microsoft/sp-lodash-subset';
  6. import { TextField } from 'office-ui-fabric-react/lib/TextField';
  7. import { MessageBar, MessageBarType, IStackProps, Stack } from 'office-ui-fabric-react';
  8. import { autobind } from 'office-ui-fabric-react';
  9. import { DateTimePicker, DateConvention, TimeConvention, TimeDisplayControlType } from '@pnp/spfx-controls-react/lib/dateTimePicker';
  10. import { sp } from "@pnp/sp";
  11. import "@pnp/sp/webs";
  12. import "@pnp/sp/lists";
  13. import "@pnp/sp/items";
  14. const verticalStackProps: IStackProps = {
  15. styles: { root: { overflow: 'hidden', width: '100%' } },
  16. tokens: { childrenGap: 20 }
  17. };
  18. export default class Datetimepickercontrol extends React.Component<IDatetimepickercontrolProps, IDatetimepickercontrolState> {
  19. constructor(props: IDatetimepickercontrolProps, state: IDatetimepickercontrolState) {
  20. super(props);
  21. sp.setup({
  22. spfxContext: this.props.context
  23. });
  24. this.state = {
  25. projectTitle: '',
  26. projectDescription: '',
  27. startDate: new Date(),
  28. endDate: new Date(),
  29. showMessageBar: false
  30. };
  31. }
  32. public render(): React.ReactElement<IDatetimepickercontrolProps> {
  33. return (
  34. <div className={styles.row}>
  35. <h1>Create New Project</h1>
  36. {
  37. this.state.showMessageBar
  38. ?
  39. <div className="form-group">
  40. <Stack {...verticalStackProps}>
  41. <MessageBar messageBarType={this.state.messageType}>{this.state.message}</MessageBar>
  42. </Stack>
  43. </div>
  44. :
  45. null
  46. }
  47. <div className={styles.row}>
  48. <TextField label="Project Title" required onChanged={this.__onchangedTitle} />
  49. <TextField label="Project Description" required onChanged={this.__onchangedDescription} />
  50. <DateTimePicker label="Start Date"
  51. dateConvention={DateConvention.DateTime}
  52. timeConvention={TimeConvention.Hours12}
  53. timeDisplayControlType={TimeDisplayControlType.Dropdown}
  54. showLabels={false}
  55. value={this.state.startDate}
  56. onChange={this.__onchangedStartDate}
  57. />
  58. <DateTimePicker label="End Date"
  59. dateConvention={DateConvention.Date}
  60. timeConvention={TimeConvention.Hours12}
  61. timeDisplayControlType={TimeDisplayControlType.Dropdown}
  62. showLabels={false}
  63. value={this.state.endDate}
  64. onChange={this.__onchangedEndDate}
  65. />
  66. <div className={styles.button}>
  67. <button type="button" className="btn btn-primary" onClick={this.__createItem}>Submit</button>
  68. </div>
  69. </div>
  70. </div>
  71. );
  72. }
  73. @autobind
  74. private __onchangedTitle(title: any): void {
  75. this.setState({ projectTitle: title });
  76. }
  77. @autobind
  78. private __onchangedDescription(description: any): void {
  79. this.setState({ projectDescription: description });
  80. }
  81. @autobind
  82. private __onchangedStartDate(date: any): void {
  83. this.setState({ startDate: date });
  84. }
  85. @autobind
  86. private __onchangedEndDate(date: any): void {
  87. this.setState({ endDate: date });
  88. }
  89. @autobind
  90. private async __createItem() {
  91. try {
  92. await sp.web.lists.getByTitle('Project Details').items.add({
  93. Title: this.state.projectTitle,
  94. Description: this.state.projectDescription,
  95. StartDate: this.state.startDate,
  96. EndDate: this.state.endDate
  97. });
  98. this.setState({
  99. message: "Item: " + this.state.projectTitle + " - created successfully!",
  100. showMessageBar: true,
  101. messageType: MessageBarType.success
  102. });
  103. }
  104. catch (error) {
  105. this.setState({
  106. message: "Item " + this.state.projectTitle + " creation failed with error: " + error,
  107. showMessageBar: true,
  108. messageType: MessageBarType.error
  109. });
  110. }
  111. }
  112. }

Deploy the solution

Execute the following commands to bundle and package the solution.
>gulp bundle --ship
>gulp package-solution --ship
Navigate to tenant app catalog – Example: https://c986.sharepoint.com/sites/appcatalog/SitePages/Home.aspx
Go to Apps for SharePoint library and upload the package file (sharepoint\solution\spfx-pnpreact-datetimepicker.sppkg). Click Deploy.
PnP Date Time Picker Control In SharePoint Framework

Test the webpart

Navigate to the SharePoint site and add the app.
PnP Date Time Picker Control In SharePoint Framework

Result

Navigate to the page and add the webpart. Enter all the details and click submit, a new item gets created in the SharePoint list.
PnP Date Time Picker Control In SharePoint Framework

Summary

Thus, in this article, you saw how to use PnP Date Time Picker Control in SharePoint Framework.