|
| 1 | +import * as React from 'react'; |
| 2 | +import { Callout, Stack, TextField, DefaultButton, BaseButton, Button, IStackStyles, ITextFieldStyles, ICalloutContentStyles, DirectionalHint } from 'office-ui-fabric-react'; |
| 3 | +import { CompositeValue } from '../EntitiesDefinition'; |
| 4 | + |
| 5 | +export interface ICompositeControlProps { |
| 6 | + disabled : boolean; |
| 7 | + visible: boolean; |
| 8 | + compositeValue : CompositeValue; |
| 9 | + doneLabel : string; |
| 10 | + randNumber: number; |
| 11 | + onClickedDone : (compositeValue? : CompositeValue) => void; |
| 12 | +} |
| 13 | + |
| 14 | +export interface IBCompositeControlState { |
| 15 | + showCallout: boolean, |
| 16 | + compositeValue : CompositeValue; |
| 17 | +} |
| 18 | + |
| 19 | +const stackStyles: Partial<IStackStyles> = { root: { width: "100%" } }; |
| 20 | +const textFieldStyles: Partial<ITextFieldStyles> = { root: { width: "100%" } }; |
| 21 | +const calloutStyles: Partial<ICalloutContentStyles> = { root: { width: "300px" } }; |
| 22 | + |
| 23 | +export default class CompositeControl extends React.Component<ICompositeControlProps, IBCompositeControlState> { |
| 24 | + constructor(props: ICompositeControlProps) { |
| 25 | + super(props); |
| 26 | + this.state = { |
| 27 | + showCallout : false, |
| 28 | + compositeValue : this.props.compositeValue, |
| 29 | + }; |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + render(){ |
| 35 | + return ( |
| 36 | + <Stack horizontal id="acf_compositestack" styles={stackStyles}> |
| 37 | + <TextField |
| 38 | + value={this.state.compositeValue.fullValue} |
| 39 | + readOnly={true} |
| 40 | + onClick={() => this.setState({ showCallout : true }) } |
| 41 | + styles={textFieldStyles} |
| 42 | + id={"acf_compositeFullValue"+this.props.randNumber} |
| 43 | + /> |
| 44 | + {this.state.showCallout && ( |
| 45 | + <Callout |
| 46 | + target={"#acf_compositeFullValue"+this.props.randNumber} |
| 47 | + onDismiss={() => this.setState({ showCallout : false }) } |
| 48 | + styles={calloutStyles} |
| 49 | + directionalHint={DirectionalHint.topCenter} |
| 50 | + > |
| 51 | + <Stack style={{margin : "10px"}}> |
| 52 | + |
| 53 | + {this.state.compositeValue.fieldValue1.attributes.LogicalName != undefined && <TextField |
| 54 | + value={this.state.compositeValue.fieldValue1.raw!} |
| 55 | + label={this.state.compositeValue.fieldValue1.attributes.DisplayName} |
| 56 | + id={"acf_fieldValue1"} |
| 57 | + onChange={this.onChangeField} |
| 58 | + disabled={this.props.disabled || this.state.compositeValue.fieldValue1.disabled!} |
| 59 | + styles={textFieldStyles} |
| 60 | + />} |
| 61 | + {this.state.compositeValue.fieldValue2.attributes.LogicalName != undefined && <TextField |
| 62 | + value={this.state.compositeValue.fieldValue2.raw!} |
| 63 | + label={this.state.compositeValue.fieldValue2.attributes.DisplayName} |
| 64 | + id={"acf_fieldValue2"} |
| 65 | + onChange={this.onChangeField} |
| 66 | + disabled={this.props.disabled || this.state.compositeValue.fieldValue2.disabled!} |
| 67 | + styles={textFieldStyles} |
| 68 | + />} |
| 69 | + {this.state.compositeValue.fieldValue3.attributes.LogicalName != undefined && <TextField |
| 70 | + value={this.state.compositeValue.fieldValue3.raw!} |
| 71 | + label={this.state.compositeValue.fieldValue3.attributes.DisplayName} |
| 72 | + id={"acf_fieldValue3"} |
| 73 | + onChange={this.onChangeField} |
| 74 | + disabled={this.props.disabled || this.state.compositeValue.fieldValue3.disabled!} |
| 75 | + styles={textFieldStyles} |
| 76 | + />} |
| 77 | + {this.state.compositeValue.fieldValue4.attributes.LogicalName != undefined && <TextField |
| 78 | + value={this.state.compositeValue.fieldValue4.raw!} |
| 79 | + label={this.state.compositeValue.fieldValue4.attributes.DisplayName} |
| 80 | + id={"acf_fieldValue4"} |
| 81 | + onChange={this.onChangeField} |
| 82 | + disabled={this.props.disabled || this.state.compositeValue.fieldValue4.disabled!} |
| 83 | + styles={textFieldStyles} |
| 84 | + />} |
| 85 | + {this.state.compositeValue.fieldValue5.attributes.LogicalName != undefined && <TextField |
| 86 | + value={this.state.compositeValue.fieldValue5.raw!} |
| 87 | + label={this.state.compositeValue.fieldValue5.attributes.DisplayName} |
| 88 | + id={"acf_fieldValue5"} |
| 89 | + onChange={this.onChangeField} |
| 90 | + disabled={this.props.disabled || this.state.compositeValue.fieldValue5.disabled!} |
| 91 | + styles={textFieldStyles} |
| 92 | + />} |
| 93 | + {this.state.compositeValue.fieldValue6.attributes.LogicalName != undefined && <TextField |
| 94 | + value={this.state.compositeValue.fieldValue6.raw!} |
| 95 | + label={this.state.compositeValue.fieldValue6.attributes.DisplayName} |
| 96 | + id={"acf_fieldValue6"} |
| 97 | + onChange={this.onChangeField} |
| 98 | + disabled={this.props.disabled || this.state.compositeValue.fieldValue6.disabled!} |
| 99 | + styles={textFieldStyles} |
| 100 | + />} |
| 101 | + {this.state.compositeValue.fieldValue7.attributes.LogicalName != undefined && <TextField |
| 102 | + value={this.state.compositeValue.fieldValue7.raw!} |
| 103 | + label={this.state.compositeValue.fieldValue7.attributes.DisplayName} |
| 104 | + id={"acf_fieldValue7"} |
| 105 | + onChange={this.onChangeField} |
| 106 | + disabled={this.props.disabled || this.state.compositeValue.fieldValue7.disabled!} |
| 107 | + styles={textFieldStyles} |
| 108 | + />} |
| 109 | + {this.state.compositeValue.fieldValue8.attributes.LogicalName != undefined && <TextField |
| 110 | + value={this.state.compositeValue.fieldValue8.raw!} |
| 111 | + label={this.state.compositeValue.fieldValue8.attributes.DisplayName} |
| 112 | + id={"acf_fieldValue8"} |
| 113 | + onChange={this.onChangeField} |
| 114 | + disabled={this.props.disabled || this.state.compositeValue.fieldValue8.disabled!} |
| 115 | + styles={textFieldStyles} |
| 116 | + />} |
| 117 | + |
| 118 | + <DefaultButton text={this.props.doneLabel} onClick={this.onClick} style={{marginTop:'10px',alignSelf: "flex-end"}}/> |
| 119 | + </Stack> |
| 120 | + |
| 121 | + </Callout> |
| 122 | + )} |
| 123 | + </Stack> |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + private onChangeField = (event: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, newValue?: string | undefined) : void => { |
| 128 | + // @ts-ignore |
| 129 | + let target = event.target.id.replace('acf_', ''); |
| 130 | + const compositeValue = {...this.state}.compositeValue; |
| 131 | + // @ts-ignore |
| 132 | + compositeValue[target].raw = newValue!; |
| 133 | + this.setState({compositeValue : compositeValue}); |
| 134 | + } |
| 135 | + |
| 136 | + private onClick = (event: React.MouseEvent<HTMLDivElement | HTMLAnchorElement | HTMLButtonElement | BaseButton | Button | HTMLSpanElement, MouseEvent>) : void => { |
| 137 | + const compositeValue = {...this.state}.compositeValue; |
| 138 | + this.buildFullValue(compositeValue); |
| 139 | + this.setState({showCallout : false}); |
| 140 | + this.props.onClickedDone(this.state.compositeValue); |
| 141 | + } |
| 142 | + |
| 143 | + private buildFullValue = (compositeValue : CompositeValue) : void => { |
| 144 | + let arrayValues = []; |
| 145 | + |
| 146 | + let i = 1; |
| 147 | + for(i ; i<9; i++){ |
| 148 | + // @ts-ignore |
| 149 | + if(compositeValue["fieldValue"+i]!.raw!){ |
| 150 | + // @ts-ignore |
| 151 | + arrayValues.push(compositeValue["fieldValue"+i].raw); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + compositeValue.fullValue = arrayValues.join(compositeValue.separator); |
| 156 | + |
| 157 | + this.setState({compositeValue : compositeValue}); |
| 158 | + } |
| 159 | +}; |
| 160 | + |
0 commit comments