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

93 lines
3.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
  1. // text-field.tsx
  2. // Copyright (C) 2020 Dwayne Harris
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. import React, { FC, FocusEventHandler } from 'react'
  14. import { useSelector, useDispatch } from 'react-redux'
  15. import noop from 'lodash/noop'
  16. import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
  17. import { IconDefinition } from '@fortawesome/fontawesome-common-types'
  18. import { useTheme } from '../../hooks'
  19. import { setFieldValue } from '../../actions/forms'
  20. import { getFieldValue, getFieldNotification } from '../../selectors/forms'
  21. import { AppState, FormNotification, NotificationType } from '../../types'
  22. import FieldLabel from '../../components/controls/field-label'
  23. import FormNotificationComponent from '../../components/form-notification'
  24. import HelpText from '../../components/help-text'
  25. interface Props {
  26. name: string
  27. label: string
  28. type?: 'text' | 'email'
  29. placeholder?: string
  30. help?: string
  31. icon?: IconDefinition
  32. onBlur?: FocusEventHandler<HTMLInputElement>
  33. }
  34. const TextField: FC<Props> = ({
  35. name,
  36. label,
  37. type = 'text',
  38. placeholder,
  39. help,
  40. icon,
  41. onBlur = noop,
  42. }) => {
  43. const theme = useTheme()
  44. const value = useSelector<AppState, string>(state => getFieldValue<string>(state, name, ''))
  45. const notification = useSelector<AppState, FormNotification | undefined>(state => getFieldNotification(state, name))
  46. const dispatch = useDispatch()
  47. let color = theme.primary
  48. if (notification) {
  49. switch (notification.type) {
  50. case NotificationType.Error:
  51. color = theme.red
  52. break
  53. case NotificationType.Success:
  54. color = theme.green
  55. break
  56. }
  57. }
  58. return (
  59. <div className="field">
  60. <FieldLabel>{label}</FieldLabel>
  61. <div className="control-container">
  62. {icon &&
  63. <div className="control-icon" style={{ backgroundColor: color, color: theme.primaryAlternate }}>
  64. <FontAwesomeIcon icon={icon} />
  65. </div>
  66. }
  67. <div className="control">
  68. <input
  69. style={{ backgroundColor: theme.backgroundSecondary, borderColor: color, color: theme.text }}
  70. type={type}
  71. placeholder={placeholder}
  72. value={value}
  73. onChange={e => dispatch(setFieldValue(name, e.target.value))}
  74. onBlur={onBlur} />
  75. </div>
  76. </div>
  77. {(!notification && help) && <HelpText>{help}</HelpText>}
  78. {notification && <FormNotificationComponent notification={notification} />}
  79. </div>
  80. )
  81. }
  82. export default TextField