Skip to content

Commit 061f13f

Browse files
committed
commmit changes
1 parent 88fa5dc commit 061f13f

14 files changed

+175
-25
lines changed
51.6 KB
Loading
55.3 KB
Loading
51.9 KB
Loading
50.1 KB
Loading
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# List-Item-Attachments control
2+
3+
This control allows you to manage list item attachments, you can add or delete associated attachments, the attachments are listed in tile view.
4+
5+
6+
Here is an example of the control:
7+
8+
![ListItemAttachments Tiles](./assets/ListItemAttachmentsTitles.png)
9+
10+
![ListItemAttachments Confirm Delete](./assets/ListItemAttachmentDeleteConfirm.png)
11+
12+
![ListItemAttachments Attachment Deleted ](./assets/ListItemAttachementDeletedMsg.png)
13+
14+
## How to use this control in your solutions
15+
16+
- Check that you installed the `@pnp/spfx-controls-react` dependency. Check out the [getting started](../#getting-started) page for more information about installing the dependency.
17+
- Import the control into your component:
18+
19+
```TypeScript
20+
21+
import { ListItemAttachments } from '@pnp/spfx-controls-react/listItemAttachments';
22+
```
23+
- Use the `ListItemAttachments` control in your code as follows:
24+
25+
```TypeScript
26+
<ListItemAttachments listId='dfa283f4-5faf-4d54-b6b8-5bcaf2725af5'
27+
itemId={1}
28+
context={this.props.context}
29+
disabled={false} />
30+
```
31+
32+
## Implementation
33+
34+
The `ListItemAttachments` control can be configured with the following properties:
35+
36+
37+
| Property | Type | Required | Description |
38+
| ---- | ---- | ---- | ---- |
39+
| context | WebPartContext \| ApplicationCustomizerContext | yes | SPFx web part or extention context |
40+
| listId | string | yes | Guid of the list. |
41+
| itemId | number | yes | List Item Id |
42+
| webUrl | string | no | URL of the site. By default it uses the current site URL. |
43+
| disabled | boolean | no | Specifies if the control is disabled or not. |
44+
45+
![](https://telemetry.sharepointpnp.com/sp-dev-fx-controls-react/wiki/controls/ListItemAttachments)
46+

src/ListItemAttachments.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './controls/listItemAttachments';
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import {IListItemAttachmentFile } from '../spentities/IListItemAttachmentFile';
1+
import {IListItemAttachmentFile } from './IListItemAttachmentFile';
22
export interface IListItemAttachmentsState {
33
file: IListItemAttachmentFile;
44
showDialog: boolean;
55
dialogMessage: string;
6-
Documents: IListItemAttachmentFile[];
6+
attachments: IListItemAttachmentFile[];
77
deleteAttachment: boolean;
8+
disableButton: boolean;
89
}

src/controls/listItemAttachments/IUploadAttachmentProps.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ export interface IUploadAttachmentProps {
99
disabled?: boolean;
1010
context: WebPartContext | ApplicationCustomizerContext;
1111
onAttachmentUpload: () => void;
12-
iconButton: boolean;
1312
}

src/controls/listItemAttachments/ListItemAttachments.tsx

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { IListItemAttachmentsProps } from '.';
2121
import { IListItemAttachmentsState } from '.';
2222
import SPservice from "../../services/SPService";
2323
import { TooltipHost, DirectionalHint } from 'office-ui-fabric-react/lib/Tooltip';
24+
import { Spinner, SpinnerSize } from 'office-ui-fabric-react/lib/Spinner';
2425
import utilities from './utilities';
2526
export class ListItemAttachments extends React.Component<IListItemAttachmentsProps, IListItemAttachmentsState> {
2627
private _spservice: SPservice;
@@ -34,8 +35,9 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
3435
file: null,
3536
showDialog: false,
3637
dialogMessage: '',
37-
Documents: [],
38-
deleteAttachment: false
38+
attachments: [],
39+
deleteAttachment: false,
40+
disableButton: false
3941
};
4042
// Get SPService Factory
4143
this._spservice = new SPservice(this.props.context);
@@ -54,9 +56,10 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
5456
const files: IListItemAttachmentFile[] = await this._spservice.getListItemAttachments(this.props.listId, this.props.itemId);
5557
for (const _file of files) {
5658

59+
const _previewImage = await this._utilities.GetFileImageUrl(_file);
5760
this.previewImages.push({
5861
name: _file.FileName,
59-
previewImageSrc: await this._utilities.GetFileImageUrl(_file),
62+
previewImageSrc: _previewImage,
6063
iconSrc: '',
6164
imageFit: ImageFit.center,
6265
width: 187,
@@ -66,7 +69,7 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
6669
this.setState({
6770
showDialog: false,
6871
dialogMessage: '',
69-
Documents: files
72+
attachments: files
7073
});
7174
}
7275
catch (error) {
@@ -89,13 +92,12 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
8992
<UploadAttachment
9093
listId={this.props.listId}
9194
itemId={this.props.itemId}
92-
iconButton={true}
9395
disabled={this.props.disabled}
9496
context={this.props.context}
9597
onAttachmentUpload={this._onAttachmentpload}
9698
/>
9799

98-
{this.state.Documents.map((_file, i: number) => {
100+
{this.state.attachments.map((_file, i: number) => {
99101
return (
100102
<div className={styles.documentCardWrapper}>
101103
<TooltipHost
@@ -105,7 +107,7 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
105107
directionalHint={DirectionalHint.rightCenter}>
106108

107109
<DocumentCard
108-
onClickHref={_file.ServerRelativeUrl}
110+
onClickHref={ `${_file.ServerRelativeUrl}?web=1`}
109111
className={styles.documentCard}>
110112
<DocumentCardPreview previewImages={[this.previewImages[i]]} />
111113
<Label className={styles.fileLabel}>
@@ -137,6 +139,7 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
137139
);
138140
})}
139141
{
142+
140143
<Dialog
141144
isOpen={this.state.showDialog}
142145
type={DialogType.normal}
@@ -145,11 +148,17 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
145148
subText={this.state.dialogMessage}
146149
isBlocking={true}>
147150
<DialogFooter>
151+
<div style={{ marginBottom: 7 }}>
152+
{
153+
this.state.disableButton ? <Spinner size={SpinnerSize.medium} /> : ''
154+
}
155+
</div>
148156
{
149-
this.state.deleteAttachment ? (<PrimaryButton onClick={this._onConfirmedDeleteAttachment}>{strings.ListItemAttachmentsdialogOKbuttonLabelOnDelete}</PrimaryButton>) : ""
157+
this.state.deleteAttachment ? (<PrimaryButton disabled={this.state.disableButton} onClick={this._onConfirmedDeleteAttachment}>{strings.ListItemAttachmentsdialogOKbuttonLabelOnDelete}</PrimaryButton>) : ""
150158
}
151159
{
152-
this.state.deleteAttachment ? (<DefaultButton onClick={this._closeDialog}>{strings.ListItemAttachmentsdialogCancelButtonLabel}</DefaultButton>) : <PrimaryButton onClick={this._closeDialog}>{strings.ListItemAttachmentsdialogOKbuttonLabel}</PrimaryButton>
160+
this.state.deleteAttachment ? (<DefaultButton disabled={this.state.disableButton} onClick={this._closeDialog}>{strings.ListItemAttachmentsdialogCancelButtonLabel}</DefaultButton>)
161+
: <PrimaryButton onClick={this._closeDialog}>{strings.ListItemAttachmentsdialogOKbuttonLabel}</PrimaryButton>
153162
}
154163
</DialogFooter>
155164
</Dialog>
@@ -192,12 +201,18 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
192201
private _onConfirmedDeleteAttachment() {
193202
// Delete Attachment
194203
const _file = this.state.file;
204+
205+
this.setState({
206+
disableButton: true,
207+
});
208+
195209
this._spservice.deleteAttachment(_file.FileName, this.props.listId, this.props.itemId, this.props.webUrl)
196210
.then(() => {
197211

198212
this.setState({
199213
showDialog: true,
200214
deleteAttachment: false,
215+
disableButton: false,
201216
file: null,
202217
dialogMessage: strings.ListItemAttachmentsfileDeletedMsg.replace('{0}', _file.FileName),
203218
});

src/controls/listItemAttachments/UploadAttachment.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import SPservice from "../../services/SPService";
88
import * as strings from 'ControlStrings';
99
import { CommandBar } from 'office-ui-fabric-react/lib/CommandBar';
1010
import { ProgressIndicator } from 'office-ui-fabric-react/lib/ProgressIndicator';
11+
import * as $ from 'jquery';
12+
import { createRef } from '@uifabric/utilities/lib';
1113

1214
export class UploadAttachment extends React.Component<IUploadAttachmentProps, IUploadAttachmentState> {
1315
private _spservice: SPservice;
14-
16+
private fileInput;
1517
constructor(props) {
1618
super(props);
1719
this.state = {
@@ -22,25 +24,33 @@ export class UploadAttachment extends React.Component<IUploadAttachmentProps, IU
2224
};
2325
// Get SPService
2426
this._spservice = new SPservice(this.props.context);
27+
this.fileInput = createRef();
2528
// Handlers
2629
this._closeDialog = this._closeDialog.bind(this);
30+
this._addAttachment = this._addAttachment.bind(this);
31+
this._onAttachmentUpload = this._onAttachmentUpload.bind(this);
2732
}
2833
// FileReader event
2934
private _onAttachmentUpload(e) {
3035
e.preventDefault();
3136
// fire click event
32-
document.getElementById('file-picker').click();
37+
this.fileInput.current.value='';
38+
this.fileInput.current.click();
39+
//$('#file-picker').val('');
40+
// $('#file-picker').trigger('click');
41+
42+
// document.getElementById('file-picker').click();
3343
}
3444
// Add Attachment
3545
private _addAttachment(e) {
46+
3647
e.preventDefault();
3748
this.setState({
3849
isLoading: true
3950
});
4051

4152
const reader = new FileReader();
4253
const file = e.target.files[0];
43-
reader.readAsDataURL(file);
4454

4555
reader.onloadend = () => {
4656
this.setState({
@@ -62,6 +72,7 @@ export class UploadAttachment extends React.Component<IUploadAttachmentProps, IU
6272
});
6373
});
6474
};
75+
reader.readAsDataURL(file);
6576
}
6677
// Render
6778
public render() {
@@ -72,6 +83,7 @@ export class UploadAttachment extends React.Component<IUploadAttachmentProps, IU
7283
style={{ display: 'none' }}
7384
type="file"
7485
onChange={(e) => this._addAttachment(e)}
86+
ref={this.fileInput}
7587
/>
7688
<div style={{ textAlign: 'left', marginTop: 25, marginBottom: 25 }}>
7789
<CommandBar

0 commit comments

Comments
 (0)