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.

84 lines
2.6 KiB

  1. import React from 'react';
  2. import { TextValidator, ValidatorForm, SelectValidator } from 'react-material-ui-form-validator';
  3. import { Checkbox, MenuItem } from '@material-ui/core';
  4. import SaveIcon from '@material-ui/icons/Save';
  5. import { RestFormProps, FormActions, FormButton, BlockFormControlLabel } from '../components';
  6. import { isIP, isHostname, or } from '../validators';
  7. import { TIME_ZONES, timeZoneSelectItems, selectedTimeZone } from './TZ';
  8. import { NTPSettings } from './types';
  9. type NTPSettingsFormProps = RestFormProps<NTPSettings>;
  10. class NTPSettingsForm extends React.Component<NTPSettingsFormProps> {
  11. componentDidMount() {
  12. ValidatorForm.addValidationRule('isIPOrHostname', or(isIP, isHostname));
  13. }
  14. changeTimeZone = (event: React.ChangeEvent<HTMLSelectElement>) => {
  15. const { data, setData } = this.props;
  16. setData({
  17. ...data,
  18. tz_label: event.target.value,
  19. tz_format: TIME_ZONES[event.target.value]
  20. });
  21. }
  22. render() {
  23. const { data, handleValueChange, handleCheckboxChange, saveData, loadData } = this.props;
  24. return (
  25. <ValidatorForm onSubmit={saveData}>
  26. <BlockFormControlLabel
  27. control={
  28. <Checkbox
  29. checked={data.enabled}
  30. onChange={handleCheckboxChange('enabled')}
  31. value="enabled"
  32. />
  33. }
  34. label="Enable NTP?"
  35. />
  36. <TextValidator
  37. validators={['required', 'isIPOrHostname']}
  38. errorMessages={['Server is required', "Not a valid IP address or hostname"]}
  39. name="server"
  40. label="Server"
  41. fullWidth
  42. variant="outlined"
  43. value={data.server}
  44. onChange={handleValueChange('server')}
  45. margin="normal"
  46. />
  47. <SelectValidator
  48. validators={['required']}
  49. errorMessages={['Time zone is required']}
  50. name="tz_label"
  51. labelId="tz_label"
  52. label="Time zone"
  53. fullWidth
  54. variant="outlined"
  55. native
  56. value={selectedTimeZone(data.tz_label, data.tz_format)}
  57. onChange={this.changeTimeZone}
  58. margin="normal"
  59. >
  60. <MenuItem disabled>Time zone...</MenuItem>
  61. {timeZoneSelectItems()}
  62. </SelectValidator>
  63. <FormActions>
  64. <FormButton startIcon={<SaveIcon />} variant="contained" color="primary" type="submit">
  65. Save
  66. </FormButton>
  67. <FormButton variant="contained" color="secondary" onClick={loadData}>
  68. Reset
  69. </FormButton>
  70. </FormActions>
  71. </ValidatorForm>
  72. );
  73. }
  74. }
  75. export default NTPSettingsForm;