diff --git a/block-kit/README.md b/block-kit/README.md index 0ccfb5d..015289f 100644 --- a/block-kit/README.md +++ b/block-kit/README.md @@ -13,6 +13,7 @@ Read the [docs](https://docs.slack.dev/block-kit/) to learn concepts behind thes - **[Carousel](https://docs.slack.dev/reference/block-kit/blocks/carousel-block)**: Displays related card blocks in a horizontally-scrolling container. [Implementation](./src/main/java/blocks/Carousel.java). - **[Card](https://docs.slack.dev/reference/block-kit/blocks/card-block)**: Displays content in a card. [Implementation](./src/main/java/blocks/Card.java). - **[Context](https://docs.slack.dev/reference/block-kit/blocks/context-block)**: Provides contextual info, which can include both images and text. [Implementation](./src/main/java/blocks/Context.java). +- **[Data table](https://docs.slack.dev/reference/block-kit/blocks/data-table-block)**: Displays rich tables that support pagination, sorting, filtering, and interactivity. [Implementation](./src/main/java/blocks/DataTable.java). - **[Divider](https://docs.slack.dev/reference/block-kit/blocks/divider-block)**: Visually separates pieces of info inside of a message. [Implementation](./src/main/java/blocks/Divider.java). - **[File](https://docs.slack.dev/reference/block-kit/blocks/file-block)**: Displays info about remote files. [Implementation](./src/main/java/blocks/File.java). - **[Header](https://docs.slack.dev/reference/block-kit/blocks/header-block)**: Displays a larger-sized text. [Implementation](./src/main/java/blocks/Header.java). diff --git a/block-kit/src/main/java/blocks/DataTable.java b/block-kit/src/main/java/blocks/DataTable.java new file mode 100644 index 0000000..3e8825a --- /dev/null +++ b/block-kit/src/main/java/blocks/DataTable.java @@ -0,0 +1,65 @@ +package blocks; + +import com.slack.api.model.block.Blocks; +import com.slack.api.model.block.DataTableBlock; +import com.slack.api.model.block.composition.BlockCompositions; +import com.slack.api.model.block.element.RichTextSectionElement; +import com.slack.api.model.block.element.RichTextSectionElement.TextStyle; +import java.util.List; + +/** + * Displays rich tables that support pagination, sorting, filtering, and interactivity. + * {@link https://docs.slack.dev/reference/block-kit/blocks/data-table-block/} + */ +public class DataTable { + /** + * A sample data table block. + */ + public static DataTableBlock example01() { + DataTableBlock block = Blocks.dataTable(t -> t.caption("A Fabulous Table") + .rows(List.of( + List.of( + BlockCompositions.rawText("Name"), + BlockCompositions.rawText("Department"), + BlockCompositions.rawText("Badge")), + List.of( + BlockCompositions.rawText("Data Refinement Department"), + BlockCompositions.rawText("MDR"), + Blocks.richText(rt -> rt.elements(List.of(RichTextSectionElement.builder() + .elements(List.of(RichTextSectionElement.Text.builder() + .text("Blue") + .style(TextStyle.builder() + .bold(true) + .build()) + .build())) + .build())))), + List.of( + BlockCompositions.rawText("Art Sourcing Department"), + BlockCompositions.rawText("O&D"), + Blocks.richText(rt -> rt.elements(List.of(RichTextSectionElement.builder() + .elements(List.of( + RichTextSectionElement.Text.builder() + .text("Green") + .build(), + RichTextSectionElement.Text.builder() + .text("review") + .style( + TextStyle.builder() + .italic(true) + .build()) + .build())) + .build())))), + List.of( + BlockCompositions.rawText("Wellness Department"), + BlockCompositions.rawText("Wellness Center"), + Blocks.richText(rt -> rt.elements(List.of(RichTextSectionElement.builder() + .elements(List.of(RichTextSectionElement.Text.builder() + .text("Limited") + .style(TextStyle.builder() + .bold(true) + .build()) + .build())) + .build()))))))); + return block; + } +} diff --git a/block-kit/src/test/java/blocks/DataTableTest.java b/block-kit/src/test/java/blocks/DataTableTest.java new file mode 100644 index 0000000..31e2cce --- /dev/null +++ b/block-kit/src/test/java/blocks/DataTableTest.java @@ -0,0 +1,145 @@ +package blocks; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.google.gson.JsonParser; +import com.slack.api.model.block.DataTableBlock; +import com.slack.api.util.json.GsonFactory; +import org.junit.jupiter.api.Test; + +public class DataTableTest { + @Test + public void testExample01() { + DataTableBlock block = DataTable.example01(); + String actual = GsonFactory.createSnakeCase().toJson(block); + String expected = """ + { + "type": "data_table", + "caption": "A Fabulous Table", + "rows": [ + [ + { + "type": "raw_text", + "text": "Name" + }, + { + "type": "raw_text", + "text": "Department" + }, + { + "type": "raw_text", + "text": "Badge" + } + ], + [ + { + "type": "raw_text", + "text": "Data Refinement Department" + }, + { + "type": "raw_text", + "text": "MDR" + }, + { + "type": "rich_text", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Blue", + "style": { + "bold": true, + "italic": false, + "strike": false, + "highlight": false, + "client_highlight": false, + "underline": false, + "unlink": false, + "code": false + } + } + ] + } + ] + } + ], + [ + { + "type": "raw_text", + "text": "Art Sourcing Department" + }, + { + "type": "raw_text", + "text": "O&D" + }, + { + "type": "rich_text", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Green" + }, + { + "type": "text", + "text": "review", + "style": { + "bold": false, + "italic": true, + "strike": false, + "highlight": false, + "client_highlight": false, + "underline": false, + "unlink": false, + "code": false + } + } + ] + } + ] + } + ], + [ + { + "type": "raw_text", + "text": "Wellness Department" + }, + { + "type": "raw_text", + "text": "Wellness Center" + }, + { + "type": "rich_text", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "Limited", + "style": { + "bold": true, + "italic": false, + "strike": false, + "highlight": false, + "client_highlight": false, + "underline": false, + "unlink": false, + "code": false + } + } + ] + } + ] + } + ] + ] + } + """; + assertEquals(JsonParser.parseString(expected), JsonParser.parseString(actual)); + } +}