index.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import {
  2. type FC
  3. } from "react";
  4. import {
  5. View
  6. } from "react-native";
  7. import type INotificationIndicatorProps from "./type";
  8. import stylesheet, {
  9. getNotificationIndicatorType,
  10. useStyles
  11. } from "./stylesheet";
  12. import {
  13. NCoreUIKitTheme
  14. } from "../../core/hooks";
  15. import type ITextProps from "../text/type";
  16. import Loading from "../loading";
  17. import Text from "../text";
  18. const NotificationIndicator: FC<INotificationIndicatorProps> = ({
  19. floorBackgroundColor = "default",
  20. titleVariant = "labelSmallSize",
  21. spreadBehaviour = "baseline",
  22. isDisabled = false,
  23. type = "neutral",
  24. customTheme,
  25. titleStyle,
  26. isLoading,
  27. location,
  28. children,
  29. title,
  30. style,
  31. ...props
  32. }) => {
  33. const {
  34. typography,
  35. radiuses,
  36. borders,
  37. colors,
  38. spaces
  39. } = NCoreUIKitTheme.useContext(customTheme);
  40. const currentType = getNotificationIndicatorType({
  41. type
  42. });
  43. const {
  44. indicatorContainer: indicatorContainerDynamicStyle,
  45. titleContainer: titleContainerDynamicStyle,
  46. container: containerDynamicStyle,
  47. loading: loadingDynamicStyle,
  48. overlay: overlayDynamicStyle,
  49. title: titleDynamicStyle
  50. } = useStyles({
  51. floorBackgroundColor,
  52. spreadBehaviour,
  53. currentType,
  54. isDisabled,
  55. isLoading,
  56. radiuses,
  57. borders,
  58. colors,
  59. spaces,
  60. type
  61. });
  62. const titleProps: ITextProps = {
  63. color: currentType.titleColor,
  64. variant: titleVariant
  65. };
  66. const fontSize = typography[titleProps.variant as keyof NCoreUIKit.Typography].fontSize;
  67. let baseLocation = location ? location : {
  68. bottom: undefined,
  69. left: undefined,
  70. right: 0,
  71. top: 2
  72. };
  73. if(!location && title) {
  74. baseLocation = {
  75. right: -fontSize / String(title).length,
  76. top: -fontSize / 2,
  77. bottom: undefined,
  78. left: undefined
  79. };
  80. }
  81. if (isDisabled || isLoading) {
  82. const stateType = type === "danger" ? "error" : type;
  83. titleProps.customColor = colors.system.state.content.disabled[stateType];
  84. }
  85. const renderTitle = () => {
  86. if (!title) {
  87. return null;
  88. }
  89. return <View
  90. style={[
  91. stylesheet.titleContainer,
  92. titleContainerDynamicStyle
  93. ]}
  94. >
  95. <Text
  96. variant="bodyMediumSize"
  97. style={[
  98. titleStyle,
  99. stylesheet.title,
  100. titleDynamicStyle
  101. ]}
  102. {...titleProps}
  103. >
  104. {title}
  105. </Text>
  106. {renderOverlay()}
  107. </View>;
  108. };
  109. const renderIndicatorContainer = () => {
  110. if (isLoading) {
  111. return <Loading
  112. style={[
  113. loadingDynamicStyle
  114. ]}
  115. />;
  116. }
  117. return <View
  118. style={[
  119. stylesheet.indicatorContainer,
  120. indicatorContainerDynamicStyle,
  121. baseLocation
  122. ]}
  123. >
  124. {renderTitle()}
  125. </View>;
  126. };
  127. const renderOverlay = () => {
  128. return <View
  129. style={[
  130. stylesheet.overlay,
  131. overlayDynamicStyle
  132. ]}
  133. />;
  134. };
  135. return <View
  136. {...props}
  137. style={[
  138. style,
  139. stylesheet.container,
  140. containerDynamicStyle
  141. ]}
  142. >
  143. {children}
  144. {renderIndicatorContainer()}
  145. </View>;
  146. };
  147. export default NotificationIndicator;