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

60 lines
2.1 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import React, { FC } from 'react'
  2. import { useSelector, useDispatch } from 'react-redux'
  3. import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
  4. import { IconDefinition } from '@fortawesome/fontawesome-svg-core'
  5. import { useTheme } from '../../hooks'
  6. import { setFieldValue } from '../../actions/forms'
  7. import { getFieldValue, getFieldNotification } from '../../selectors/forms'
  8. import { AppState, FormNotification } from '../../types'
  9. import FieldLabel from '../../components/controls/field-label'
  10. import FormNotificationComponent from '../../components/form-notification'
  11. interface SelectOptions {
  12. [value: string]: string
  13. }
  14. interface Props {
  15. name: string
  16. label: string
  17. options: SelectOptions
  18. icon?: IconDefinition
  19. }
  20. const SelectField: FC<Props> = ({
  21. name,
  22. label,
  23. options,
  24. icon,
  25. }) => {
  26. const theme = useTheme()
  27. const value = useSelector<AppState, string>(state => getFieldValue<string>(state, name, ''))
  28. const notification = useSelector<AppState, FormNotification | undefined>(state => getFieldNotification(state, name))
  29. const dispatch = useDispatch()
  30. const opts = Object.entries(options)
  31. return (
  32. <div className="field">
  33. <FieldLabel>{label}</FieldLabel>
  34. <div className="control-container">
  35. {icon &&
  36. <div className="control-icon" style={{ backgroundColor: theme.primary, color: theme.primaryAlternate }}>
  37. <FontAwesomeIcon icon={icon} />
  38. </div>
  39. }
  40. <div className="control">
  41. <select
  42. style={{ backgroundColor: theme.backgroundSecondary, borderColor: theme.secondary, color: theme.text }}
  43. value={value}
  44. onChange={e => dispatch(setFieldValue(name, e.target.value))}>
  45. {opts.map(([key, value]) => <option key={key} value={key}>{value}</option>)}
  46. </select>
  47. </div>
  48. </div>
  49. {notification && <FormNotificationComponent notification={notification} />}
  50. </div>
  51. )
  52. }
  53. export default SelectField