BlockInputs.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import FormatSelector from './FormatSelector'
  2. import { TextInput } from '../../form'
  3. import BlockInstanceInputs from './BlockInstanceInputs'
  4. import { emptyBlockInstance } from '../utils'
  5. import ExerciseInstanceInputs from './ExerciseInstanceInputs'
  6. import { TBlock } from '../types'
  7. interface IBlockInputs {
  8. onChange: GenericEventHandler
  9. value: TBlock
  10. name: string
  11. }
  12. const BlockInputs = ({ onChange, value, name }: IBlockInputs) => {
  13. return (
  14. <>
  15. <TextInput
  16. name={`${name}.title`}
  17. label='Title'
  18. value={value.title}
  19. onChange={onChange}
  20. />
  21. <TextInput
  22. name={`${name}.description`}
  23. label='Description'
  24. value={value.description ?? ''}
  25. onChange={onChange}
  26. />
  27. <TextInput
  28. name={`${name}.duration`}
  29. label='Duration'
  30. value={value.duration ?? ''}
  31. type='number'
  32. onChange={onChange}
  33. />
  34. <FormatSelector
  35. name={`${name}.format`}
  36. value={value.format}
  37. onChange={onChange}
  38. />
  39. <TextInput
  40. name={`${name}.rest`}
  41. label='Rest'
  42. value={value.rest ?? ''}
  43. type='number'
  44. onChange={onChange}
  45. />
  46. <label>Blocks</label>
  47. {value.blocks && value.blocks.length > 0 && (
  48. <BlockInstanceInputs
  49. name={`${name}.blocks`}
  50. value={value.blocks}
  51. onChange={onChange}
  52. />
  53. )}
  54. <button
  55. onClick={event => {
  56. event.preventDefault()
  57. const newBlock = emptyBlockInstance({
  58. order: value.blocks ? value.blocks.length : 0
  59. })
  60. onChange({
  61. target: {
  62. type: 'custom',
  63. name: `${name}.blocks`,
  64. value: value.blocks ? [...value.blocks, newBlock] : [newBlock]
  65. }
  66. })
  67. }}
  68. type='button'
  69. >
  70. Add block
  71. </button>
  72. <label>Exercises</label>
  73. {value.exercises && value.exercises.length > 0 && (
  74. <ExerciseInstanceInputs
  75. name={`${name}.blocks`}
  76. value={value.exercises}
  77. onChange={onChange}
  78. />
  79. )}
  80. <button
  81. onClick={event => {
  82. event.preventDefault()
  83. const newExercise = empty({
  84. order: value.blocks ? value.blocks.length : 0
  85. })
  86. onChange({
  87. target: {
  88. type: 'custom',
  89. name: `${name}.blocks`,
  90. value: value.blocks ? [...value.blocks, newBlock] : [newBlock]
  91. }
  92. })
  93. }}
  94. type='button'
  95. >
  96. Add exercise
  97. </button>
  98. </>
  99. )
  100. }
  101. export default BlockInputs