index.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import {
  2. Fragment,
  3. type FC
  4. } from "react";
  5. import {
  6. ScrollView,
  7. View
  8. } from "react-native";
  9. import type IPageContainerProps from "./type";
  10. import stylesheet from "./stylesheet";
  11. import {
  12. NCoreUIKitTheme
  13. } from "../../core/hooks";
  14. import {
  15. SafeAreaView
  16. } from "react-native-safe-area-context";
  17. const PageContainer: FC<IPageContainerProps> = ({
  18. backgroundColor = "default" as keyof NCoreUIKit.ContainerContentColors,
  19. isWrapSafeareaContext = false,
  20. safeAreaViewBackgroundColor,
  21. isScrollable = false,
  22. safeAreaViewStyle,
  23. scrollViewStyle,
  24. scrollViewProps,
  25. renderOverlays,
  26. customTheme,
  27. children,
  28. style,
  29. ...props
  30. }) => {
  31. const {
  32. colors,
  33. spaces
  34. } = NCoreUIKitTheme.useContext(customTheme);
  35. const renderScrollview = () => {
  36. if(style) {
  37. console.error("Hey!. You make wrong things. If you must be use isScrollable={true}, you must be use scrollViewStyle. Do not use style prop for this case.");
  38. }
  39. if(Object.keys(props).length) {
  40. console.error("Hey!. You make wrong things. If you must be use isScrollable={true}, you must be use scrollViewProps. Do not use default view props for this case.");
  41. }
  42. return <ScrollView
  43. keyboardShouldPersistTaps="handled"
  44. {...scrollViewProps}
  45. contentContainerStyle={[
  46. {
  47. padding: spaces.spacingMd
  48. },
  49. scrollViewProps?.contentContainerStyle
  50. ]}
  51. style={[
  52. {
  53. backgroundColor: colors.content.container[backgroundColor]
  54. },
  55. stylesheet.container,
  56. scrollViewStyle
  57. ]}
  58. >
  59. {children}
  60. </ScrollView>;
  61. };
  62. const renderView = () => {
  63. if(scrollViewStyle) {
  64. console.error("Hey!. You make wrong things. If you must be use isScrollable={false}, you must be use style. Do not use scrollViewStyle prop for this case.");
  65. }
  66. if(scrollViewProps) {
  67. console.error("Hey!. You make wrong things. If you must be use isScrollable={false}, you musn't use scrollViewProps.");
  68. }
  69. return <View
  70. {...props}
  71. style={[
  72. {
  73. backgroundColor: colors.content.container[backgroundColor],
  74. padding: spaces.spacingMd
  75. },
  76. stylesheet.container,
  77. style
  78. ]}
  79. >
  80. {children}
  81. </View>;
  82. };
  83. const renderWithSafeareaView = () => {
  84. return <SafeAreaView
  85. style={[
  86. safeAreaViewStyle,
  87. {
  88. backgroundColor: safeAreaViewBackgroundColor ? colors.content.container[safeAreaViewBackgroundColor] : colors.content.container[backgroundColor]
  89. },
  90. stylesheet.safeAreaViewContainer
  91. ]}
  92. >
  93. {isScrollable ? renderScrollview() : renderView()}
  94. {renderOverlays ? renderOverlays() : null}
  95. </SafeAreaView>;
  96. };
  97. const renderWithoutSafeareaView = () => {
  98. return isScrollable ? renderScrollview() : renderView();
  99. };
  100. const renderSafeareaContext = () => {
  101. return isWrapSafeareaContext ? renderWithSafeareaView() : <Fragment>
  102. {renderWithoutSafeareaView()}
  103. {renderOverlays ? renderOverlays() : null}
  104. </Fragment>;
  105. };
  106. return renderSafeareaContext();
  107. };
  108. export default PageContainer;