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

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