1
0

Field.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright 2021-2024 Avaiga Private Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  5. * the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  10. * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  11. * specific language governing permissions and limitations under the License.
  12. */
  13. import React, { lazy, useMemo } from "react";
  14. import Typography from "@mui/material/Typography";
  15. import Tooltip from "@mui/material/Tooltip";
  16. import { formatWSValue } from "../../utils";
  17. import { useClassNames, useDynamicProperty, useFormatConfig } from "../../utils/hooks";
  18. import { TaipyBaseProps, TaipyHoverProps, getCssSize } from "./utils";
  19. interface TaipyFieldProps extends TaipyBaseProps, TaipyHoverProps {
  20. dataType?: string;
  21. value: string | number;
  22. defaultValue?: string;
  23. format?: string;
  24. raw?: boolean;
  25. mode?: string;
  26. width?: string | number;
  27. }
  28. const unsetWeightSx = { fontWeight: "unset" };
  29. const Markdown = lazy(() => import("react-markdown"));
  30. const Field = (props: TaipyFieldProps) => {
  31. const { id, dataType, format, defaultValue, raw } = props;
  32. const formatConfig = useFormatConfig();
  33. const className = useClassNames(props.libClassName, props.dynamicClassName, props.className);
  34. const hover = useDynamicProperty(props.hoverText, props.defaultHoverText, undefined);
  35. const mode = typeof props.mode === "string" ? props.mode.toLowerCase() : undefined;
  36. const style = useMemo(
  37. () => ({ overflow: "auto", width: props.width ? getCssSize(props.width) : undefined }),
  38. [props.width]
  39. );
  40. const typoSx = useMemo(
  41. () =>
  42. props.width
  43. ? { ...unsetWeightSx, overflow: "auto", width: getCssSize(props.width), display: "inline-block" }
  44. : unsetWeightSx,
  45. [props.width]
  46. );
  47. const value = useMemo(() => {
  48. return formatWSValue(
  49. props.value !== undefined ? props.value : defaultValue || "",
  50. dataType,
  51. format,
  52. formatConfig
  53. );
  54. }, [defaultValue, props.value, dataType, format, formatConfig]);
  55. return (
  56. <Tooltip title={hover || ""}>
  57. {mode == "pre" ? (
  58. <pre className={className} id={id} style={style}>
  59. {value}
  60. </pre>
  61. ) : mode == "markdown" || mode == "md" ? (
  62. <Markdown className={className}>{value}</Markdown>
  63. ) : raw || mode == "raw" ? (
  64. <span className={className} id={id} style={style}>
  65. {value}
  66. </span>
  67. ) : (
  68. <Typography className={className} id={id} component="span" sx={typoSx}>
  69. {value}
  70. </Typography>
  71. )}
  72. </Tooltip>
  73. );
  74. };
  75. export default Field;