settings-menu-select.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import BootstrapVersionSwitcher from '@/features/ui/components/bootstrap-5/bootstrap-version-switcher'
  2. import OLFormGroup from '@/features/ui/components/ol/ol-form-group'
  3. import OLFormLabel from '@/features/ui/components/ol/ol-form-label'
  4. import OLFormSelect from '@/features/ui/components/ol/ol-form-select'
  5. import { ChangeEventHandler, useCallback } from 'react'
  6. import { Spinner } from 'react-bootstrap-5'
  7. type PossibleValue = string | number | boolean
  8. export type Option<T extends PossibleValue = string> = {
  9. value: T
  10. label: string
  11. ariaHidden?: 'true' | 'false'
  12. disabled?: boolean
  13. }
  14. export type Optgroup<T extends PossibleValue = string> = {
  15. label: string
  16. options: Array<Option<T>>
  17. }
  18. type SettingsMenuSelectProps<T extends PossibleValue = string> = {
  19. label: string
  20. name: string
  21. options: Array<Option<T>>
  22. optgroup?: Optgroup<T>
  23. loading?: boolean
  24. onChange: (val: T) => void
  25. value?: T
  26. disabled?: boolean
  27. }
  28. export default function SettingsMenuSelect<T extends PossibleValue = string>({
  29. label,
  30. name,
  31. options,
  32. optgroup,
  33. loading,
  34. onChange,
  35. value,
  36. disabled = false,
  37. }: SettingsMenuSelectProps<T>) {
  38. const handleChange: ChangeEventHandler<HTMLSelectElement> = useCallback(
  39. event => {
  40. const selectedValue = event.target.value
  41. let onChangeValue: PossibleValue = selectedValue
  42. if (typeof value === 'boolean') {
  43. onChangeValue = selectedValue === 'true'
  44. } else if (typeof value === 'number') {
  45. onChangeValue = parseInt(selectedValue, 10)
  46. }
  47. onChange(onChangeValue as T)
  48. },
  49. [onChange, value]
  50. )
  51. return (
  52. <OLFormGroup
  53. controlId={`settings-menu-${name}`}
  54. className="left-menu-setting"
  55. >
  56. <OLFormLabel>{label}</OLFormLabel>
  57. {loading ? (
  58. <BootstrapVersionSwitcher
  59. bs3={
  60. <p className="loading pull-right">
  61. <i className="fa fa-fw fa-spin fa-refresh" />
  62. </p>
  63. }
  64. bs5={
  65. <p className="mb-0">
  66. <Spinner
  67. animation="border"
  68. aria-hidden="true"
  69. size="sm"
  70. role="status"
  71. />
  72. </p>
  73. }
  74. />
  75. ) : (
  76. <OLFormSelect
  77. size="sm"
  78. onChange={handleChange}
  79. value={value?.toString()}
  80. disabled={disabled}
  81. >
  82. {options.map(option => (
  83. <option
  84. key={`${name}-${option.value}`}
  85. value={option.value.toString()}
  86. aria-hidden={option.ariaHidden}
  87. disabled={option.disabled}
  88. >
  89. {option.label}
  90. </option>
  91. ))}
  92. {optgroup ? (
  93. <optgroup label={optgroup.label}>
  94. {optgroup.options.map(option => (
  95. <option
  96. value={option.value.toString()}
  97. key={option.value.toString()}
  98. >
  99. {option.label}
  100. </option>
  101. ))}
  102. </optgroup>
  103. ) : null}
  104. </OLFormSelect>
  105. )}
  106. </OLFormGroup>
  107. )
  108. }