LayerRadio.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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, { useCallback } from 'react'
  21. import classnames from 'classnames'
  22. import { Tooltip } from 'antd'
  23. import 'antd/lib/radio/style/index.less'
  24. import './LayerList.less'
  25. interface ILayerRadioProps {
  26. id: number
  27. checked: boolean
  28. onChange: (layerId: number, checked: boolean, exclusive: boolean) => void
  29. }
  30. export const LayerRadio: React.FC<ILayerRadioProps> = React.memo((props) => {
  31. const { id, checked, onChange } = props
  32. const wrapperCls = classnames({
  33. 'display-layer-radio': true,
  34. 'ant-radio-wrapper-checked': checked
  35. })
  36. const radioCls = classnames({
  37. 'ant-radio': true,
  38. 'ant-radio-checked': checked
  39. })
  40. const change = useCallback(
  41. (e: React.MouseEvent<HTMLLabelElement>) => {
  42. e.preventDefault()
  43. const { altKey, metaKey } = e
  44. const exclusive = !altKey && !metaKey
  45. onChange(id, !checked, exclusive)
  46. },
  47. [id, checked, onChange]
  48. )
  49. return (
  50. <label className={wrapperCls} onClick={change}>
  51. <span className={radioCls}>
  52. <input
  53. type="radio"
  54. className="ant-radio-input"
  55. defaultChecked={checked}
  56. />
  57. <span className="ant-radio-inner" />
  58. </span>
  59. <Tooltip title={props.children} placement="right">
  60. <span>{props.children}</span>
  61. </Tooltip>
  62. </label>
  63. )
  64. })
  65. export const LayerRadioGroup: React.FC = React.memo((props) => (
  66. <div className="display-layer-radio-group ant-radio-group-outline">
  67. {props.children}
  68. </div>
  69. ))