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.

86 lines
3.0 KiB

  1. import React, { RefObject } from 'react';
  2. import { TextValidator, ValidatorForm } from 'react-material-ui-form-validator';
  3. import { Dialog, DialogTitle, DialogContent, DialogActions, Checkbox } from '@material-ui/core';
  4. import { PasswordValidator, BlockFormControlLabel, FormButton } from '../components';
  5. import { User } from './types';
  6. interface UserFormProps {
  7. creating: boolean;
  8. user: User;
  9. uniqueUsername: (value: any) => boolean;
  10. handleValueChange: (name: keyof User) => (event: React.ChangeEvent<HTMLInputElement>) => void;
  11. onDoneEditing: () => void;
  12. onCancelEditing: () => void;
  13. }
  14. class UserForm extends React.Component<UserFormProps> {
  15. formRef: RefObject<any> = React.createRef();
  16. componentDidMount() {
  17. ValidatorForm.addValidationRule('uniqueUsername', this.props.uniqueUsername);
  18. }
  19. submit = () => {
  20. this.formRef.current.submit();
  21. }
  22. render() {
  23. const { user, creating, handleValueChange, onDoneEditing, onCancelEditing } = this.props;
  24. return (
  25. <ValidatorForm onSubmit={onDoneEditing} ref={this.formRef}>
  26. <Dialog onClose={onCancelEditing} aria-labelledby="user-form-dialog-title" open>
  27. <DialogTitle id="user-form-dialog-title">{creating ? 'Add' : 'Modify'} User</DialogTitle>
  28. <DialogContent dividers>
  29. <TextValidator
  30. validators={creating ? ['required', 'uniqueUsername', 'matchRegexp:^[a-zA-Z0-9_\\.]{1,24}$'] : []}
  31. errorMessages={creating ? ['Username is required', "Username already exists", "Must be 1-24 characters: alpha numeric, '_' or '.'"] : []}
  32. name="username"
  33. label="Username"
  34. fullWidth
  35. variant="outlined"
  36. value={user.username}
  37. disabled={!creating}
  38. onChange={handleValueChange('username')}
  39. margin="normal"
  40. />
  41. <PasswordValidator
  42. validators={['required', 'matchRegexp:^.{1,64}$']}
  43. errorMessages={['Password is required', 'Password must be 64 characters or less']}
  44. name="password"
  45. label="Password"
  46. fullWidth
  47. variant="outlined"
  48. value={user.password}
  49. onChange={handleValueChange('password')}
  50. margin="normal"
  51. />
  52. <BlockFormControlLabel
  53. control={
  54. <Checkbox
  55. value="admin"
  56. checked={user.admin}
  57. onChange={handleValueChange('admin')}
  58. />
  59. }
  60. label="Admin?"
  61. />
  62. </DialogContent>
  63. <DialogActions>
  64. <FormButton variant="contained" color="primary" type="submit" onClick={this.submit}>
  65. Done
  66. </FormButton>
  67. <FormButton variant="contained" color="secondary" onClick={onCancelEditing}>
  68. Cancel
  69. </FormButton>
  70. </DialogActions>
  71. </Dialog>
  72. </ValidatorForm>
  73. );
  74. }
  75. }
  76. export default UserForm;