Setting.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import React, { useState, useCallback, forwardRef } from 'react'
  2. import { IFontSetting, FontStyles } from './types'
  3. import { fontSelect, EmptyFontSetting } from './constants'
  4. import { SelectProps } from 'antd/lib/select'
  5. import { ColorPickerProps } from 'components/ColorPicker'
  6. import Styles from './Font.less'
  7. interface IFontSettingProps {
  8. value?: IFontSetting
  9. size?: SelectProps['size']
  10. pathPrefix?: string
  11. onChange?: (setting: IFontSetting, pathPrefix?: string) => void
  12. }
  13. const FontSetting: React.FC<IFontSettingProps> = (props, ref) => {
  14. const { value, size: containerSize, pathPrefix, onChange } = props
  15. const { fontFamily, fontStyle, fontSize, fontWeight, fontColor } = value || EmptyFontSetting
  16. const [family, setFamily] = useState(fontFamily)
  17. const [style, setStyle] = useState(fontStyle)
  18. const [size, setSize] = useState(fontSize)
  19. const [weight, setWeight] = useState(fontWeight)
  20. const [color, setColor] = useState(fontColor)
  21. const triggerChange = useCallback(
  22. (changedValue: Partial<IFontSetting>) => {
  23. if (onChange) {
  24. onChange(
  25. { fontFamily, fontStyle, fontSize, fontWeight, fontColor, ...changedValue },
  26. pathPrefix
  27. )
  28. }
  29. },
  30. [onChange, pathPrefix, value]
  31. )
  32. const fontFamilySelect = React.cloneElement<SelectProps<string>>(
  33. fontSelect.family,
  34. {
  35. value: family,
  36. size: containerSize,
  37. className: Styles.width1,
  38. onChange: (value) => {
  39. setFamily(value)
  40. triggerChange({ fontFamily: value })
  41. }
  42. }
  43. )
  44. const fontStyleSelect = React.cloneElement<SelectProps<string>>(
  45. fontSelect.style,
  46. {
  47. value: style,
  48. size: containerSize,
  49. className: Styles.width2,
  50. onChange: (value: FontStyles) => {
  51. setStyle(value)
  52. triggerChange({ fontStyle: value })
  53. }
  54. }
  55. )
  56. const fontSizeSelect = React.cloneElement<SelectProps<string>>(
  57. fontSelect.size,
  58. {
  59. value: size,
  60. size: containerSize,
  61. className: Styles.width2,
  62. onChange: (value) => {
  63. setSize(value)
  64. triggerChange({ fontSize: value })
  65. }
  66. }
  67. )
  68. const fontWeightSelect = React.cloneElement<SelectProps<string>>(
  69. fontSelect.weight,
  70. {
  71. value: weight,
  72. size: containerSize,
  73. className: Styles.width3,
  74. onChange: (value) => {
  75. setWeight(value)
  76. triggerChange({ fontWeight: value })
  77. }
  78. }
  79. )
  80. const fontColorPicker = React.cloneElement<ColorPickerProps>(
  81. fontSelect.color,
  82. {
  83. value: color,
  84. size: containerSize,
  85. onChange: (value: string) => {
  86. setColor(value)
  87. triggerChange({ fontColor: value })
  88. }
  89. }
  90. )
  91. return (
  92. <div className={Styles.setting} ref={ref}>
  93. {fontFamilySelect}
  94. {fontStyleSelect}
  95. {fontSizeSelect}
  96. {fontWeightSelect}
  97. <div className={Styles.colorWrapper}>{fontColorPicker}</div>
  98. </div>
  99. )
  100. }
  101. export default React.memo(forwardRef(FontSetting))