index.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import {
  2. useState
  3. } from "react";
  4. import {
  5. TouchableOpacity,
  6. Image,
  7. View
  8. } from "react-native";
  9. import type ISiteLogoProps from "./type";
  10. import stylesheet, {
  11. getSiteLogoSize,
  12. useStyles
  13. } from "./stylesheet";
  14. import {
  15. NCoreUIKitTheme
  16. } from "../../core/hooks";
  17. import {
  18. Home as HomeIcon
  19. } from "lucide-react-native";
  20. import Loading from "../loading";
  21. import Text from "../text";
  22. const SiteLogo = ({
  23. logoBackgroundColor = "default",
  24. isWorkWithFlex1 = true,
  25. isShowBorder = false,
  26. subTitleCustomColor,
  27. isCustomImageSize,
  28. isShowIcon = true,
  29. isWorkWithAction,
  30. titleCustomColor,
  31. image: ImageProp,
  32. backgroundColor,
  33. subTitleVariant,
  34. icon: IconProp,
  35. size = "large",
  36. subTitleColor,
  37. titleVariant,
  38. imageSource,
  39. customTheme,
  40. borderColor,
  41. imageSpace,
  42. isDisabled,
  43. titleColor,
  44. imageProps,
  45. isLoading,
  46. iconColor,
  47. imageUrl,
  48. subTitle,
  49. onPress,
  50. title,
  51. style
  52. }: ISiteLogoProps) => {
  53. const {
  54. borders,
  55. colors,
  56. spaces
  57. } = NCoreUIKitTheme.useContext(customTheme);
  58. const currentSize = getSiteLogoSize({
  59. spaces,
  60. size
  61. });
  62. const [
  63. imageLoadError,
  64. setImageLoadError
  65. ] = useState(false);
  66. const {
  67. contentContainer: contentContainerDynamicStyle,
  68. titlesContainer: titlesContainerDynamicStyle,
  69. container: containerDynamicStyle,
  70. image: imageDynamicStyle,
  71. icon: iconDynamicStyle
  72. } = useStyles({
  73. logoBackgroundColor,
  74. isCustomImageSize,
  75. image: ImageProp,
  76. isWorkWithFlex1,
  77. backgroundColor,
  78. isShowBorder,
  79. borderColor,
  80. currentSize,
  81. isDisabled,
  82. imageSpace,
  83. isLoading,
  84. imageUrl,
  85. borders,
  86. colors,
  87. spaces
  88. });
  89. const renderIcon = () => {
  90. if(IconProp) {
  91. return <IconProp
  92. size={currentSize.iconSize}
  93. customColor={iconColor}
  94. color="default"
  95. style={{
  96. ...iconDynamicStyle
  97. }}
  98. />;
  99. }
  100. return <HomeIcon
  101. color={iconColor ? iconColor : colors.content.icon.default}
  102. size={currentSize.iconSize}
  103. style={{
  104. ...iconDynamicStyle
  105. }}
  106. />;
  107. };
  108. const renderLoading = () => {
  109. let loadingColor: keyof NCoreUIKit.IconContentColors = "default";
  110. if(title) {
  111. loadingColor = "onPrimary";
  112. } else if(imageUrl || ImageProp) {
  113. loadingColor = "mid";
  114. }
  115. return <Loading
  116. color={loadingColor}
  117. />;
  118. };
  119. const renderContent = () => {
  120. if(isLoading) {
  121. return renderLoading();
  122. }
  123. if(IconProp) {
  124. return renderIcon();
  125. }
  126. if(!ImageProp && !imageUrl) {
  127. return renderIcon();
  128. }
  129. if((imageUrl || ImageProp) && imageLoadError) {
  130. return renderIcon();
  131. }
  132. if(ImageProp) {
  133. return <ImageProp
  134. {...imageProps}
  135. onError={() => setImageLoadError(true)}
  136. height={currentSize.size}
  137. width={currentSize.size}
  138. style={[
  139. stylesheet.image,
  140. imageDynamicStyle,
  141. imageProps?.style
  142. ]}
  143. />;
  144. }
  145. if(imageUrl) {
  146. return <Image
  147. {...imageProps}
  148. onError={() => setImageLoadError(true)}
  149. height={currentSize.size}
  150. width={currentSize.size}
  151. source={{
  152. uri: imageUrl,
  153. ...imageSource
  154. }}
  155. style={[
  156. stylesheet.image,
  157. imageDynamicStyle,
  158. imageProps?.style
  159. ]}
  160. />;
  161. }
  162. return null;
  163. };
  164. const renderTitles = () => {
  165. if(!title) {
  166. return null;
  167. }
  168. return <View
  169. style={[
  170. stylesheet.titlesContainer,
  171. titlesContainerDynamicStyle
  172. ]}
  173. >
  174. <Text
  175. variant={titleVariant ? titleVariant : currentSize.titleSize}
  176. color={titleColor ? titleColor : "high"}
  177. customColor={titleCustomColor}
  178. ellipsizeMode="tail"
  179. numberOfLines={1}
  180. style={{
  181. width: "100%"
  182. }}
  183. >
  184. {title}
  185. </Text>
  186. {
  187. subTitle ?
  188. <Text
  189. variant={subTitleVariant ? subTitleVariant : currentSize.subTitleSize}
  190. color={subTitleColor ? subTitleColor : "low"}
  191. customColor={subTitleCustomColor}
  192. ellipsizeMode="tail"
  193. numberOfLines={1}
  194. style={{
  195. width: "100%"
  196. }}
  197. >
  198. {subTitle}
  199. </Text>
  200. :
  201. null
  202. }
  203. </View>;
  204. };
  205. const renderContentContainer = () => {
  206. if(!isShowIcon) {
  207. return null;
  208. }
  209. return <View
  210. style={[
  211. stylesheet.contentContainer,
  212. contentContainerDynamicStyle
  213. ]}
  214. >
  215. {renderContent()}
  216. </View>;
  217. };
  218. return <TouchableOpacity
  219. disabled={!isWorkWithAction || isDisabled || isLoading}
  220. onPress={!isWorkWithAction || isDisabled || isLoading ? undefined : () => {
  221. if(onPress) onPress();
  222. }}
  223. style={[
  224. style,
  225. stylesheet.container,
  226. containerDynamicStyle
  227. ]}
  228. >
  229. {renderContentContainer()}
  230. {renderTitles()}
  231. </TouchableOpacity>;
  232. };
  233. export default SiteLogo;