Giter VIP home page Giter VIP logo

menu-pagination's Introduction

Pagination Addon for Menu Plugin


This plugin extends the capabilities of the menu plugin by adding pagination support.

It provides functions to create paginated menus, handle page changes, and display data in various formats.


Main Function createPagination(menuRange, ...options)

Function createPagination is main core of pagination on menu. There we are define how all have to work.

...options type description default
allowBackToMenu boolean Determines whether the user can go back to an previous menu true
displayType string Determines how the data is to be displayed, ("text" or "buttons") "text"
schema string Schema how pagination is to be displayed "default"
staticData NestedObject Static data on which pagination is to work undefined
dynamicDataFn function(page) Function from which the data are taken on which pagination is to work undefined
displayDataFn function(data, index) Function by which we define how the data are to be displayed undefined
buttonFn function(ctx, data) Function to be executed when the button is pressed (only for displayType "buttons") undefined

Usage

Static data

import { Bot, session } from "grammy";
import { Menu } from "@grammyjs/menu";
import {
    createPagination,
    parseArrayIntoObjects,
} from "@grammyjs/menu-pagination";

const bot = new Bot("");

bot.use(
    session({
        initial() {
            return {};
        },
    }),
);

const someMenu = new Menu("some-menu");

const myData = [
    { name: "John", dsc: "CEO" },
    { name: "Kendrick", dsc: "Devops" },
    {
        name: "Leo",
        dsc: "HR Agent",
    },
];

const elementsPerPage = 2;

const parsedData = parseArrayIntoObjects(myData, elementsPerPage);

someMenu.dynamic(async (ctx, range) => {
    await createPagination(range, ctx, {
        allowBackToMenu: false,
        displayType: "buttons",
        staticData: parsedData,
        displayDataFn: (data) => {
            return `${data.name}, ${data.dsc}`;
        },
        buttonFn: async (ctx, data) => {
            await ctx.reply(
                `Hello! My name is ${data.name}, I'am ${data.dsc}`,
            );
        },
    });
});

bot.use(someMenu);

bot.command("pagination", (ctx) => {
    ctx.reply("Check this crazy pagination!", { reply_markup: someMenu });
});

bot.start();

You can find more examples and documentation on the plugin page on the website.


Todos 🧰

  • Add a middleware that extends ctx with ctx.replyWithPagination, so that you can display the first page of pagination without developer integration ( only for displayType "text")
  • Add more advanced(optimized) logic for memoization dynamicData
  • Add "numbers" schema
  • Add debug's with https://deno.land/x/debug/mod.ts
  • Add session cleanup if pagination is not used

menu-pagination's People

Contributors

camelik avatar

Stargazers

 avatar Rakso Fox avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.