[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.

61 lines
2.0 KiB

5 years ago
5 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 { useTheme } from '../../hooks'
  5. import { setFieldValue } from '../../actions/forms'
  6. import { getFieldValue, getFieldNotification } from '../../selectors/forms'
  7. import { AppState, FormNotification, NotificationType } from '../../types'
  8. import FieldLabel from '../../components/controls/field-label'
  9. import FormNotificationComponent from '../../components/form-notification'
  10. export interface Props {
  11. name: string
  12. label: string
  13. placeholder?: string
  14. onBlur?: FocusEventHandler
  15. }
  16. const TextField: FC<Props> = ({
  17. name,
  18. label,
  19. placeholder,
  20. onBlur = noop,
  21. }) => {
  22. const theme = useTheme()
  23. const value = useSelector<AppState, string>(state => getFieldValue<string>(state, name, ''))
  24. const notification = useSelector<AppState, FormNotification | undefined>(state => getFieldNotification(state, name))
  25. const dispatch = useDispatch()
  26. let color = theme.secondary
  27. if (notification) {
  28. switch (notification.type) {
  29. case NotificationType.Error:
  30. color = theme.red
  31. break
  32. case NotificationType.Success:
  33. color = theme.green
  34. break
  35. }
  36. }
  37. return (
  38. <div className="field">
  39. <FieldLabel>{label}</FieldLabel>
  40. <div className="control-container">
  41. <div className="control">
  42. <textarea
  43. style={{ backgroundColor: theme.backgroundSecondary, borderColor: color, color: theme.text }}
  44. placeholder={placeholder}
  45. value={value}
  46. onChange={e => dispatch(setFieldValue(name, e.target.value))}
  47. onBlur={onBlur} />
  48. </div>
  49. </div>
  50. {notification && <FormNotificationComponent notification={notification} />}
  51. </div>
  52. )
  53. }
  54. export default TextField