menu.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import {
  2. type ReactNode,
  3. Fragment
  4. } from "react";
  5. import {
  6. type MenuContextType,
  7. type MenuDataType
  8. } from "../types/menu";
  9. import NCoreContext, {
  10. type ConfigType
  11. } from "ncore-context";
  12. import Menu from "../components/menu";
  13. import {
  14. uuid
  15. } from "../utils";
  16. class NCoreUIKitMenu extends NCoreContext<MenuContextType, ConfigType<MenuContextType>> {
  17. constructor({
  18. data = []
  19. }: {
  20. data?: Array<MenuDataType>
  21. }) {
  22. super({
  23. get: () => undefined,
  24. unload: () => "",
  25. update: () => {},
  26. close: () => {},
  27. load: () => "",
  28. open: () => {},
  29. data: data
  30. }, {
  31. key: "NCoreUIKit-MenuContext"
  32. });
  33. };
  34. load = (menuData: MenuDataType) => {
  35. const currentData = this.state.data;
  36. const menuID = menuData.id ? menuData.id : uuid();
  37. currentData.push({
  38. ...menuData,
  39. id: menuID
  40. });
  41. this.setState({
  42. data: currentData
  43. });
  44. return menuID;
  45. };
  46. get = ({
  47. index,
  48. id
  49. }: {
  50. index?: number;
  51. id?: string;
  52. }) => {
  53. const currentData = this.state.data;
  54. if(id) {
  55. const currentIndex = currentData.findIndex(cI => cI.id === id);
  56. return currentData[currentIndex];
  57. }
  58. if(index) {
  59. return currentData[index];
  60. }
  61. return undefined;
  62. };
  63. update = ({
  64. menuData,
  65. index,
  66. id
  67. }: {
  68. menuData: MenuDataType;
  69. index?: number;
  70. id?: string;
  71. }) => {
  72. const currentData = this.state.data;
  73. if(id) {
  74. const currentIndex = currentData.findIndex(cI => cI.id === id);
  75. currentData[currentIndex] = menuData;
  76. }
  77. if(index) {
  78. currentData[index] = menuData;
  79. }
  80. this.setState({
  81. data: currentData
  82. });
  83. };
  84. open = (props?: {
  85. index?: number;
  86. id?: string;
  87. }) => {
  88. const currentData = this.state.data;
  89. if (props && props.id) {
  90. const keyIndex = currentData.findIndex((menu) => menu.id === props.id);
  91. if (keyIndex !== -1) {
  92. currentData[keyIndex]!.isCollapse = true;
  93. this.setState({
  94. data: currentData
  95. });
  96. }
  97. return;
  98. }
  99. if (props && props.index !== undefined && currentData[props.index]) {
  100. currentData[props.index!]!.isCollapse = true;
  101. this.setState({
  102. data: currentData
  103. });
  104. return;
  105. }
  106. };
  107. close = (props?: {
  108. index?: number;
  109. id?: string;
  110. }) => {
  111. const currentData = this.state.data;
  112. if (props && props.id) {
  113. const keyIndex = currentData.findIndex((menu) => menu.id === props.id);
  114. if (keyIndex !== -1) {
  115. currentData[keyIndex]!.isCollapse = false;
  116. this.setState({
  117. data: currentData
  118. });
  119. }
  120. return;
  121. }
  122. if (props && props.index !== undefined && currentData[props.index]) {
  123. currentData[props.index!]!.isCollapse = false;
  124. this.setState({
  125. data: currentData
  126. });
  127. return;
  128. }
  129. };
  130. unload = (props?: {
  131. index?: number;
  132. id?: string;
  133. }) => {
  134. const currentData = this.state.data;
  135. if (props && props.id) {
  136. const keyIndex = currentData.findIndex((menu) => menu.id === props.id);
  137. if (keyIndex !== -1) {
  138. currentData.splice(keyIndex, 1);
  139. this.setState({
  140. data: currentData
  141. });
  142. }
  143. return;
  144. }
  145. if (props && props.index !== undefined) {
  146. currentData.splice(props.index, 1);
  147. this.setState({
  148. data: currentData
  149. });
  150. return;
  151. }
  152. currentData.pop();
  153. this.setState({
  154. data: currentData
  155. });
  156. };
  157. Render = ({
  158. children
  159. }: {
  160. children: ReactNode;
  161. }) => {
  162. const {
  163. data
  164. } = this.useContext();
  165. return <Fragment>
  166. {children}
  167. {data.map((item: MenuDataType, index: number) => {
  168. return <Menu
  169. key={`NCoreUIKit-Menu-${item.id}`}
  170. id={item.id as string}
  171. close={() => {
  172. if(item.isAutoClosed) {
  173. this.close({
  174. index: index,
  175. id: item.id
  176. });
  177. }
  178. }}
  179. onClosed={() => {
  180. if(item.onClosed) {
  181. item.onClosed({
  182. id: item.id as string
  183. });
  184. }
  185. if(item.isAutoClosedOnClosed === undefined || item.isAutoClosedOnClosed === true) {
  186. this.close({
  187. index: index,
  188. id: item.id
  189. });
  190. }
  191. }}
  192. {...item}
  193. />;
  194. })}
  195. </Fragment>;
  196. };
  197. }
  198. export default NCoreUIKitMenu;