| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- import {
- type ReactNode,
- Fragment
- } from "react";
- import {
- type MenuContextType,
- type MenuDataType
- } from "../types/menu";
- import NCoreContext, {
- type ConfigType
- } from "ncore-context";
- import Menu from "../components/menu";
- import {
- uuid
- } from "../utils";
- class NCoreUIKitMenu extends NCoreContext<MenuContextType, ConfigType<MenuContextType>> {
- constructor({
- data = []
- }: {
- data?: Array<MenuDataType>
- }) {
- super({
- get: () => undefined,
- unload: () => "",
- update: () => {},
- close: () => {},
- load: () => "",
- open: () => {},
- data: data
- }, {
- key: "NCoreUIKit-MenuContext"
- });
- };
- load = (menuData: MenuDataType) => {
- const currentData = this.state.data;
- const menuID = menuData.id ? menuData.id : uuid();
- currentData.push({
- ...menuData,
- id: menuID
- });
- this.setState({
- data: currentData
- });
- return menuID;
- };
- get = ({
- index,
- id
- }: {
- index?: number;
- id?: string;
- }) => {
- const currentData = this.state.data;
- if(id) {
- const currentIndex = currentData.findIndex(cI => cI.id === id);
- return currentData[currentIndex];
- }
- if(index) {
- return currentData[index];
- }
- return undefined;
- };
- update = ({
- menuData,
- index,
- id
- }: {
- menuData: MenuDataType;
- index?: number;
- id?: string;
- }) => {
- const currentData = this.state.data;
- if(id) {
- const currentIndex = currentData.findIndex(cI => cI.id === id);
- currentData[currentIndex] = menuData;
- }
- if(index) {
- currentData[index] = menuData;
- }
- this.setState({
- data: currentData
- });
- };
- open = (props?: {
- index?: number;
- id?: string;
- }) => {
- const currentData = this.state.data;
- if (props && props.id) {
- const keyIndex = currentData.findIndex((menu) => menu.id === props.id);
- if (keyIndex !== -1) {
- currentData[keyIndex]!.isCollapse = true;
- this.setState({
- data: currentData
- });
- }
- return;
- }
- if (props && props.index !== undefined && currentData[props.index]) {
- currentData[props.index!]!.isCollapse = true;
- this.setState({
- data: currentData
- });
- return;
- }
- };
- close = (props?: {
- index?: number;
- id?: string;
- }) => {
- const currentData = this.state.data;
- if (props && props.id) {
- const keyIndex = currentData.findIndex((menu) => menu.id === props.id);
- if (keyIndex !== -1) {
- currentData[keyIndex]!.isCollapse = false;
- this.setState({
- data: currentData
- });
- }
- return;
- }
- if (props && props.index !== undefined && currentData[props.index]) {
- currentData[props.index!]!.isCollapse = false;
- this.setState({
- data: currentData
- });
- return;
- }
- };
- unload = (props?: {
- index?: number;
- id?: string;
- }) => {
- const currentData = this.state.data;
- if (props && props.id) {
- const keyIndex = currentData.findIndex((menu) => menu.id === props.id);
- if (keyIndex !== -1) {
- currentData.splice(keyIndex, 1);
- this.setState({
- data: currentData
- });
- }
- return;
- }
- if (props && props.index !== undefined) {
- currentData.splice(props.index, 1);
- this.setState({
- data: currentData
- });
- return;
- }
- currentData.pop();
- this.setState({
- data: currentData
- });
- };
- Render = ({
- children
- }: {
- children: ReactNode;
- }) => {
- const {
- data
- } = this.useContext();
- return <Fragment>
- {children}
- {data.map((item: MenuDataType, index: number) => {
- return <Menu
- key={`NCoreUIKit-Menu-${item.id}`}
- id={item.id as string}
- close={() => {
- if(item.isAutoClosed) {
- this.close({
- index: index,
- id: item.id
- });
- }
- }}
- onClosed={() => {
- if(item.onClosed) {
- item.onClosed({
- id: item.id as string
- });
- }
- if(item.isAutoClosedOnClosed === undefined || item.isAutoClosedOnClosed === true) {
- this.close({
- index: index,
- id: item.id
- });
- }
- }}
- {...item}
- />;
- })}
- </Fragment>;
- };
- }
- export default NCoreUIKitMenu;
|