index.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import React from 'react';
  2. import OuterRect from '../OuterRect';
  3. import { rectCalc, parsePositionForBackend } from '../../helpers/position';
  4. import { AnnotationContainer } from '../../global/otherStyled';
  5. import { TextBox } from './styled';
  6. const TextField: React.FC<AnnotationElementPropsType> = ({
  7. id,
  8. obj_type,
  9. obj_attr: { position, style },
  10. scale,
  11. viewport,
  12. isCollapse,
  13. onUpdate,
  14. }: AnnotationElementPropsType) => {
  15. const annotRect = rectCalc(position as PositionType, viewport.height, scale);
  16. const handleScaleOrMove = ({
  17. top,
  18. left,
  19. width = 0,
  20. height = 0,
  21. }: CoordType): void => {
  22. const newPosition = {
  23. top,
  24. left,
  25. bottom: top + (height || annotRect.height),
  26. right: left + (width || annotRect.width),
  27. };
  28. onUpdate({
  29. fontsize: (height || annotRect.height) / scale,
  30. position: parsePositionForBackend(
  31. obj_type,
  32. newPosition,
  33. viewport.height,
  34. scale,
  35. ),
  36. });
  37. };
  38. let formType = obj_type;
  39. if (obj_type === 'checkbox' && style === '1') {
  40. formType = 'radio';
  41. }
  42. return (
  43. <>
  44. <AnnotationContainer
  45. id={id}
  46. top={`${annotRect.top}px`}
  47. left={`${annotRect.left}px`}
  48. width={`${annotRect.width}px`}
  49. height={`${annotRect.height}px`}
  50. >
  51. <TextBox type={formType}>
  52. {obj_type === 'textfield' ? '輸入框' : ''}
  53. </TextBox>
  54. </AnnotationContainer>
  55. {!isCollapse ? (
  56. <OuterRect
  57. top={annotRect.top}
  58. left={annotRect.left}
  59. width={annotRect.width}
  60. height={annotRect.height}
  61. onMove={handleScaleOrMove}
  62. onScale={handleScaleOrMove}
  63. />
  64. ) : (
  65. ''
  66. )}
  67. </>
  68. );
  69. };
  70. export default TextField;