FieldList.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import {
  4. TextField,
  5. Grid,
  6. InputBase,
  7. Button,
  8. NativeSelect,
  9. Box,
  10. Typography
  11. } from '@material-ui/core';
  12. import { withStyles, makeStyles } from '@material-ui/core/styles';
  13. import Autocomplete, { createFilterOptions } from '@material-ui/lab/Autocomplete';
  14. import { MButton } from '..';
  15. const filter = createFilterOptions();
  16. const BootstrapInput = withStyles((theme) => ({
  17. root: {
  18. 'label + &': {
  19. marginTop: theme.spacing(3),
  20. },
  21. },
  22. input: {
  23. borderRadius: 4,
  24. position: 'relative',
  25. backgroundColor: 'transparent',
  26. border: '1px solid #ced4da',
  27. fontSize: 16,
  28. padding: '10px 26px 10px 12px',
  29. transition: theme.transitions.create(['border-color', 'box-shadow']),
  30. // Use the system font instead of the default Roboto font.
  31. fontFamily: [
  32. '-apple-system',
  33. 'BlinkMacSystemFont',
  34. '"Segoe UI"',
  35. 'Roboto',
  36. '"Helvetica Neue"',
  37. 'Arial',
  38. 'sans-serif',
  39. '"Apple Color Emoji"',
  40. '"Segoe UI Emoji"',
  41. '"Segoe UI Symbol"',
  42. ].join(','),
  43. '&:focus': {
  44. borderRadius: 4,
  45. borderColor: '#80bdff',
  46. boxShadow: '0 0 0 0.2rem rgba(0,123,255,.25)',
  47. },
  48. },
  49. }))(InputBase);
  50. const useStyles = makeStyles((theme) => ({
  51. root: {
  52. width: '100%',
  53. },
  54. paper: {
  55. width: '100%',
  56. marginBottom: theme.spacing(2),
  57. },
  58. table: {
  59. minWidth: 750,
  60. },
  61. visuallyHidden: {
  62. border: 0,
  63. clip: 'rect(0 0 0 0)',
  64. height: 1,
  65. margin: -1,
  66. overflow: 'hidden',
  67. padding: 0,
  68. position: 'absolute',
  69. top: 20,
  70. width: 1,
  71. },
  72. underline: {
  73. "&&&:before": {
  74. borderBottom: "none"
  75. },
  76. "&&:after": {
  77. borderBottom: "none"
  78. }
  79. }
  80. }));
  81. function FieldList(props) {
  82. const classes = useStyles();
  83. const {
  84. fields = [],
  85. updateData = {},
  86. isNew = false,
  87. currentTabName,
  88. onCancel,
  89. onCanCreateNew,
  90. actions = [],
  91. } = props;
  92. const [data, setDataField] = React.useState(updateData != undefined ? updateData : {});
  93. const handleTextString = (e, fieldName) => {
  94. setDataField({ ...data, [fieldName]: e.target.value });
  95. }
  96. const handleTextNumber = (e, fieldName) => {
  97. setDataField({ ...data, [fieldName]: e.target.value });
  98. }
  99. const handleTextMultiline = (e, fieldName) => {
  100. setDataField({ ...data, [fieldName]: e.target.value });
  101. }
  102. const handleDate = (e, fieldName) => {
  103. setDataField({ ...data, [fieldName]: e.target.value });
  104. }
  105. const handleDropDownChange = (e, fieldName) => {
  106. var selectedIndex = e.target.options.selectedIndex;
  107. var selectedValue = e.target.options[selectedIndex].getAttribute('name');
  108. var fn = fieldName.split('_');
  109. var fieldId = fn[0] + '_' + 'id';
  110. setDataField({ ...data, [fieldName]: selectedValue, [fieldId]: e.target.value });
  111. }
  112. const handleImgUpload = (e, fieldName) => {
  113. e.preventDefault();
  114. let reader = new FileReader();
  115. let file = e.target.files[0];
  116. reader.onloadend = () => {
  117. setImgPreviewPath(reader.result);
  118. }
  119. reader.readAsDataURL(file);
  120. setDataField({ ...data, [fieldName]: e.target.files[0].name });
  121. }
  122. const handleCancel = () => {
  123. onCancel();
  124. }
  125. const handleCanCreateNew = (data) => {
  126. onCanCreateNew(data);
  127. }
  128. const onChangeValue = (fieldName, value) => {
  129. var fn = fieldName.split('_');
  130. var fieldId = fn[0] + '_' + 'id';
  131. var d = value['name'] != undefined ? value['name'] : value['product_desc']
  132. if (isNew) {
  133. setDataField({ ...data, [fieldName]: d, [fieldId]: value['id'] });
  134. } else {
  135. setDataField({ ...data, [fieldName]: d, [fieldId]: value['id'] });
  136. }
  137. }
  138. return (
  139. <div className={classes.root}>
  140. <Grid container>
  141. <Grid item xs={12}>
  142. {fields.map((f, i) => {
  143. if (f.type == 'text_string') {
  144. return <Grid key={f.fieldName} container style={{ display: 'flex', alignItems: 'center', marginBottom: '10px' }}>
  145. <Grid item xs={12} sm={5}>
  146. <Box style={{ width: '150px' }}>
  147. <Typography style={{ paddingRight: '30px', color: 'grey' }}>{f.label}</Typography>
  148. </Box>
  149. </Grid>
  150. <Grid item xs={12} sm={7}>
  151. <TextField id={f.fieldName}
  152. variant="outlined"
  153. autoComplete="off"
  154. size={"small"}
  155. style={{ width: '100%' }}
  156. InputProps={{
  157. readOnly: f.readOnly ? f.readOnly : false,
  158. }}
  159. value={data != undefined ? data[f.fieldName] : ''}
  160. onChange={(e) => handleTextString(e, f.fieldName)}
  161. />
  162. </Grid>
  163. </Grid>;
  164. }
  165. else if (f.type == 'text_number') {
  166. return <Grid key={f.fieldName} container style={{ display: 'flex', alignItems: 'center', marginBottom: '10px' }}>
  167. <Grid item xs={12} sm={5}>
  168. <Box style={{ width: '150px' }}>
  169. <Typography style={{ paddingRight: '30px', color: 'grey' }}>{f.label}</Typography>
  170. </Box>
  171. </Grid>
  172. <Grid item xs={12} sm={7}>
  173. <TextField
  174. id={f.fieldName}
  175. variant="outlined"
  176. autoComplete="off"
  177. size={"small"}
  178. style={{ width: '100%' }}
  179. type="number"
  180. value={data != undefined ? data[f.fieldName] : ''}
  181. onChange={(e) => handleTextNumber(e, f.fieldName)}
  182. />
  183. </Grid>
  184. </Grid>;
  185. }
  186. else if (f.type == 'text_multiline') {
  187. return <Grid key={f.fieldName} container style={{ display: 'flex', alignItems: 'center', marginBottom: '10px' }}>
  188. <Grid item xs={12} sm={5}>
  189. <Box style={{ width: '150px' }}>
  190. <Typography style={{ paddingRight: '30px', color: 'grey' }}>{f.label}</Typography>
  191. </Box>
  192. </Grid>
  193. <Grid item xs={12} sm={7}>
  194. <TextField
  195. id={f.fieldName}
  196. multiline
  197. autoComplete="off"
  198. rows={3}
  199. size={"small"}
  200. style={{ width: '100%' }}
  201. value={data != undefined ? data[f.fieldName] : ''}
  202. variant="outlined"
  203. onChange={(e) => handleTextMultiline(e, f.fieldName)}
  204. />
  205. </Grid>
  206. </Grid>;
  207. }
  208. else if (f.type == 'date') {
  209. return <Grid key={f.fieldName} container style={{ display: 'flex', alignItems: 'center', marginBottom: '10px' }}>
  210. <Grid item xs={12} sm={5}>
  211. <Box style={{ width: '150px' }}>
  212. <Typography style={{ paddingRight: '30px', color: 'grey' }}>{f.label}</Typography>
  213. </Box>
  214. </Grid>
  215. <Grid item xs={12} sm={7}>
  216. <TextField
  217. id={f.fieldName}
  218. variant="outlined"
  219. autoComplete="off"
  220. size={"small"}
  221. value={data != undefined ? data[f.fieldName] : ''}
  222. type="date"
  223. style={{ width: '100%' }}
  224. onChange={(e) => handleDate(e, f.fieldName)}
  225. />
  226. </Grid>
  227. </Grid>;
  228. }
  229. else if (f.type == 'dropdown') {
  230. if (f.options != undefined) {
  231. if (f.fieldName == 'priority') {
  232. return <Grid key={f.fieldName} container style={{ display: 'flex', alignItems: 'center', marginBottom: '10px' }}>
  233. <Grid item xs={12} sm={5}>
  234. <Box style={{ width: '150px' }}>
  235. <Typography style={{ paddingRight: '30px', color: 'grey' }}>{f.label}</Typography>
  236. </Box>
  237. </Grid>
  238. <Grid item xs={12} sm={7}>
  239. <NativeSelect
  240. id="demo-customized-select-native"
  241. value={data != undefined ? data[f.fieldName] : ''}
  242. onChange={(e) => handleDropDownChange(e, f.fieldName)}
  243. id={f.fieldName}
  244. input={<BootstrapInput />}
  245. style={{ width: '100%' }}
  246. >
  247. <option aria-label="None" value="" >Select</option>
  248. {f.options.map((d, i) => {
  249. return <option name={d.name} value={d.id}>{d.name}</option>;
  250. })}
  251. </NativeSelect>
  252. </Grid>
  253. </Grid>;
  254. } else {
  255. var fn = f.fieldName.split('_');
  256. var fieldId = fn[0] + '_' + 'id';
  257. if (isNew) {
  258. return <Grid key={f.fieldName} container style={{ display: 'flex', alignItems: 'center', marginBottom: '10px' }}>
  259. <Grid item xs={12} sm={5}>
  260. <Box style={{ width: '150px' }}>
  261. <Typography style={{ paddingRight: '30px', color: 'grey' }}>{f.label}</Typography>
  262. </Box>
  263. </Grid>
  264. <Grid item xs={12} sm={7}>
  265. <Autocomplete
  266. id="combo-box-demo"
  267. options={f.options}
  268. getOptionLabel={(option) => {
  269. if (typeof option === 'string') {
  270. return option;
  271. }
  272. return option[f.fieldName] != undefined ? option[f.fieldName] : option.name != undefined ? option.name : option.product_desc != undefined ? option.product_desc : '';
  273. }}
  274. style={{ width: '100%' }}
  275. size='small'
  276. value={data != undefined ? data[f.fieldName] ? data[f.fieldName] : " " : " "}
  277. filterOptions={(options, params) => {
  278. console.log("Autocomplete", f.canCreate);
  279. if (f.canCreate) {
  280. var newFilter = ['+ Add New']
  281. var filtered = filter(options, params);
  282. return [...newFilter, ...filtered];
  283. } else {
  284. var filtered = filter(options, params);
  285. return filtered;
  286. }
  287. }}
  288. onChange={(event, newValue) => {
  289. if (typeof newValue === 'string') {
  290. console.log('f.fieldName', f.fieldName, " f.canCreate", f.canCreate);
  291. var d = {
  292. "canCreate": f.canCreate,
  293. "fields": f.fields,
  294. "name": f.name,
  295. "fieldName": f.fieldName
  296. }
  297. handleCanCreateNew(d);
  298. } else {
  299. if (newValue != null && newValue.inputValue != '' && newValue.product_desc != "") {
  300. onChangeValue(f.fieldName, newValue);
  301. }
  302. }
  303. }}
  304. renderInput={(params) => <TextField {...params} variant="outlined" />}
  305. />
  306. </Grid>
  307. </Grid>;
  308. }
  309. else {
  310. return <Grid key={f.fieldName} container style={{ display: 'flex', alignItems: 'center', marginBottom: '10px' }}>
  311. <Grid item xs={12} sm={5}>
  312. <Box style={{ width: '150px' }}>
  313. <Typography style={{ paddingRight: '30px', color: 'grey' }}>{f.label}</Typography>
  314. </Box>
  315. </Grid>
  316. <Grid item xs={12} sm={7}>
  317. <NativeSelect
  318. id="demo-customized-select-native"
  319. value={data != undefined ? data[fieldId] : ''}
  320. onChange={(e) => handleDropDownChange(e, f.fieldName)}
  321. id={f.fieldName}
  322. input={<BootstrapInput />}
  323. style={{ width: '100%' }}
  324. >
  325. <option aria-label="None" value="" >Select</option>
  326. {f.options.map((d, i) => {
  327. var name = d[f.fieldName] != undefined ? d[f.fieldName] : d.name;
  328. return <option name={name} value={d.id}>{name}</option>;
  329. })}
  330. </NativeSelect>
  331. </Grid>
  332. </Grid>;
  333. }
  334. }
  335. }
  336. }
  337. else if (f.type == 'photo_list') {
  338. console.log('photo_list:', data);
  339. return <div>
  340. <Grid
  341. key={f.fieldName} container
  342. style={{ display: 'flex', alignItems: 'center', marginBottom: '10px' }}>
  343. <Grid item xs={12} sm={5}>
  344. <Box style={{ width: '150px' }}>
  345. <Typography style={{ paddingRight: '30px', color: 'grey' }}>{f.label}</Typography>
  346. </Box>
  347. </Grid>
  348. <Grid item xs={12} sm={7}>
  349. <div style={{ display: 'flex', alignItems: 'center', marginBottom: '10px' }}>
  350. <form>
  351. <div className="form-group">
  352. <input type="file" name="imgCollection"
  353. onChange={(e) => onFileChange(e, f.fieldName)} multiple />
  354. </div>
  355. </form>
  356. </div>
  357. </Grid>
  358. </Grid>
  359. {data[f.fieldName] != undefined && data[f.fieldName].length != 0 ?
  360. <Grid
  361. key={f.fieldName} container
  362. style={{ display: 'flex', alignItems: 'center', marginBottom: '10px' }}>
  363. <Grid item xs={12}>
  364. <div style={{ display: 'block', alignItems: 'center', marginBottom: '10px' }}>
  365. <GridList className={classes.gridList}>
  366. {data[f.fieldName] == undefined ? <span /> : data[f.fieldName].map((tile) => (
  367. <GridListTile key={tile} style={{ width: '100px', height: '100px' }}>
  368. <img src={tile} alt={tile} onClick={(e) => {
  369. setSelectedPhoto(tile);
  370. setOpen(true);
  371. }
  372. } />
  373. </GridListTile>
  374. ))}
  375. </GridList>
  376. </div>
  377. </Grid>
  378. <Dialog maxWidth="lg" aria-labelledby="customized-dialog-title" open={open}>
  379. <DialogTitle id="customized-dialog-title" onClose={(e) => setOpen(false)} >
  380. Photos
  381. </DialogTitle>
  382. <DialogContent dividers>
  383. <Grid item xs={12}>
  384. <Grid>
  385. <img src={selectedPhoto} className="show-img" alt="logo" />
  386. </Grid>
  387. <br />
  388. <Grid container spacing={3}>
  389. {data[f.fieldName].length > 0 ? data[f.fieldName].map((value) => (
  390. <Grid key={value} item>
  391. <Box className="square" > <img src={value} className="thumnail-img" alt="logo" onClick={(e) => setSelectedPhoto(value)} /></Box>
  392. </Grid>
  393. )) : <span />}
  394. </Grid>
  395. </Grid>
  396. </DialogContent>
  397. </Dialog>
  398. </Grid>
  399. : <Grid></Grid>}
  400. </div>;
  401. }
  402. else if (f.type == 'list') {
  403. console.log('list', data[f.fieldName]);
  404. return <div>
  405. <Grid
  406. key={f.fieldName} container
  407. style={{ display: 'flex', alignItems: 'center', marginBottom: '10px' }}>
  408. <Grid item xs={12} sm={5}>
  409. <Box style={{ width: '150px' }}>
  410. <Typography style={{ paddingRight: '30px', color: 'grey' }}>{f.label}</Typography>
  411. </Box>
  412. </Grid>
  413. <Grid item xs={12} sm={7}>
  414. <div style={{ display: 'block', alignItems: 'center', marginBottom: '10px' }}>
  415. <Box>
  416. <Button onClick={handleSelectItemDialog}><AddIcon /></Button>
  417. </Box>
  418. </div>
  419. </Grid>
  420. </Grid>
  421. <Grid
  422. key={f.fieldName} container
  423. style={{ display: 'block', alignItems: 'center', marginBottom: '10px' }}>
  424. <div style={{ display: 'block', alignItems: 'center', marginBottom: '10px' }}>
  425. <TableContainer>
  426. <Table className={classes.table} size="small" aria-label="a dense table">
  427. <TableHead>
  428. <TableRow>
  429. {partHeaders.map((h, i) => {
  430. return (<TableCell key={h.id} align='left'>{h.label}</TableCell>);
  431. })}
  432. </TableRow>
  433. </TableHead>
  434. <TableBody>
  435. {data[f.fieldName].length > 0 ? data[f.fieldName].map((row) => (
  436. <TableRow key={row.name}>
  437. {partHeaders.map((h, i) => {
  438. return (<TableCell key={h.id} align={h.numeric ? 'right' : 'left'}>{row[h.id]}</TableCell>);
  439. })}
  440. </TableRow>
  441. )) : <span />
  442. }
  443. </TableBody>
  444. </Table>
  445. </TableContainer>
  446. </div>
  447. </Grid>
  448. </div>;
  449. }
  450. else if (f.type == 'time') {
  451. return <Grid key={f.fieldName} container style={{ display: 'flex', alignItems: 'center', marginBottom: '10px' }}>
  452. <Grid item xs={12} sm={5}>
  453. <Box style={{ width: '150px' }}>
  454. <Typography style={{ paddingRight: '30px', color: 'grey' }}>{f.label}</Typography>
  455. </Box>
  456. </Grid>
  457. <Grid item xs={12} sm={7}> <TextField
  458. id="time"
  459. variant="outlined"
  460. size="small"
  461. type="time"
  462. className={classes.textField}
  463. InputLabelProps={{
  464. shrink: true,
  465. }}
  466. inputProps={{
  467. step: 300, // 5 min
  468. }}
  469. onChange={(e) => handleTime(e, f.fieldName)}
  470. />
  471. </Grid>
  472. </Grid>;
  473. }
  474. })}
  475. </Grid>
  476. {/* display actions buttons */}
  477. {actions.length > 0 ?
  478. currentTabName === 'account' ? <Grid item xs={12}>
  479. {data != undefined ?
  480. actions.map((a) => {
  481. if (a.status === data.status) {
  482. return <MButton action={a} onCallback={a.callback(data)}></MButton>;
  483. }
  484. })
  485. : <Grid />
  486. }
  487. </Grid>
  488. : <Grid item xs={12}>
  489. {actions.map((a) => {
  490. if (a.status === data.status && currentTabName !== 'account') {
  491. return <MButton action={a} onCallback={a.callback(data)}></MButton>;
  492. }
  493. })}
  494. </Grid>
  495. : <Grid />}
  496. </Grid>
  497. </div>
  498. );
  499. }
  500. FieldList.propTypes = {
  501. history: PropTypes.object,
  502. fields: PropTypes.array.isRequired,
  503. updateData: PropTypes.object,
  504. isNew: PropTypes.bool,
  505. currentTabName: PropTypes.string,
  506. onCancel: PropTypes.func,
  507. actions: PropTypes.array,
  508. onCanCreateNew: PropTypes.func
  509. };
  510. export default (FieldList);