@@ -2,17 +2,21 @@ import {IInputs, IOutputs} from "./generated/ManifestTypes";
22import * as React from "react" ;
33import * as ReactDOM from "react-dom" ;
44import { IProps , MultiSelectControl } from "./MultiSelect" ;
5+ import { defaultProps } from "react-select/src/Select" ;
6+ import { debug } from "console" ;
57
68export 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