[ABANDONED] React/Redux front end for the Flexor social network.
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.

77 lines
2.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
  1. import React, { FC, FocusEventHandler } from 'react'
  2. import { useSelector, useDispatch } from 'react-redux'
  3. import noop from 'lodash/noop'
  4. import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
  5. import { IconDefinition } from '@fortawesome/fontawesome-common-types'
  6. import { useTheme } from '../../hooks'
  7. import { setFieldValue } from '../../actions/forms'
  8. import { getFieldValue, getFieldNotification } from '../../selectors/forms'
  9. import { AppState, FormNotification, NotificationType } from '../../types'
  10. import FieldLabel from '../../components/controls/field-label'
  11. import FormNotificationComponent from '../../components/form-notification'
  12. import HelpText from '../../components/help-text'
  13. interface Props {
  14. name: string
  15. label: string
  16. type?: 'text' | 'email'
  17. placeholder?: string
  18. help?: string
  19. icon?: IconDefinition
  20. onBlur?: FocusEventHandler<HTMLInputElement>
  21. }
  22. const TextField: FC<Props> = ({
  23. name,
  24. label,
  25. type = 'text',
  26. placeholder,
  27. help,
  28. icon,
  29. onBlur = noop,
  30. }) => {
  31. const theme = useTheme()
  32. const value = useSelector<AppState, string>(state => getFieldValue<string>(state, name, ''))
  33. const notification = useSelector<AppState, FormNotification | undefined>(state => getFieldNotification(state, name))
  34. const dispatch = useDispatch()
  35. let color = theme.primary
  36. if (notification) {
  37. switch (notification.type) {
  38. case NotificationType.Error:
  39. color = theme.red
  40. break
  41. case NotificationType.Success:
  42. color = theme.green
  43. break
  44. }
  45. }
  46. return (
  47. <div className="field">
  48. <FieldLabel>{label}</FieldLabel>
  49. <div className="control-container">
  50. {icon &&
  51. <div className="control-icon" style={{ backgroundColor: color, color: theme.primaryAlternate }}>
  52. <FontAwesomeIcon icon={icon} />
  53. </div>
  54. }
  55. <div className="control">
  56. <input
  57. style={{ backgroundColor: theme.backgroundSecondary, borderColor: color, color: theme.text }}
  58. type={type}
  59. placeholder={placeholder}
  60. value={value}
  61. onChange={e => dispatch(setFieldValue(name, e.target.value))}
  62. onBlur={onBlur} />
  63. </div>
  64. </div>
  65. {(!notification && help) && <HelpText>{help}</HelpText>}
  66. {notification && <FormNotificationComponent notification={notification} />}
  67. </div>
  68. )
  69. }
  70. export default TextField