LayerSettingForm.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, { useState, useEffect, useCallback } from 'react'
  21. import { WrappedFormUtils } from 'antd/lib/form/Form'
  22. import { SlideLayerSettingForm } from './Form'
  23. import {
  24. slideSettings,
  25. GraphTypes,
  26. SecondaryGraphTypes
  27. } from './Form/constants'
  28. import { ILayerParams } from '../types'
  29. interface ILayerSettingFormProps {
  30. type: GraphTypes | SecondaryGraphTypes
  31. slideId: number
  32. layerId: number
  33. layerParams: ILayerParams
  34. onChange: (layerId: number, changedParams: Partial<ILayerParams>) => void
  35. }
  36. const LayerSettingForm: React.FC<ILayerSettingFormProps> = (props) => {
  37. const { type, slideId, layerId, layerParams, onChange } = props
  38. const layerSetting = slideSettings[type]
  39. const refForm = React.useRef<WrappedFormUtils>(null)
  40. const [lastLayerId, setLastLayerId] = useState<number>(null)
  41. useEffect(() => {
  42. if (refForm.current && layerId !== lastLayerId) {
  43. refForm.current.setFieldsValue(layerParams)
  44. setLastLayerId(layerId)
  45. }
  46. }, [layerParams, layerId])
  47. const change = (changedValues, layerId) => {
  48. onChange(layerId, changedValues)
  49. }
  50. return (
  51. <SlideLayerSettingForm
  52. wrappedComponentRef={refForm}
  53. setting={layerSetting}
  54. slideId={slideId}
  55. layerId={layerId}
  56. onChange={change}
  57. />
  58. )
  59. }
  60. export default React.memo(LayerSettingForm)