Form.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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, { useImperativeHandle, forwardRef } from 'react'
  21. import debounce from 'lodash/debounce'
  22. import { Form } from 'antd'
  23. import { FormComponentProps } from 'antd/lib/form'
  24. import ItemGroup from './ItemGroup'
  25. import { SlideLayerSetting } from './types'
  26. import { SlideSettingContext } from './util'
  27. import './Form.less'
  28. interface ISettingFormProps extends FormComponentProps {
  29. setting: SlideLayerSetting
  30. slideId: number
  31. layerId?: number
  32. onChange: (
  33. changedValues: { [name: string]: number | string },
  34. layerId?: number
  35. ) => void
  36. }
  37. const SettingForm: React.FC<ISettingFormProps> = (props, ref) => {
  38. const { form, setting, slideId, layerId } = props
  39. useImperativeHandle(ref, () => form)
  40. return (
  41. <Form className="display-setting-form" labelAlign="left">
  42. <SlideSettingContext.Provider
  43. value={{ form, slideId, layerId, size: 'small' }}
  44. >
  45. {setting.params.map((param) => (
  46. <ItemGroup key={param.name} param={param} />
  47. ))}
  48. </SlideSettingContext.Provider>
  49. </Form>
  50. )
  51. }
  52. let cachedValues = {}
  53. export default Form.create<ISettingFormProps>({
  54. onValuesChange: (props, changedValues, allValues) => {
  55. if (Object.keys(changedValues).length > 1) {
  56. return
  57. }
  58. cachedValues = { ...cachedValues, ...allValues, ...changedValues }
  59. const { onChange, layerId } = props
  60. const debouncedChange = debounce((layerId) => {
  61. onChange({ ...cachedValues }, layerId)
  62. cachedValues = {}
  63. }, 1000)
  64. debouncedChange(layerId)
  65. }
  66. })(forwardRef(SettingForm))