Skip to content

Commit edc1ae3

Browse files
Mc/bug fixes (#5)
* Added feature to remember previous selection. * removed old solutions
1 parent 2a9391b commit edc1ae3

9 files changed

Lines changed: 158 additions & 267 deletions

File tree

controls/pcfcontroldemo/MultiSelectControl/MultiSelect.tsx

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ import * as React from "react";
22
import AsyncSelect from 'react-select/async';
33

44
export interface IProps {
5+
initialValues: any;
56
isControlVisible: boolean;
67
isControlDisabled: boolean;
78
displayValueField: any;
8-
displayFieldLabel: any;
9+
displayFieldLabel: any;
910
columns: any;
1011
topCount: any;
1112
filterField: any;
1213
entityName: any;
1314
value: string;
1415
onChange: (value:string) => void;
15-
onSearch: (value:string) => void;
16+
onSearch: (value:string) => void;
1617
records: any
1718
}
1819

@@ -21,16 +22,15 @@ export interface IState {
2122
}
2223

2324
export class MultiSelectControl extends React.Component<IProps, IState> {
24-
25+
2526
constructor(props: Readonly<IProps>) {
26-
super(props);
27+
super(props);
2728
this.state = { value: props.value};
28-
}
29+
}
2930

3031
componentWillReceiveProps(p: IProps)
3132
{
3233
this.setState({value : (p.value)});
33-
console.log("react props");
3434
}
3535

3636
onChange = (ob: any) =>
@@ -49,7 +49,6 @@ export class MultiSelectControl extends React.Component<IProps, IState> {
4949

5050
loadOptions = async (inputValue: string) => {
5151
const res = this.props.onSearch(inputValue);
52-
console.log("returning" + JSON.stringify(res));
5352
return res;
5453
}
5554

@@ -64,18 +63,21 @@ export class MultiSelectControl extends React.Component<IProps, IState> {
6463
<AsyncSelect
6564
isMulti={true}
6665
menuPortalTarget={document.body}
66+
defaultOptions
6767
styles={selectStyles}
6868
getOptionLabel={e => e[this.props.displayFieldLabel]}
69-
getOptionValue={e => e[this.props.displayValueField]}
69+
getOptionValue={e => e[this.props.displayValueField]}
7070
loadOptions={this.loadOptions}
71-
defaultOptions
7271
isDisabled={this.props.isControlDisabled}
73-
onChange={this.onChange}
72+
onChange={this.onChange}
73+
defaultValue={this.props.initialValues}
7474
/></div>
7575
)
7676
}
7777
else{
7878
return (<></>);
7979
};
8080
}
81-
}
81+
}
82+
83+

controls/pcfcontroldemo/MultiSelectControl/index.ts

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ import {IInputs, IOutputs} from "./generated/ManifestTypes";
22
import * as React from "react";
33
import * as ReactDOM from "react-dom";
44
import { IProps, MultiSelectControl } from "./MultiSelect";
5+
import { defaultProps } from "react-select/src/Select";
6+
import { debug } from "console";
57

68
export class MultiSelectPCFControl implements ComponentFramework.StandardControl<IInputs, IOutputs> {
79

10+
private _existingValues: any
811
private _value: any;
912
private _notifyOutputChanged:() => void;
1013
private _container: HTMLDivElement;
1114
private props: IProps =
1215
{
1316
value : "",
1417
onChange : this.notifyChange.bind(this),
15-
onSearch : this.notifySearch.bind(this),
18+
onSearch : this.notifySearch.bind(this),
19+
initialValues : undefined,
1620
records: [],
1721
displayValueField: "",
1822
displayFieldLabel: "",
@@ -41,7 +45,7 @@ export class MultiSelectPCFControl implements ComponentFramework.StandardControl
4145
* @param state A piece of data that persists in one session for a single user. Can be set at any point in a controls life cycle by calling 'setControlState' in the Mode interface.
4246
* @param container If a control is marked control-type='starndard', it will receive an empty div element within which it can render its content.
4347
*/
44-
public init(context: ComponentFramework.Context<IInputs>, notifyOutputChanged: () => void, state: ComponentFramework.Dictionary, container:HTMLDivElement)
48+
public async init(context: ComponentFramework.Context<IInputs>, notifyOutputChanged: () => void, state: ComponentFramework.Dictionary, container:HTMLDivElement)
4549
{
4650
// Add control initialization code
4751
this._context = context;
@@ -54,9 +58,20 @@ export class MultiSelectPCFControl implements ComponentFramework.StandardControl
5458
this.props.columns = context.parameters.columns.raw || "";
5559
this.props.displayFieldLabel = context.parameters.displayFieldLabel.raw || "";
5660
this.props.displayValueField = context.parameters.displayValueField.raw || "";
57-
61+
62+
if(this.props.value.length > 0)
63+
{
64+
this.props.initialValues = await this.onLoad();
65+
console.log(JSON.stringify(this.props.initialValues));
66+
this.updateView(context);
67+
}
68+
else
69+
{
70+
this.props.initialValues =[];
71+
}
72+
5873
container.appendChild(this._container);
59-
console.log("init");
74+
6075
}
6176

6277
notifyChange(newValue: string)
@@ -77,16 +92,51 @@ export class MultiSelectPCFControl implements ComponentFramework.StandardControl
7792
})
7893
}
7994

95+
//Load previous values
96+
public async onLoad()
97+
{
98+
var count = 0;
99+
var qs = `?$select=${this.props.columns}&$filter=`;
100+
101+
this.props.value.split(",").forEach(c=>{
102+
if (count > 0)
103+
{
104+
qs = qs + ' or ' + this.props.displayValueField + ' eq ' + c
105+
}
106+
else
107+
{
108+
qs = qs + this.props.displayValueField + ' eq ' + c
109+
}
110+
count++;
111+
});
112+
113+
console.log("querystring is " + qs);
114+
115+
return this._context.webAPI.retrieveMultipleRecords(this.props.entityName,qs)
116+
.then(function (results) {
117+
return results?.entities.map(val => ({
118+
accountid: val.accountid,
119+
name: val.name
120+
}));
121+
122+
});
123+
124+
}
125+
80126
private renderElement()
81127
{
82-
this.props.isControlDisabled = this._context.mode.isControlDisabled;
83-
this.props.isControlVisible = this._context.mode.isVisible;
84-
85-
ReactDOM.render(
86-
React.createElement(MultiSelectControl, this.props)
87-
, this._container
88-
);
89-
console.log("viewUpdated");
128+
if(this.props.initialValues != undefined)
129+
{
130+
this.props.isControlDisabled = this._context.mode.isControlDisabled;
131+
this.props.isControlVisible = this._context.mode.isVisible;
132+
133+
ReactDOM.render(
134+
React.createElement(MultiSelectControl, this.props)
135+
, this._container
136+
);
137+
console.log("viewUpdated");
138+
}
139+
90140
}
91141

92142
/**
@@ -105,7 +155,7 @@ export class MultiSelectPCFControl implements ComponentFramework.StandardControl
105155
this.props.displayFieldLabel = context.parameters.displayFieldLabel.raw;
106156
this.props.displayValueField = context.parameters.displayValueField.raw;
107157
this.props.entityName =context.parameters.entityName.raw;
108-
158+
109159
this.renderElement();
110160
}
111161

0 commit comments

Comments
 (0)