embeddedMenu.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import {
  2. type ReactNode
  3. } from "react";
  4. import {
  5. StyleSheet,
  6. View
  7. } from "react-native";
  8. import {
  9. type EmbeddedMenuContextType,
  10. type EmbeddedMenuDataType
  11. } from "../types/embeddedMenu";
  12. import NCoreContext, {
  13. type ConfigType
  14. } from "ncore-context";
  15. import EmbeddedMenu from "../components/embeddedMenu";
  16. import {
  17. uuid
  18. } from "../utils";
  19. const stylesheet = StyleSheet.create({
  20. wrapper: {
  21. flexDirection: "row",
  22. flex: 1
  23. }
  24. });
  25. class NCoreUIKitEmbeddedMenu extends NCoreContext<EmbeddedMenuContextType, ConfigType<EmbeddedMenuContextType>> {
  26. constructor({
  27. data = []
  28. }: {
  29. data?: Array<EmbeddedMenuDataType>
  30. }) {
  31. super({
  32. get: () => undefined,
  33. unload: () => "",
  34. update: () => {},
  35. close: () => {},
  36. load: () => "",
  37. open: () => {},
  38. data: data
  39. }, {
  40. key: "NCoreUIKit-MenuContext"
  41. });
  42. };
  43. load = (menuData: EmbeddedMenuDataType) => {
  44. const currentData = this.state.data;
  45. const menuID = menuData.id ? menuData.id : uuid();
  46. currentData.push({
  47. ...menuData,
  48. id: menuID
  49. });
  50. this.setState({
  51. data: currentData
  52. });
  53. return menuID;
  54. };
  55. get = ({
  56. index,
  57. id
  58. }: {
  59. index?: number;
  60. id?: string;
  61. }) => {
  62. const currentData = this.state.data;
  63. if(id) {
  64. const currentIndex = currentData.findIndex(cI => cI.id === id);
  65. return currentData[currentIndex];
  66. }
  67. if(index) {
  68. return currentData[index];
  69. }
  70. return undefined;
  71. };
  72. update = ({
  73. menuData,
  74. index,
  75. id
  76. }: {
  77. menuData: EmbeddedMenuDataType;
  78. index?: number;
  79. id?: string;
  80. }) => {
  81. const currentData = this.state.data;
  82. if(id) {
  83. const currentIndex = currentData.findIndex(cI => cI.id === id);
  84. currentData[currentIndex] = menuData;
  85. }
  86. if(index) {
  87. currentData[index] = menuData;
  88. }
  89. this.setState({
  90. data: currentData
  91. });
  92. };
  93. open = (props?: {
  94. index?: number;
  95. id?: string;
  96. }) => {
  97. const currentData = this.state.data;
  98. if (props && props.id) {
  99. const keyIndex = currentData.findIndex((menu) => menu.id === props.id);
  100. if (keyIndex !== -1) {
  101. currentData[keyIndex]!.isCollapse = true;
  102. this.setState({
  103. data: currentData
  104. });
  105. }
  106. return;
  107. }
  108. if (props && props.index !== undefined && currentData[props.index]) {
  109. currentData[props.index!]!.isCollapse = true;
  110. this.setState({
  111. data: currentData
  112. });
  113. return;
  114. }
  115. };
  116. close = (props?: {
  117. index?: number;
  118. id?: string;
  119. }) => {
  120. const currentData = this.state.data;
  121. if (props && props.id) {
  122. const keyIndex = currentData.findIndex((menu) => menu.id === props.id);
  123. if (keyIndex !== -1) {
  124. currentData[keyIndex]!.isCollapse = false;
  125. this.setState({
  126. data: currentData
  127. });
  128. }
  129. return;
  130. }
  131. if (props && props.index !== undefined && currentData[props.index]) {
  132. currentData[props.index!]!.isCollapse = false;
  133. this.setState({
  134. data: currentData
  135. });
  136. return;
  137. }
  138. };
  139. unload = (props?: {
  140. index?: number;
  141. id?: string;
  142. }) => {
  143. const currentData = this.state.data;
  144. if (props && props.id) {
  145. const keyIndex = currentData.findIndex((menu) => menu.id === props.id);
  146. if (keyIndex !== -1) {
  147. currentData.splice(keyIndex, 1);
  148. this.setState({
  149. data: currentData
  150. });
  151. }
  152. return;
  153. }
  154. if (props && props.index !== undefined) {
  155. currentData.splice(props.index, 1);
  156. this.setState({
  157. data: currentData
  158. });
  159. return;
  160. }
  161. currentData.pop();
  162. this.setState({
  163. data: currentData
  164. });
  165. };
  166. Render = ({
  167. headerSpace,
  168. navigation,
  169. children,
  170. index,
  171. id
  172. }: {
  173. navigation: NCoreUIKit.Navigation;
  174. children?: ReactNode;
  175. headerSpace?: number;
  176. index?: number;
  177. id?: string;
  178. }) => {
  179. const {
  180. data
  181. } = this.useContext();
  182. const renderMenu = ({
  183. currentMenu
  184. }: {
  185. currentMenu: EmbeddedMenuDataType;
  186. }) => {
  187. return <EmbeddedMenu
  188. {...currentMenu}
  189. key={`NCoreUIKit-Menu-${currentMenu.id}`}
  190. id={currentMenu.id as string}
  191. headerSpace={headerSpace}
  192. close={() => {
  193. if(currentMenu.isAutoClosed) {
  194. this.close({
  195. id: currentMenu.id,
  196. index: index
  197. });
  198. }
  199. }}
  200. onClosed={() => {
  201. if(currentMenu.onClosed) {
  202. currentMenu.onClosed({
  203. id: currentMenu.id
  204. });
  205. }
  206. if(currentMenu.isAutoClosedOnClosed === undefined || currentMenu.isAutoClosedOnClosed === true) {
  207. this.close({
  208. id: currentMenu.id,
  209. index: index
  210. });
  211. }
  212. }}
  213. navigation={navigation}
  214. />;
  215. };
  216. if(id || index) {
  217. const currentMenu = data.find((dItem, dIndex) => {
  218. if(id) {
  219. return dItem.id === id;
  220. } else {
  221. return dIndex === index;
  222. }
  223. });
  224. if(!currentMenu) {
  225. return null;
  226. }
  227. if(children) {
  228. return <View
  229. style={stylesheet.wrapper}
  230. >
  231. {renderMenu({
  232. currentMenu
  233. })}
  234. {children}
  235. </View>;
  236. }
  237. return renderMenu({
  238. currentMenu
  239. });
  240. }
  241. return null;
  242. };
  243. }
  244. export default NCoreUIKitEmbeddedMenu;