utils.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 { MouseEvent } from "react";
  14. import { SxProps } from "@mui/material";
  15. export interface TaipyActiveProps extends TaipyDynamicProps, TaipyHoverProps {
  16. defaultActive?: boolean;
  17. active?: boolean;
  18. }
  19. export interface TaipyHoverProps {
  20. hoverText?: string;
  21. defaultHoverText?: string;
  22. }
  23. interface TaipyDynamicProps extends TaipyBaseProps {
  24. updateVarName?: string;
  25. propagate?: boolean;
  26. updateVars?: string;
  27. }
  28. export interface TaipyBaseProps {
  29. id?: string;
  30. libClassName?: string;
  31. className?: string;
  32. dynamicClassName?: string;
  33. }
  34. export interface TaipyMultiSelectProps {
  35. selected?: number[];
  36. }
  37. export interface TaipyChangeProps {
  38. onChange?: string;
  39. }
  40. export interface TaipyInputProps extends TaipyActiveProps, TaipyChangeProps, TaipyLabelProps {
  41. type: string;
  42. value: string;
  43. defaultValue?: string;
  44. step?: number;
  45. defaultStep?: number;
  46. stepMultiplier?: number;
  47. defaultStepMultiplier?: number;
  48. min?: number;
  49. defaultMin?: number;
  50. max?: number;
  51. defaultMax?: number;
  52. changeDelay?: number;
  53. onAction?: string;
  54. actionKeys?: string;
  55. multiline?: boolean;
  56. linesShown?: number;
  57. width?: string | number;
  58. }
  59. export interface TaipyLabelProps {
  60. label?: string;
  61. }
  62. export interface DateProps {
  63. maxDate?: unknown;
  64. maxDateTime?: unknown;
  65. maxTime?: unknown;
  66. minDate?: unknown;
  67. minDateTime?: unknown;
  68. minTime?: unknown;
  69. }
  70. export const getArrayValue = <T>(arr: T[], idx: number, defVal?: T): T | undefined => {
  71. const val = Array.isArray(arr) && idx < arr.length ? arr[idx] : undefined;
  72. return val ?? defVal;
  73. };
  74. /**
  75. * Extracts the backend name of a property.
  76. *
  77. * @param updateVars - The value held by the property *updateVars*.
  78. * @param name - The name of a bound property.
  79. * @returns The backend-generated variable name.
  80. */
  81. export const getUpdateVar = (updateVars: string, name: string) => {
  82. const sel = updateVars && updateVars.split(";").find((uv) => uv && uv.startsWith(name + "="));
  83. if (sel) {
  84. return sel.substring(name.length + 1);
  85. }
  86. return sel;
  87. };
  88. export const getUpdateVars = (updateVars?: string) =>
  89. updateVars
  90. ? updateVars
  91. .split(";")
  92. .filter((uv) => uv && uv.indexOf("=") > -1)
  93. .map((uv) => uv.split("=")[1].trim())
  94. : [];
  95. export const doNotPropagateEvent = (event: MouseEvent) => event.stopPropagation();
  96. export const noDisplayStyle = { display: "none" };
  97. const RE_ONLY_NUMBERS = /^\d+(\.\d*)?$/;
  98. export const getCssSize = (val: string | number) => {
  99. if (typeof val === "number") {
  100. return "" + val + "px";
  101. } else {
  102. val = val.trim();
  103. if (RE_ONLY_NUMBERS.test(val)) {
  104. return val + "px";
  105. }
  106. }
  107. return val;
  108. };
  109. export const getSuffixedClassNames = (names: string | undefined, suffix: string) =>
  110. (names || "")
  111. .split(/\s+/)
  112. .map((n) => n + suffix)
  113. .join(" ");
  114. export const disableColor = <T>(color: T, disabled: boolean) => (disabled ? ("disabled" as T) : color);
  115. export const getProps = (p: DateProps, start: boolean, val: Date | null, withTime: boolean): DateProps => {
  116. if (!val) {
  117. return {};
  118. }
  119. const propName: keyof DateProps = withTime
  120. ? start
  121. ? "minDateTime"
  122. : "maxDateTime"
  123. : start
  124. ? "minDate"
  125. : "maxDate";
  126. if (p[propName] == val) {
  127. return p;
  128. }
  129. return { ...p, [propName]: val };
  130. };
  131. export const expandSx = (sx: SxProps | undefined, ...partials: (SxProps | undefined)[]) => {
  132. const start = sx || {};
  133. const end = partials.reduce((prevSx: SxProps, partialSx) => {
  134. if (partialSx) {
  135. return { ...prevSx, ...partialSx } as SxProps;
  136. }
  137. return prevSx;
  138. }, start);
  139. return start === end ? sx : end;
  140. };