Skip to content

Commit f433e99

Browse files
committed
support for custom rendering of Placeholder's iconText and description
1 parent 6b0ce5e commit f433e99

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

docs/documentation/docs/controls/Placeholder.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ import { Placeholder } from "@pnp/spfx-controls-react/lib/Placeholder";
2323
onConfigure={this._onConfigure} />
2424
```
2525

26+
- With custom element for `description`:
27+
28+
```TypeScript
29+
<Placeholder iconName='Edit'
30+
iconText='Configure your web part'
31+
description={defaultClassNames => <span className={`${defaultClassNames} ${additionalStyles}`}>Please configure the web part.</span>}
32+
buttonLabel='Configure'
33+
onConfigure={this._onConfigure} />
34+
```
35+
2636
- With the `onConfigure` property you can define what it needs to do when you click on the button. Like for example opening the property pane:
2737

2838
```typescript
@@ -51,9 +61,9 @@ The placeholder control can be configured with the following properties:
5161
| ---- | ---- | ---- | ---- |
5262
| buttonLabel | string | no | Text label to be displayed on the button bellow the description. The button is optional. |
5363
| contentClassName | string | no | This is the className that is applied to the root element of the content zone. You can use this to apply custom styles to the placeholder. |
54-
| description | string | yes | Text description for the placeholder. This appears bellow the Icon and IconText. |
64+
| description | string \| ((defaultClassNames: string) =&gt; React.ReactElement) | yes | Text description or render function for the placeholder. This appears bellow the Icon and IconText. |
5565
| iconName | string | yes | The name of the icon that will be used in the placeholder. This is the same name as you can find on the Office UI Fabric icons page: [Office UI Fabric icons](https://dev.office.com/fabric#/styles/icons). For example: `Page` or `Add`. |
56-
| iconText | string | yes | Heading text which is displayed next to the icon. |
66+
| iconText | string \| ((defaultClassNames: string) =&gt; React.ReactElement) | yes | Heading text or render function which is displayed next to the icon. |
5767
| hideButton | boolean | no | Specify if you want to hide the button. Default is `false`. |
5868
| onConfigure | function | no | onConfigure handler for the button. The button is optional. |
5969

src/controls/placeholder/IPlaceholderComponent.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
export interface IPlaceholderProps {
66

77
/**
8-
* Text description for the placeholder. Appears bellow the Icon and IconText.
8+
* Text description or component for the placeholder. Appears bellow the Icon and IconText.
99
*/
10-
description: string;
10+
description: string | ((defaultClassNames: string) => React.ReactElement);
1111
/**
1212
* Icon name used for the className from the MDL2 set. Example: 'Add'.
1313
*/
1414
iconName: string;
1515
/**
1616
* Heading displayed against the Icon.
1717
*/
18-
iconText: string;
18+
iconText: string | ((defaultClassNames: string) => React.ReactElement);
1919
/**
2020
* Text label to be displayed on button below the description.
2121
* Optional: As the button is optional.

src/controls/placeholder/PlaceholderComponent.tsx

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class Placeholder extends React.Component<IPlaceholderProps, IPlaceholder
6464
return true;
6565
}
6666
}
67-
}
67+
}
6868
return this.state.width !== nextState.width || this.props.hideButton !== nextProps.hideButton;
6969
}
7070

@@ -95,28 +95,42 @@ export class Placeholder extends React.Component<IPlaceholderProps, IPlaceholder
9595
* Default React component render method
9696
*/
9797
public render(): React.ReactElement<IPlaceholderProps> {
98+
99+
const {
100+
iconName,
101+
iconText,
102+
description,
103+
children,
104+
buttonLabel,
105+
hideButton
106+
} = this.props;
107+
108+
const iconTextClassNames = `${styles.placeholderText} ${(this.state.width && this.state.width <= 380) ? styles.hide : "" }`;
109+
const iconTextEl = typeof iconText === 'string' ? <span className={iconTextClassNames}>{this.props.iconText}</span> : iconText(iconTextClassNames);
110+
const descriptionEl = typeof description === 'string' ? <span className={styles.placeholderDescriptionText}>{this.props.description}</span> : description(styles.placeholderDescriptionText);
111+
98112
return (
99113
<div className={`${styles.placeholder} ${this.props.contentClassName ? this.props.contentClassName : ''}`} ref={this._linkElm}>
100114
<div className={styles.placeholderContainer}>
101115
<div className={styles.placeholderHead}>
102116
<div className={styles.placeholderHeadContainer}>
103117
{
104-
this.props.iconName && <Icon iconName={this.props.iconName} className={styles.placeholderIcon} />
118+
iconName && <Icon iconName={iconName} className={styles.placeholderIcon} />
105119
}
106-
<span className={`${styles.placeholderText} ${(this.state.width && this.state.width <= 380) ? styles.hide : "" }`}>{this.props.iconText}</span>
120+
{iconTextEl}
107121
</div>
108122
</div>
109123
<div className={styles.placeholderDescription}>
110-
<span className={styles.placeholderDescriptionText}>{this.props.description}</span>
124+
{descriptionEl}
111125
</div>
112-
{this.props.children}
126+
{children}
113127
<div className={styles.placeholderDescription}>
114128
{
115-
(this.props.buttonLabel && !this.props.hideButton) &&
129+
(buttonLabel && !hideButton) &&
116130
<PrimaryButton
117-
text={this.props.buttonLabel}
118-
ariaLabel={this.props.buttonLabel}
119-
ariaDescription={this.props.description}
131+
text={buttonLabel}
132+
ariaLabel={buttonLabel}
133+
ariaDescription={typeof description === 'string' ? description : ''}
120134
onClick={this._handleBtnClick} />
121135
}
122136
</div>

src/webparts/controlsTest/components/ControlsTest.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ export default class ControlsTest extends React.Component<IControlsTestProps, IC
885885

886886
<Placeholder iconName='Edit'
887887
iconText='Configure your web part'
888-
description='Please configure the web part.'
888+
description={defaultClassNames => <span className={defaultStyle}>Please configure the web part.</span>}
889889
buttonLabel='Configure'
890890
hideButton={this.props.displayMode === DisplayMode.Read}
891891
onConfigure={this._onConfigure} />

0 commit comments

Comments
 (0)