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.

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