Skip to content

Commit f69b539

Browse files
committed
#165 - Updates to improve bundle
1 parent e944a62 commit f69b539

File tree

8 files changed

+67
-27
lines changed

8 files changed

+67
-27
lines changed

src/FieldPickerListData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './controls/fields/fieldPickerListData/index';
1+
export * from './controls/fieldPickerListData/index';

src/controls/fields/fieldPickerListData/FieldPickerListData.tsx renamed to src/controls/fieldPickerListData/FieldPickerListData.tsx

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import * as strings from 'ControlStrings';
22
import * as React from "react";
3-
import SPservice from "../../../services/SPService";
3+
import SPservice from "../../services/SPService";
44
import { escape } from "@microsoft/sp-lodash-subset";
55
import { TagPicker } from "office-ui-fabric-react/lib/components/pickers/TagPicker/TagPicker";
66
import { Label } from "office-ui-fabric-react/lib/Label";
77
import { IFieldPickerListDataProps, IFieldPickerListDataState } from ".";
8+
import * as telemetry from '../../common/telemetry';
89

910

1011
export class FieldPickerListData extends React.Component<IFieldPickerListDataProps, IFieldPickerListDataState> {
11-
private _value: Array<any>;
12+
private _value: any[];
1213
private _spservice: SPservice;
14+
private selectedItems: any[];
1315

1416
constructor(props: IFieldPickerListDataProps) {
1517
super(props);
1618

19+
telemetry.track('FieldPickerListData', {});
20+
1721
// States
1822
this.state = {
1923
noresultsFoundText: typeof this.props.noresultsFoundText === undefined ? strings.genericNoResultsFoundText : this.props.noresultsFoundText,
@@ -25,8 +29,15 @@ export class FieldPickerListData extends React.Component<IFieldPickerListDataPro
2529
// Get SPService Factory
2630
this._spservice = new SPservice(this.props.context);
2731

28-
// Teste Parameters
32+
// Test Parameters
2933
this._value = this.props.value !== undefined ? this.props.value : [];
34+
this.selectedItems = [];
35+
}
36+
37+
public componentDidUpdate(prevProps: IFieldPickerListDataProps, prevState: IFieldPickerListDataState): void {
38+
if (this.props.listId !== prevProps.listId) {
39+
this.selectedItems = [];
40+
}
3041
}
3142

3243
/**
@@ -66,16 +77,27 @@ export class FieldPickerListData extends React.Component<IFieldPickerListDataPro
6677
* On Selected Item
6778
*/
6879
private onItemChanged = (selectedItems: { key: string; name: string }[]): void => {
69-
let item: { key: string; name: string } = selectedItems[0];
70-
console.log(`selected items nr: ${selectedItems.length}`);
80+
this.selectedItems = selectedItems;
7181
this.props.onSelectedItem(selectedItems);
7282
}
7383

7484
/**
7585
* Filter Change
7686
*/
7787
private onFilterChanged = async (filterText: string, tagList: { key: string; name: string }[]) => {
78-
const resolvedSugestions: { key: string; name: string }[] = await this.loadListItems(filterText);
88+
let resolvedSugestions: { key: string; name: string }[] = await this.loadListItems(filterText);
89+
90+
// Filter out the already retrieved items, so that they cannot be selected again
91+
if (this.selectedItems && this.selectedItems.length > 0) {
92+
let filteredSuggestions = [];
93+
for (const suggestion of resolvedSugestions) {
94+
const exists = this.selectedItems.filter(sItem => sItem.key === suggestion.key);
95+
if (!exists || exists.length === 0) {
96+
filteredSuggestions.push(suggestion);
97+
}
98+
}
99+
resolvedSugestions = filteredSuggestions;
100+
}
79101

80102
if (resolvedSugestions) {
81103
this.setState({

src/controls/fields/fieldPickerListData/IFieldPickerListDataProps.ts renamed to src/controls/fieldPickerListData/IFieldPickerListDataProps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { WebPartContext } from "@microsoft/sp-webpart-base";
22
import { ApplicationCustomizerContext } from "@microsoft/sp-application-base";
3+
34
export interface IFieldPickerListDataProps {
45
listId: string;
56
columnInternalName:string;
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// A file is required to be in the root of the /src directory by the TypeScript compiler
22
export * from './IFieldPickerListDataProps';
33
export * from './IFieldPickerListDataState';
4-
export * from '../../../services/SPService';
54
export * from './FieldPickerListData';

src/services/SPService.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@ import { ISPLists } from "../common/SPEntities";
33
import { WebPartContext } from "@microsoft/sp-webpart-base";
44
import { ApplicationCustomizerContext } from "@microsoft/sp-application-base";
55
import { SPHttpClient, SPHttpClientResponse } from "@microsoft/sp-http";
6-
import { sp, Web } from "@pnp/sp";
76

87
export default class SPService implements ISPService {
98

10-
constructor(private _context: WebPartContext | ApplicationCustomizerContext) {
11-
sp.setup({
12-
spfxContext: this._context
13-
});
14-
}
9+
constructor(private _context: WebPartContext | ApplicationCustomizerContext) {}
1510

1611
/**
1712
* Get lists or libraries
@@ -47,19 +42,19 @@ export default class SPService implements ISPService {
4742
* Get List Items
4843
*/
4944
public async getListItems(filterText: string, listId: string, internalColumnName: string, webUrl?: string): Promise<any[]> {
50-
let filter = `startswith(${internalColumnName},'${filterText}')`;
51-
5245
let returnItems: any[];
53-
let spWeb: Web;
54-
if (typeof webUrl === undefined) {
55-
spWeb = new Web(webUrl);
56-
} else {
57-
spWeb = new Web(this._context.pageContext.web.absoluteUrl);
58-
}
5946

6047
try {
61-
returnItems = await spWeb.lists.getById(listId).items.select("Id", internalColumnName).filter(filter).get();
62-
return Promise.resolve(returnItems);
48+
const apiUrl = `${this._context.pageContext.web.absoluteUrl}/_api/web/lists('${listId}')/items?$select=Id,${internalColumnName}&$filter=startswith(${internalColumnName},'${filterText}')`;
49+
const data = await this._context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1);
50+
if (data.ok) {
51+
const results = await data.json();
52+
if (results && results.value && results.value.length > 0) {
53+
return results.value;
54+
}
55+
}
56+
57+
return [];
6358
} catch (error) {
6459
return Promise.reject(error);
6560
}

src/webparts/controlsTest/components/ControlsTest.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { SecurityTrimmedControl, PermissionLevel } from '../../../SecurityTrimme
1919
import { SPPermission } from '@microsoft/sp-page-context';
2020
import { PeoplePicker, PrincipalType } from '../../../PeoplePicker';
2121
import { getItemClassNames } from 'office-ui-fabric-react/lib/components/ContextualMenu/ContextualMenu.classNames';
22+
import { FieldPickerListData } from "../../../../lib/FieldPickerListData";
2223

2324
/**
2425
* Component that can be used to test out the React controls from this project
@@ -32,7 +33,8 @@ export default class ControlsTest extends React.Component<IControlsTestProps, IC
3233
items: [],
3334
iFrameDialogOpened: false,
3435
initialValues: [],
35-
authorEmails: []
36+
authorEmails: [],
37+
selectedList: null
3638
};
3739

3840
this._onIconSizeChange = this._onIconSizeChange.bind(this);
@@ -119,8 +121,11 @@ private onServicePickerChange(terms: IPickerTerms): void {
119121
* Selected lists change event
120122
* @param lists
121123
*/
122-
private onListPickerChange (lists: string | string[]) {
124+
private onListPickerChange = (lists: string | string[]) => {
123125
console.log("Lists:", lists);
126+
this.setState({
127+
selectedList: typeof lists === "string" ? lists : lists.pop()
128+
});
124129
}
125130

126131
/**
@@ -135,13 +140,22 @@ private onServicePickerChange(terms: IPickerTerms): void {
135140
});
136141
}
137142
}
138-
/** Method that retrieves the selected items from People Picker
143+
144+
/**
145+
* Method that retrieves the selected items from People Picker
139146
* @param items
140147
*/
141148
private _getPeoplePickerItems(items: any[]) {
142149
console.log('Items:', items);
143150
}
144151

152+
/**
153+
* Selected item from the list data picker
154+
*/
155+
private fieldPickerListDataSelected(item: any) {
156+
console.log(item);
157+
}
158+
145159
/**
146160
* Renders the component
147161
*/
@@ -262,6 +276,14 @@ private onServicePickerChange(terms: IPickerTerms): void {
262276
onSelectionChanged={this.onListPickerChange} />
263277
</div>
264278

279+
<div className="ms-font-m">Field picker list data tester:
280+
<FieldPickerListData listId={this.state.selectedList}
281+
columnInternalName="Title"
282+
itemLimit={5}
283+
context={this.props.context}
284+
onSelectedItem={this.fieldPickerListDataSelected} />
285+
</div>
286+
265287
<div className="ms-font-m">Services tester:
266288
<TaxonomyPicker
267289
allowMultipleSelections={true}

src/webparts/controlsTest/components/IControlsTestProps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ export interface IControlsTestState {
1616
initialValues: any[];
1717
iFrameDialogOpened?: boolean;
1818
authorEmails: string[];
19+
selectedList: string;
1920
}

0 commit comments

Comments
 (0)