-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Keyboard button builders and Keyboard.from
#104
Comments
Keyboard.from
Keyboard.from
Putting n buttons into m columns is something helpful that should be part of the main library in my opinion. See grammy-inline-menu align.ts and its test for inspiration. Creating buttons via functions seems overkill to me as its literally only an object with two keys which are autosuggested from typings (even for JavaScript users). This would only create pointless boilerplate code which abstracts something that isnt worth abstracting. |
I disagree. Having to write |
I am not sure if I'd ever use the button builders myself, but I do see that especially in functional patters such as |
I don't see the need in functional patterns which I use all the time. I also don't see the argument of writing callback_data which is auto suggested and auto completed from IDEs. const buttons = ['a', 'b'].map(o => ({name: o, callback_data: `something-${o}`})); |
I think the idea is to be able to do const keyboard = Keyboard.from(['a', 'b'].map(o => Keyboord.button(o, `something-${o}`))); |
Update: this is already partially implemented, we have Helper functions for buttons are not here yet, but we have a package (https://github.com/loskir/grammy-markup) that provides them. new InlineKeyboard([
[IButton.text("Get random music", "random")],
[IButton.switchInline("Send music to friends")],
]) I hope we're still planning to add this feature in one form or another. For example, we can add static methods on |
I agree that we should add static methods to the keyboard classes. I'm comparably unsure about the naming, though … |
I'm looking forward to things like Keyboard.from([MONTHS]).reflow() to let the user pick a month |
Why not |
That will obviously work if you prefer to work on raw arrays. I don't. I'd like to tell the keyboard how many columns it should have, and then it will do that. I don't want to know about transforming arrays, neither with nor without a third-party library. |
As soon as you are using buttons of multiple things at the same time like months and years you need to put both into columns and not mix them together. Also at the end should be a full width confirm button. In my opinion it’s needed to work with the double array structure to get the keyboard you want. Simplifying the whole menu with a simple array with a column width setting will not work for most cases. |
While correct, this is unrelated from the current discussion. Obviously we support custom layouts with granular control. The point is that if this is not needed (the array can be processed by |
Currently, we can only create complete keyboards using the keyboard plugin. It may be useful to add helpers that create individual buttons.
We should then add a static method
Keyboard.from
which takes a 2D-array of button objects, and creates a keyboard from them. Optionally, it handles reshapes on the fly. As a result, you can also pass a 1D-array and a number of columns/rows and the created keyboard will have the specified dimensions.All of the above should be done for inline keyboards too.
Requested by @Loskir in the discussion around https://t.me/grammyjs/11332
The text was updated successfully, but these errors were encountered: