Skip to content

Commit

Permalink
feat(select): limit maximal rows of buttons
Browse files Browse the repository at this point in the history
also add tests
  • Loading branch information
EdJoPaTo committed Sep 16, 2018
1 parent 2e017b2 commit 4089727
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Creates a button that toggles a setting
This will show an emoji to the user on the button as text prefix.
`hide(ctx)` (optional) can hide the button when return is true.

### `menu.select(actionCode, options, setFunc, {isSetFunc, prefixFunc, hide, joinLastRow, columns})`
### `menu.select(actionCode, options, setFunc, {isSetFunc, prefixFunc, hide, joinLastRow, columns, maxRows})`

Creates multiple buttons for each provided option.

Expand All @@ -146,7 +146,9 @@ Can only be used when `isSetFunc` is not used.

`hide(ctx, key)` (optional) can be used to hide some or all buttons in the menu when true is returned on the specific key.

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

`maxRows` (Integer, optional) can be provided to limit the maximal rows of buttons. (default: 10)

### `menu.list`

Expand Down
8 changes: 3 additions & 5 deletions align-buttons.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
function getRowsOfButtons(buttons, columns) {
if (!columns) {
columns = buttons.length
}
function getRowsOfButtons(buttons, columns = 6, maxRows = 10) {
const maxButtons = Math.min(maxRows * columns, buttons.length)
const rows = []
for (let i = 0; i < buttons.length; i += columns) {
for (let i = 0; i < maxButtons; i += columns) {
const slice = buttons.slice(i, i + columns)
rows.push(slice)
}
Expand Down
42 changes: 42 additions & 0 deletions align-buttons.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import test from 'ava'

import {getRowsOfButtons} from './align-buttons'

function generateCharArray(charA, charZ) {
// https://stackoverflow.com/questions/24597634/how-to-generate-an-array-of-alphabet-in-jquery/24597663#24597663
const a = []
let i = charA.charCodeAt(0)
const j = charZ.charCodeAt(0)
for (; i <= j; ++i) {
a.push(String.fromCharCode(i))
}
return a
}

const inputData = generateCharArray('A', 'E')

test('without arg in one line', t => {
const result = getRowsOfButtons(generateCharArray('A', 'E'))
t.deepEqual(result, [
inputData
])
})

test('less columns that buttons', t => {
const result = getRowsOfButtons(generateCharArray('A', 'E'), 3)
t.deepEqual(result, [
['A', 'B', 'C'],
['D', 'E']
])
})

test('trim by maxRows', t => {
const result = getRowsOfButtons(generateCharArray('A', 'Z'), 1, 5)
t.deepEqual(result, [
['A'],
['B'],
['C'],
['D'],
['E']
])
})
10 changes: 8 additions & 2 deletions telegraf-inline-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,13 @@ class TelegrafInlineMenu {
}
}

function generateSelectButtons(actionCodeBase, options, {isSetFunc, prefixFunc, hide, columns}) {
function generateSelectButtons(actionCodeBase, options, {
columns,
hide,
isSetFunc,
maxRows,
prefixFunc
}) {
const isArray = Array.isArray(options)
const keys = isArray ? options : Object.keys(options)
const buttons = keys.map(key => {
Expand All @@ -299,7 +305,7 @@ function generateSelectButtons(actionCodeBase, options, {isSetFunc, prefixFunc,
hide: hideKey
}
})
return getRowsOfButtons(buttons, columns)
return getRowsOfButtons(buttons, columns, maxRows)
}

function goUpUntilTrue(start, func) {
Expand Down

0 comments on commit 4089727

Please sign in to comment.