Skip to content

Commit 1c53b70

Browse files
KoenZomersVesaJuvonen
authored andcommitted
Updating article to work with SPFx 1.91 (SharePoint#4706)
Updated code samples and text so it works with SPFx 1.91 and the latest version of OfficeUI Fabric for React
1 parent 8d1fbc6 commit 1c53b70

File tree

1 file changed

+68
-70
lines changed

1 file changed

+68
-70
lines changed

docs/spfx/extensions/guidance/using-custom-dialogs-with-spfx.md

Lines changed: 68 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,12 @@ To create a custom dialog box, you need to follow the steps in [Set up your deve
6060

6161
![SharePoint client-side solution scaffolded successfully](../../../images/ext-com-dialog-yeoman-complete.png)
6262

63-
6. When initial scaffolding is completed, enter the following to explicitly install the 5.x version of the Office UI Fabric to your solution:
63+
6. When initial scaffolding is completed, enter the following to install Office UI Fabric to your solution:
6464

6565
```sh
66-
npm install office-ui-fabric-react@5.132.0 --save
66+
npm install office-ui-fabric-react --save
6767
```
6868

69-
> [!NOTE]
70-
> Starting with SharePoint Framework 1.8, you can use either Office UI Fabric version 5 or version 6. In this case we are using specifically Office UI Fabric version 5.143.0, so we are adding the needed dependency on it. If you would be using Office UI Fabric version 6.x, you'd also need to update the used TypeScript version of the solution.
71-
7269
7. Open your project folder in your code editor. This article uses Visual Studio Code in the steps and screenshots, but you can use any editor that you prefer. To open the folder in Visual Studio Code, use the following command in the console:
7370

7471
```sh
@@ -103,100 +100,98 @@ In the extension manifest, configure the extension to have only one button. In t
103100
2. Add the following import statements at the top of the newly created file. You're creating your custom dialog box by using the [Office UI Fabric React components](https://developer.microsoft.com/en-us/fabric#/components), so the implementation is in React.
104101

105102
```typescript
106-
import * as React from 'react';
107-
import * as ReactDOM from 'react-dom';
108-
import { BaseDialog, IDialogConfiguration } from '@microsoft/sp-dialog';
109-
import {
110-
autobind,
111-
ColorPicker,
112-
PrimaryButton,
113-
Button,
114-
DialogFooter,
115-
DialogContent
116-
} from 'office-ui-fabric-react';
103+
import * as React from 'react';
104+
import * as ReactDOM from 'react-dom';
105+
import { BaseDialog, IDialogConfiguration } from '@microsoft/sp-dialog';
106+
import {
107+
ColorPicker,
108+
PrimaryButton,
109+
Button,
110+
DialogFooter,
111+
DialogContent,
112+
IColor
113+
} from 'office-ui-fabric-react';
117114

118115
```
119116

120117
3. Add the following interface definition just under the import statements. This is used to pass information and functions between your ListView Command Set extension and your custom dialog box.
121118

122119
```typescript
123-
interface IColorPickerDialogContentProps {
120+
interface IColorPickerDialogContentProps {
124121
message: string;
125122
close: () => void;
126-
submit: (color: string) => void;
127-
defaultColor?: string;
128-
}
123+
submit: (color: IColor) => void;
124+
defaultColor?: IColor;
125+
}
129126
```
130127

131128
4. Add the following class just under the interface definition. This React class is responsible for rendering the UI experiences inside the custom dialog box. Notice that you use the Office UI Fabric React components for actual rendering and just pass the needed properties.
132129

133130
```typescript
134-
class ColorPickerDialogContent extends React.Component<IColorPickerDialogContentProps, {}> {
135-
private _pickedColor: string;
131+
class ColorPickerDialogContent extends React.Component<IColorPickerDialogContentProps, {}> {
132+
private _pickedColor: IColor;
136133

137134
constructor(props) {
138-
super(props);
139-
// Default Color
140-
this._pickedColor = props.defaultColor || '#FFFFFF';
135+
super(props);
136+
// Default Color
137+
this._pickedColor = props.defaultColor || { hex: 'FFFFFF', str: '', r: null, g: null, b: null, h: null, s: null, v: null };
141138
}
142139

143140
public render(): JSX.Element {
144-
return <DialogContent
141+
return <DialogContent
145142
title='Color Picker'
146143
subText={this.props.message}
147144
onDismiss={this.props.close}
148145
showCloseButton={true}
149-
>
150-
<ColorPicker color={this._pickedColor} onColorChanged={this._onColorChange} />
146+
>
147+
<ColorPicker color={this._pickedColor} onChange={this._onColorChange} />
151148
<DialogFooter>
152-
<Button text='Cancel' title='Cancel' onClick={this.props.close} />
153-
<PrimaryButton text='OK' title='OK' onClick={() => { this.props.submit(this._pickedColor); }} />
149+
<Button text='Cancel' title='Cancel' onClick={this.props.close} />
150+
<PrimaryButton text='OK' title='OK' onClick={() => { this.props.submit(this._pickedColor); }} />
154151
</DialogFooter>
155-
</DialogContent>;
152+
</DialogContent>;
156153
}
157154

158-
@autobind
159-
private _onColorChange(color: string): void {
160-
this._pickedColor = color;
155+
private _onColorChange = (ev: React.SyntheticEvent<HTMLElement, Event>, color: IColor) => {
156+
this._pickedColor = color;
161157
}
162-
}
158+
}
163159
```
164160

165161
5. Add the following class definition for your custom dialog box under the `ColorPickerDialogContent` class that you just added. This is the actual custom dialog box that is called from the ListView Command Set button click and is inherited from the `BaseDialog`.
166162

167163
```typescript
168-
export default class ColorPickerDialog extends BaseDialog {
164+
export default class ColorPickerDialog extends BaseDialog {
169165
public message: string;
170-
public colorCode: string;
166+
public colorCode: IColor;
171167

172168
public render(): void {
173-
ReactDOM.render(<ColorPickerDialogContent
169+
ReactDOM.render(<ColorPickerDialogContent
174170
close={ this.close }
175171
message={ this.message }
176172
defaultColor={ this.colorCode }
177173
submit={ this._submit }
178-
/>, this.domElement);
174+
/>, this.domElement);
179175
}
180176

181177
public getConfig(): IDialogConfiguration {
182-
return {
178+
return {
183179
isBlocking: false
184-
};
180+
};
185181
}
186-
182+
187183
protected onAfterClose(): void {
188-
super.onAfterClose();
189-
190-
// Clean up the element for the next dialog
191-
ReactDOM.unmountComponentAtNode(this.domElement);
184+
super.onAfterClose();
185+
186+
// Clean up the element for the next dialog
187+
ReactDOM.unmountComponentAtNode(this.domElement);
192188
}
193189

194-
@autobind
195-
private _submit(color: string): void {
196-
this.colorCode = color;
197-
this.close();
190+
private _submit = (color: IColor) => {
191+
this.colorCode = color;
192+
this.close();
198193
}
199-
}
194+
}
200195
```
201196

202197
## Associate the dialog box with the ListView Command Set button click
@@ -205,16 +200,17 @@ To associate the custom dialog box with your custom ListView Command Set, add th
205200

206201
1. In the code editor, open the **DialogDemoCommandSet.ts** file from the **./src/extensions/dialogDemo/** folder.
207202

208-
2. Add the following import statements under the existing **strings** import. These are for using the custom dialog box in the context of your ListView Command Set.
203+
2. Add the following import statements under the existing **strings** import. These are for using the custom dialog box in the context of your ListView Command Set and using the IColor type to pass colors to and from our dialog.
209204

210205
```typescript
211206
import ColorPickerDialog from './ColorPickerDialog';
207+
import { IColor } from 'office-ui-fabric-react';
212208
```
213209

214210
3. Add the following `_colorCode` variable definition above the `onInit` function in the `DialogDemoCommandSet` class. This is used to store the color picker dialog box result.
215211

216212
```typescript
217-
private _colorCode: string;
213+
private _colorCode: IColor;
218214
```
219215

220216
4. Update the `onExecute` function as follows. This code does the following:
@@ -227,23 +223,25 @@ To associate the custom dialog box with your custom ListView Command Set, add th
227223
* Shows the received value in a default dialog box by using the `Dialog.alert()` function.
228224

229225
```typescript
230-
@override
231-
public onExecute(event: IListViewCommandSetExecuteEventParameters): void {
232-
switch (event.itemId) {
233-
case 'COMMAND_1':
234-
const dialog: ColorPickerDialog = new ColorPickerDialog();
235-
dialog.message = 'Pick a color:';
236-
// Use 'EEEEEE' as the default color for first usage
237-
dialog.colorCode = this._colorCode || '#EEEEEE';
238-
dialog.show().then(() => {
239-
this._colorCode = dialog.colorCode;
240-
Dialog.alert('Picked color: ${dialog.colorCode}');
241-
});
242-
break;
243-
default:
244-
throw new Error('Unknown command');
245-
}
226+
@override
227+
public onExecute(event: IListViewCommandSetExecuteEventParameters): void {
228+
switch (event.itemId) {
229+
case 'COMMAND_1':
230+
Dialog.alert(`${this.properties.sampleTextOne}`);
231+
const dialog: ColorPickerDialog = new ColorPickerDialog();
232+
dialog.message = 'Pick a color:';
233+
// Use 'FFFFFF' as the default color for first usage
234+
let defaultColor : IColor = { hex: 'FFFFFF', str: '', r: null, g: null, b: null, h: null, s: null, v: null };
235+
dialog.colorCode = this._colorCode|| defaultColor;
236+
dialog.show().then(() => {
237+
this._colorCode = dialog.colorCode;
238+
Dialog.alert(`Picked color: ${dialog.colorCode.hex}`);
239+
});
240+
break;
241+
default:
242+
throw new Error('Unknown command');
246243
}
244+
}
247245
```
248246

249247
## Test the dialog box in your tenant
@@ -255,7 +253,7 @@ To associate the custom dialog box with your custom ListView Command Set, add th
255253
```sh
256254
"serveConfigurations": {
257255
"default": {
258-
"pageUrl": "https://sppnp.sharepoint.com/sites/team/Shared%20Documents/Forms/AllItems.aspx",
256+
"pageUrl": "https://yourtenantname.sharepoint.com/Shared%20Documents/Forms/AllItems.aspx",
259257
"customActions": {
260258
"9b98b919-fe5e-4758-ac91-6d62e582c4fe": {
261259
"___location": "ClientSideExtension.ListViewCommandSet.CommandBar",

0 commit comments

Comments
 (0)