Skip to content

Commit

Permalink
Merge pull request #77 from raycharius/v2.4.0
Browse files Browse the repository at this point in the history
✨ Add support for focus_on_load and conditional helpers
  • Loading branch information
raycharius authored Nov 30, 2021
2 parents c532493 + 1fed766 commit 9272278
Show file tree
Hide file tree
Showing 229 changed files with 1,128 additions and 866 deletions.
55 changes: 50 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

* Declarative [SwiftUI](https://developer.apple.com/xcode/swiftui/) inspired syntax.
* Commonly-used UI components, such as a `Paginator` and `Accordion`.
* Inline conditional helper functions for declaratively appending or omitting UI content.
* The ability to build more complex flows using loops and conditionals.
* A `printPreviewURL()` method that outputs a link to preview your UI on Slack's [Block Kit Builder website](https://app.slack.com/block-kit-builder) for easier prototyping.
* A set of helper functions for formatting text with Slack's markdown standard.
Expand Down Expand Up @@ -214,12 +215,10 @@ Both of these examples render the message below. And the best part? It only took

### Creating a Simple Modal

Let's take a look at how modals are created. Here we'll also take a look at working with Bits and with loops, by adding options with the `Array.map()` method.

You'll noticed that we've added an inline condition that returns an initial option only if one has been passed into the function. This is because all the setter methods prune values of `undefined`, uncovering opportunities for inline logic.
Let's take a look at how modals are created. Here we'll also take a look at working with Bits and with loops, by adding options with the `Array.map()` method.

```javascript
import { Modal, Blocks, Elements, Bits } from 'slack-block-builder';
import { Modal, Blocks, Elements, Bits, setIfTruthy } from 'slack-block-builder';

export default ({ menuOptions, selected }) =>
Modal({ title: 'PizzaMate', submit: 'Get Fed' })
Expand All @@ -240,7 +239,7 @@ export default ({ menuOptions, selected }) =>
.actionId('item')
.options(menuOptions
.map((item) => Bits.Option({ text: item.name, value: item.id })))
.initialOption(selected && Bits.Option({ text: selected.name, value: selected.id }))))
.initialOption(setIfTruthy(selected, Bits.Option({ text: selected.name, value: selected.id })))))
.buildToJSON();
```

Expand Down Expand Up @@ -383,6 +382,52 @@ const unfurl = ({ channel, ts, url }) => client.chat.unfurl({
.catch((error) => console.log(error));
```

### Working With Inline Conditionals

There are a few helper functions available to make it easy to work with inline conditionals within your UI source code.

They can be imported separately:

```javascript
import { setIfTruthy, omitIfTruthy, setIfFalsy, omitIfFalsy } from 'slack-block-builder';
```

Or as a part of the `conditionals` object:

```javascript
import { conditionals } from 'slack-block-builder';
```

Each function accepts to arguments – the first being a value that is evaluated whether it is either `null`, `undefined`, or `false`, and the second being the value to set or omit:

```javascript
import { Modal, Blocks, Elements, Bits, setIfTruthy } from 'slack-block-builder';

export default ({ groups, selectedGroup, selectedGroupMembers }) => Modal()
.title('Edit Groups')
.callbackId('submit-edit-groups')
.blocks(
Blocks.Section({ text: 'Hello! Need to edit some groups?'}),
Blocks.Input({ label: 'Select a group to get started' })
.dispatchAction()
.element(
Elements.StaticSelect({ placeholder: 'Select a group...' })
.actionId('selectedGroup')
.options(groups
.map(({ name, id }) => Bits.Option({ text: name, value: id })))),
setIfTruthy(selectedGroup, [
Blocks.Input({ label: 'Current group members' })
.element(
Elements.UserMultiSelect({ placeholder: 'Select members...' })
.actionId('groupMembers')
.initialUsers(selectedGroupMembers))
]))
.submit(setIfTruthy(selectedGroup, 'Save Changes'))
.buildToJSON();
```

These functions essentially return either the value passed into as the second argument or `undefined`, depending on the condition. Please note that falsy is defined as `null`, `undefined`, or `false`. To avoid side effects, values such as `0` or `''` are not considered to be falsy.

### Markdown Helpers

Often you'll find that you need to format text in your messages and modals. *Block Builder* has helper functions available to simply that process. They are available both as members of the `Md` object and as top-level imports. You can find the full list of functions on the [Block Builder doc site](https://blockbuilder.dev):
Expand Down
2 changes: 1 addition & 1 deletion docs-generator/getters/get-method-data-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface MethodData {

export default (): MethodData[] => {
const basePath = './src';
const folder = 'methods';
const folder = 'internal/methods';

return getFilePathsFromFolder(basePath, folder, ['index.ts', 'required-methods.ts'])
.map((file) => {
Expand Down
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
* **Other**
* [Block Collection](other/block-collection.md "Block Builder – Block Collection – Maintainable JavaScript Code for Slack Block Kit")
* [Attachment Collection](other/attachment-collection.md "Block Builder – Attachment Collection – Maintainable JavaScript Code for Slack Block Kit")
* [Conditionals](other/conditionals.md "Block Builder – Conditionals – Maintainable JavaScript Code for Slack Block Kit")
* [Markdown](other/markdown.md "Block Builder – Markdown – Maintainable JavaScript Code for Slack Block Kit")

* **About**
Expand Down
2 changes: 1 addition & 1 deletion docs/bits/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The `Bits` object in **Block Builder** contains various bits and pieces from the

### Accessing Bits in Block Builder

All of the composition objects provided by the [Slack Block Kit](https://api.slack.com/block-kit) framework are supported in **Block Builder** and most are accessed through the `Bits` object, which is a top-level import. The others (Markdown, Plain-Text, Filter) are created in the background when a view is compiled.
All of the composition objects provided by the [Slack Block Kit](https://api.slack.com/block-kit) framework are supported in **Block Builder** and most are accessed through the `Bits` object, which is a top-level import. The others (Markdown, Plain-Text, FilterType) are created in the background when a view is compiled.

The `Bits` object also contains a method called `Attachment()` which builds out attachments for messages.

Expand Down
5 changes: 5 additions & 0 deletions docs/elements/channel-multi-select.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ ChannelMultiSelectBuilder.initialChannels([string1[, ...[, stringN]]);
Pre-populates the menu with selected, default channels.
```javascript
ChannelMultiSelectBuilder.focusOnLoad(boolean?);
```
Sets an element to have auto focus on opening the view Defaults to `true`.
```javascript
ChannelMultiSelectBuilder.actionId(string);
```
Expand Down
5 changes: 5 additions & 0 deletions docs/elements/channel-select.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ Each instance of the `ChannelSelectBuilder` object has chainable setter methods
All setter methods return `this`, the instance of `ChannelSelectBuilder` on which it is called.
```javascript
ChannelSelectBuilder.focusOnLoad(boolean?);
```
Sets an element to have auto focus on opening the view Defaults to `true`.
```javascript
ChannelSelectBuilder.responseUrlEnabled(boolean?);
```
Expand Down
5 changes: 5 additions & 0 deletions docs/elements/checkboxes.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ CheckboxesBuilder.options([Option1[, ...[, OptionN]]);
Adds options to the select or multi-select menu.
```javascript
CheckboxesBuilder.focusOnLoad(boolean?);
```
Sets an element to have auto focus on opening the view Defaults to `true`.
```javascript
CheckboxesBuilder.actionId(string);
```
Expand Down
5 changes: 5 additions & 0 deletions docs/elements/conversation-multi-select.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ ConversationMultiSelectBuilder.excludeBotUsers(boolean?);
Excludes conversations with bot users from the menu's options. Defaults to `true`.
```javascript
ConversationMultiSelectBuilder.focusOnLoad(boolean?);
```
Sets an element to have auto focus on opening the view Defaults to `true`.
```javascript
ConversationMultiSelectBuilder.actionId(string);
```
Expand Down
5 changes: 5 additions & 0 deletions docs/elements/conversation-select.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ ConversationSelectBuilder.excludeBotUsers(boolean?);
Excludes conversations with bot users from the menu's options. Defaults to `true`.
```javascript
ConversationSelectBuilder.focusOnLoad(boolean?);
```
Sets an element to have auto focus on opening the view Defaults to `true`.
```javascript
ConversationSelectBuilder.responseUrlEnabled(boolean?);
```
Expand Down
5 changes: 5 additions & 0 deletions docs/elements/datepicker.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ Each instance of the `DatePickerBuilder` object has chainable setter methods for
All setter methods return `this`, the instance of `DatePickerBuilder` on which it is called.
```javascript
DatePickerBuilder.focusOnLoad(boolean?);
```
Sets an element to have auto focus on opening the view Defaults to `true`.
```javascript
DatePickerBuilder.actionId(string);
```
Expand Down
5 changes: 5 additions & 0 deletions docs/elements/external-multi-select.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ ExternalMultiSelectBuilder.initialOptions([Option1[, ...[, OptionN]]);
Pre-populates the menu or checkbox input with selected, default options.
```javascript
ExternalMultiSelectBuilder.focusOnLoad(boolean?);
```
Sets an element to have auto focus on opening the view Defaults to `true`.
```javascript
ExternalMultiSelectBuilder.actionId(string);
```
Expand Down
5 changes: 5 additions & 0 deletions docs/elements/external-select.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ Each instance of the `ExternalSelectBuilder` object has chainable setter methods
All setter methods return `this`, the instance of `ExternalSelectBuilder` on which it is called.
```javascript
ExternalSelectBuilder.focusOnLoad(boolean?);
```
Sets an element to have auto focus on opening the view Defaults to `true`.
```javascript
ExternalSelectBuilder.actionId(string);
```
Expand Down
5 changes: 5 additions & 0 deletions docs/elements/radio-buttons.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ RadioButtonsBuilder.options([Option1[, ...[, OptionN]]);
Adds options to the select or multi-select menu.
```javascript
RadioButtonsBuilder.focusOnLoad(boolean?);
```
Sets an element to have auto focus on opening the view Defaults to `true`.
```javascript
RadioButtonsBuilder.actionId(string);
```
Expand Down
5 changes: 5 additions & 0 deletions docs/elements/static-multi-select.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ StaticMultiSelectBuilder.options([Option1[, ...[, OptionN]]);
Adds options to the select or multi-select menu.
```javascript
StaticMultiSelectBuilder.focusOnLoad(boolean?);
```
Sets an element to have auto focus on opening the view Defaults to `true`.
```javascript
StaticMultiSelectBuilder.actionId(string);
```
Expand Down
5 changes: 5 additions & 0 deletions docs/elements/static-select.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ StaticSelectBuilder.options([Option1[, ...[, OptionN]]);
Adds options to the select or multi-select menu.
```javascript
StaticSelectBuilder.focusOnLoad(boolean?);
```
Sets an element to have auto focus on opening the view Defaults to `true`.
```javascript
StaticSelectBuilder.actionId(string);
```
Expand Down
5 changes: 5 additions & 0 deletions docs/elements/text-input.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ TextInputBuilder.dispatchActionOnEnterPressed(boolean?);
Instructs the Slack API to dispatch an interaction payload to your app when the user presses the enter key while the input is in focus. Defaults to `true`.
```javascript
TextInputBuilder.focusOnLoad(boolean?);
```
Sets an element to have auto focus on opening the view Defaults to `true`.
```javascript
TextInputBuilder.multiline(boolean?);
```
Expand Down
5 changes: 5 additions & 0 deletions docs/elements/timepicker.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ Each instance of the `TimePickerBuilder` object has chainable setter methods for
All setter methods return `this`, the instance of `TimePickerBuilder` on which it is called.
```javascript
TimePickerBuilder.focusOnLoad(boolean?);
```
Sets an element to have auto focus on opening the view Defaults to `true`.
```javascript
TimePickerBuilder.actionId(string);
```
Expand Down
5 changes: 5 additions & 0 deletions docs/elements/user-multi-select.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ UserMultiSelectBuilder.initialUsers([string1[, ...[, stringN]]);
Pre-populates the menu with selected, default users.
```javascript
UserMultiSelectBuilder.focusOnLoad(boolean?);
```
Sets an element to have auto focus on opening the view Defaults to `true`.
```javascript
UserMultiSelectBuilder.actionId(string);
```
Expand Down
5 changes: 5 additions & 0 deletions docs/elements/user-select.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ Each instance of the `UserSelectBuilder` object has chainable setter methods for
All setter methods return `this`, the instance of `UserSelectBuilder` on which it is called.
```javascript
UserSelectBuilder.focusOnLoad(boolean?);
```
Sets an element to have auto focus on opening the view Defaults to `true`.
```javascript
UserSelectBuilder.actionId(string);
```
Expand Down
43 changes: 43 additions & 0 deletions docs/other/conditionals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Conditionals

### Accessing Inline Conditional Helpers

The helpers for inline conditionals can be imported as a part of the `conditionals` object, or as top-level imports:

```javascript
import { conditionals } from 'slack-block-builder';
```

```javascript
import { setIfTruthy, omitIfTruthy, setIfFalsy, omitIfFalsy } from 'slack-block-builder';
```

Each function accepts to arguments – the first being a value that is evaluated whether it is either `null`, `undefined`, or `false`, and the second being the value to set or omit:

```javascript
import { Modal, Blocks, Elements, Bits, setIfTruthy } from 'slack-block-builder';

export default ({ groups, selectedGroup, selectedGroupMembers }) => Modal()
.title('Edit Groups')
.callbackId('submit-edit-groups')
.blocks(
Blocks.Section({ text: 'Hello! Need to edit some groups?'}),
Blocks.Input({ label: 'Select a group to get started' })
.dispatchAction()
.element(
Elements.StaticSelect({ placeholder: 'Select a group...' })
.actionId('selectedGroup')
.options(groups
.map(({ name, id }) => Bits.Option({ text: name, value: id })))),
setIfTruthy(selectedGroup, [
Blocks.Input({ label: 'Current group members' })
.element(
Elements.UserMultiSelect({ placeholder: 'Select members...' })
.actionId('groupMembers')
.initialUsers(selectedGroupMembers))
]))
.submit(setIfTruthy(selectedGroup, 'Save Changes'))
.buildToJSON();
```

These functions essentially return either the value passed into as the second argument or `undefined`, depending on the condition. Please note that falsy is defined as `null`, `undefined`, or `false`. To avoid side effects, values such as `0` or `''` are not considered to be falsy.
Loading

0 comments on commit 9272278

Please sign in to comment.