| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- import {
- type ReactNode
- } from "react";
- import {
- StyleSheet,
- View
- } from "react-native";
- import {
- type EmbeddedMenuContextType,
- type EmbeddedMenuDataType
- } from "../types/embeddedMenu";
- import NCoreContext, {
- type ConfigType
- } from "ncore-context";
- import EmbeddedMenu from "../components/embeddedMenu";
- import {
- uuid
- } from "../utils";
- const stylesheet = StyleSheet.create({
- wrapper: {
- flexDirection: "row",
- flex: 1
- }
- });
- class NCoreUIKitEmbeddedMenu extends NCoreContext<EmbeddedMenuContextType, ConfigType<EmbeddedMenuContextType>> {
- constructor({
- data = []
- }: {
- data?: Array<EmbeddedMenuDataType>
- }) {
- super({
- get: () => undefined,
- unload: () => "",
- update: () => {},
- close: () => {},
- load: () => "",
- open: () => {},
- data: data
- }, {
- key: "NCoreUIKit-MenuContext"
- });
- };
- load = (menuData: EmbeddedMenuDataType) => {
- 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: EmbeddedMenuDataType;
- 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 = ({
- headerSpace,
- navigation,
- children,
- index,
- id
- }: {
- navigation: NCoreUIKit.Navigation;
- children?: ReactNode;
- headerSpace?: number;
- index?: number;
- id?: string;
- }) => {
- const {
- data
- } = this.useContext();
- const renderMenu = ({
- currentMenu
- }: {
- currentMenu: EmbeddedMenuDataType;
- }) => {
- return <EmbeddedMenu
- {...currentMenu}
- key={`NCoreUIKit-Menu-${currentMenu.id}`}
- id={currentMenu.id as string}
- headerSpace={headerSpace}
- close={() => {
- if(currentMenu.isAutoClosed) {
- this.close({
- id: currentMenu.id,
- index: index
- });
- }
- }}
- onClosed={() => {
- if(currentMenu.onClosed) {
- currentMenu.onClosed({
- id: currentMenu.id
- });
- }
- if(currentMenu.isAutoClosedOnClosed === undefined || currentMenu.isAutoClosedOnClosed === true) {
- this.close({
- id: currentMenu.id,
- index: index
- });
- }
- }}
- navigation={navigation}
- />;
- };
- if(id || index) {
- const currentMenu = data.find((dItem, dIndex) => {
- if(id) {
- return dItem.id === id;
- } else {
- return dIndex === index;
- }
- });
- if(!currentMenu) {
- return null;
- }
- if(children) {
- return <View
- style={stylesheet.wrapper}
- >
- {renderMenu({
- currentMenu
- })}
- {children}
- </View>;
- }
- return renderMenu({
- currentMenu
- });
- }
- return null;
- };
- }
- export default NCoreUIKitEmbeddedMenu;
|