- Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathuser-fields-component.tsx
106 lines (100 loc) · 3.35 KB
/
user-fields-component.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
importReactfrom'@theia/core/shared/react';
import{BoardUserField}from'../../../common/protocol';
import{nls}from'@theia/core/lib/common';
exportconstUserFieldsComponent=({
initialBoardUserFields,
updateUserFields,
cancel,
accept,
}: {
initialBoardUserFields: BoardUserField[];
updateUserFields: (userFields: BoardUserField[])=>void;
cancel: ()=>void;
accept: ()=>Promise<void>;
}): React.ReactElement=>{
const[boardUserFields,setBoardUserFields]=React.useState<
BoardUserField[]
>(initialBoardUserFields);
const[uploadButtonDisabled,setUploadButtonDisabled]=
React.useState<boolean>(true);
constfirstInputElement=React.useRef<HTMLInputElement>(null);
React.useEffect(()=>{
setBoardUserFields(initialBoardUserFields);
},[initialBoardUserFields]);
constupdateUserField=
(index: number)=>(e: React.ChangeEvent<HTMLInputElement>)=>{
constnewBoardUserFields=[...boardUserFields];
newBoardUserFields[index].value=e.target.value;
setBoardUserFields(newBoardUserFields);
};
constallFieldsHaveValues=(userFields: BoardUserField[]): boolean=>{
return(
userFields&&
userFields.length>0&&
userFields
.map<boolean>((field: BoardUserField): boolean=>{
returnfield.value.length>0;
})
.reduce((previous: boolean,current: boolean): boolean=>{
returnprevious&¤t;
})
);
};
React.useEffect(()=>{
updateUserFields(boardUserFields);
setUploadButtonDisabled(!allFieldsHaveValues(boardUserFields));
if(firstInputElement.current){
firstInputElement.current.focus();
}
},[boardUserFields,updateUserFields]);
return(
<div>
<divclassName="user-fields-container">
<divclassName="user-fields-list">
{boardUserFields.map((field,index)=>{
return(
<divclassName="dialogSection"key={index}>
<divclassName="dialogRow">
<labelclassName="field-label">{field.label}</label>
</div>
<divclassName="dialogRow">
<input
type={field.secret ? 'password' : 'text'}
value={field.value}
className="theia-input"
placeholder={nls.localize(
'arduino/userFields/enterField',
'Enter {0}',
field.label
)}
onChange={updateUserField(index)}
ref={index===0 ? firstInputElement : undefined}
/>
</div>
</div>
);
})}
</div>
</div>
<divclassName="dialogSection">
<divclassName="dialogRow button-container">
<button
type="button"
className="theia-button secondary install-cert-btn"
onClick={cancel}
>
{nls.localize('arduino/userFields/cancel','Cancel')}
</button>
<button
type="button"
className="theia-button primary install-cert-btn"
disabled={uploadButtonDisabled}
onClick={accept}
>
{nls.localize('arduino/userFields/upload','Upload')}
</button>
</div>
</div>
</div>
);
};