Skip to content

Commit

Permalink
fix(select): throw Error that hide && submenu can not work
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Sep 21, 2018
1 parent cae2ae9 commit 0c4ae33
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ Can only be used when `isSetFunc` is not used.
see `isSetFunc`

`hide(ctx, key)` (optional) can be used to hide the button with the given key in the menu when true is returned.
Can not be used when `submenu` is set.

`columns` (Integer, optional) can be provided in order to limit the amount of buttons in one row. (default: 6)

Expand Down
15 changes: 11 additions & 4 deletions inline-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,20 @@ class TelegrafInlineMenu {
if (setFunc && submenu) {
throw new Error('setFunc and submenu can not be set at the same time.')
}
if (submenu && hide) {
// The submenu is a middleware that can do other things than callback_data
// question is an example: the reply to the text does not indicate the actionCode.
// Without the exact actionCode its not possible to determine the selected key(s) which is needed for hide(…, key)
throw new Error('hiding a dynamic submenu is not possible')
}

const keyFromCtx = ctx => ctx.match[ctx.match.length - 1]
const hideSelectAction = hide && (ctx => hide(ctx, keyFromCtx(ctx)))

const handler = {
action: new ActionCode(new RegExp(`${action}-([^:]+)`)),
hide: hideSelectAction
action: new ActionCode(new RegExp(`${action}-([^:]+)`))
}

if (additionalArgs.hide) {
handler.hide = ctx => hide(ctx, keyFromCtx(ctx))
}

if (setFunc) {
Expand Down
11 changes: 11 additions & 0 deletions test/select.submenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,14 @@ test('submenu button works', async t => {

await bot.handleUpdate({callback_query: {data: 'a:c-a:d'}})
})

test('hide dynamic submenu does not work', t => {
const menu = new TelegrafInlineMenu('foo')

t.throws(() => {
menu.select('a', ['a', 'b'], {
hide: () => false,
submenu: new TelegrafInlineMenu('bar')
})
}, /dynamic/)
})

0 comments on commit 0c4ae33

Please sign in to comment.