Fork of the excellent esp8266-react - https://github.com/rjwats/esp8266-react
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.6 KiB

5 years ago
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { TextValidator, ValidatorForm } from 'react-material-ui-form-validator';
  4. import { withStyles } from '@material-ui/core/styles';
  5. import Button from '@material-ui/core/Button';
  6. import FormControlLabel from '@material-ui/core/FormControlLabel';
  7. import Switch from '@material-ui/core/Switch';
  8. import FormGroup from '@material-ui/core/FormGroup';
  9. import DialogTitle from '@material-ui/core/DialogTitle';
  10. import Dialog from '@material-ui/core/Dialog';
  11. import DialogContent from '@material-ui/core/DialogContent';
  12. import DialogActions from '@material-ui/core/DialogActions';
  13. import PasswordValidator from '../components/PasswordValidator';
  14. const styles = theme => ({
  15. textField: {
  16. width: "100%"
  17. },
  18. button: {
  19. margin: theme.spacing(0.5)
  20. }
  21. });
  22. class UserForm extends React.Component {
  23. constructor(props) {
  24. super(props);
  25. this.formRef = React.createRef();
  26. }
  27. componentWillMount() {
  28. ValidatorForm.addValidationRule('uniqueUsername', this.props.uniqueUsername);
  29. }
  30. submit = () => {
  31. this.formRef.current.submit();
  32. }
  33. render() {
  34. const { classes, user, creating, handleValueChange, handleCheckboxChange, onDoneEditing, onCancelEditing } = this.props;
  35. return (
  36. <ValidatorForm onSubmit={onDoneEditing} ref={this.formRef}>
  37. <Dialog onClose={onCancelEditing} aria-labelledby="user-form-dialog-title" open={true}>
  38. <DialogTitle id="user-form-dialog-title">{creating ? 'Add' : 'Modify'} User</DialogTitle>
  39. <DialogContent dividers={true}>
  40. <TextValidator
  41. validators={creating ? ['required', 'uniqueUsername', 'matchRegexp:^[a-zA-Z0-9_\\.]{1,24}$'] : []}
  42. errorMessages={creating ? ['Username is required', "Username already exists", "Must be 1-24 characters: alpha numeric, '_' or '.'"] : []}
  43. name="username"
  44. label="Username"
  45. className={classes.textField}
  46. value={user.username}
  47. disabled={!creating}
  48. onChange={handleValueChange('username')}
  49. margin="normal"
  50. />
  51. <PasswordValidator
  52. validators={['required', 'matchRegexp:^.{1,64}$']}
  53. errorMessages={['Password is required', 'Password must be 64 characters or less']}
  54. name="password"
  55. label="Password"
  56. className={classes.textField}
  57. value={user.password}
  58. onChange={handleValueChange('password')}
  59. margin="normal"
  60. />
  61. <FormGroup>
  62. <FormControlLabel
  63. control={<Switch checked={user.admin} onChange={handleCheckboxChange('admin')} id="admin" />}
  64. label="Admin?"
  65. />
  66. </FormGroup>
  67. </DialogContent>
  68. <DialogActions>
  69. <Button variant="contained" color="primary" className={classes.button} type="submit" onClick={this.submit}>
  70. Done
  71. </Button>
  72. <Button variant="contained" color="secondary" className={classes.button} type="submit" onClick={onCancelEditing}>
  73. Cancel
  74. </Button>
  75. </DialogActions>
  76. </Dialog>
  77. </ValidatorForm>
  78. );
  79. }
  80. }
  81. UserForm.propTypes = {
  82. classes: PropTypes.object.isRequired,
  83. user: PropTypes.object.isRequired,
  84. creating: PropTypes.bool.isRequired,
  85. onDoneEditing: PropTypes.func.isRequired,
  86. onCancelEditing: PropTypes.func.isRequired,
  87. uniqueUsername: PropTypes.func.isRequired,
  88. handleValueChange: PropTypes.func.isRequired,
  89. handleCheckboxChange: PropTypes.func.isRequired
  90. };
  91. export default withStyles(styles)(UserForm);