-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add formatState function to select and toggle
defaults to prefixEmoji allows for fancy things like 'lamp: on' and 'lamp: off' BREAKING CHANGE: select multiselect is renamed to showFalseEmoji
- Loading branch information
Showing
4 changed files
with
46 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {CallbackButtonTemplate} from '../keyboard' | ||
import {ContextPathFunc, ConstOrPromise, ConstOrContextPathFunc} from '../generic-types' | ||
import {prefixEmoji} from '../prefix' | ||
|
||
import {SingleButtonOptions} from './basic' | ||
|
||
export type FormatStateFunction<Context> = (context: Context, text: string, state: boolean, path: string) => ConstOrPromise<string> | ||
|
||
export interface ToggleOptions<Context> extends SingleButtonOptions<Context> { | ||
readonly set: (context: Context, newState: boolean, path: string) => ConstOrPromise<void>; | ||
readonly isSet: ContextPathFunc<Context, boolean>; | ||
readonly formatState?: FormatStateFunction<Context>; | ||
} | ||
|
||
export function generateToggleButton<Context>(text: ConstOrContextPathFunc<Context, string>, actionPrefix: string, options: ToggleOptions<Context>): ContextPathFunc<Context, CallbackButtonTemplate | undefined> { | ||
const formatFunction: FormatStateFunction<Context> = options.formatState ?? ((_, text, state) => prefixEmoji(text, state)) | ||
return async (context, path) => { | ||
if (options.hide && await options.hide(context)) { | ||
return undefined | ||
} | ||
|
||
const textResult = typeof text === 'function' ? await text(context, path) : text | ||
const state = await options.isSet(context, path) | ||
return { | ||
text: await formatFunction(context, textResult, state, path), | ||
relativePath: actionPrefix + ':' + (state ? 'false' : 'true') | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters