Skip to content

Commit 355fbd1

Browse files
authored
Merge pull request #15623 from Budibase/feat/pc-ts-conversions
Typescript conversion for some BBUI components
2 parents a4eb564 + 5754412 commit 355fbd1

29 files changed

+262
-258
lines changed

packages/bbui/src/ClearButton/ClearButton.svelte

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<script>
2-
export let small = false
3-
export let disabled
1+
<script lang="ts">
2+
export let small: boolean = false
3+
export let disabled: boolean = false
44
</script>
55

66
<button
+11-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
<script>
1+
<script lang="ts">
22
import Field from "./Field.svelte"
33
import Checkbox from "./Core/Checkbox.svelte"
44
import { createEventDispatcher } from "svelte"
55
6-
export let value = null
7-
export let label = null
8-
export let labelPosition = "above"
9-
export let text = null
10-
export let disabled = false
11-
export let error = null
12-
export let size = "M"
13-
export let helpText = null
6+
export let value: boolean | undefined = undefined
7+
export let label: string | undefined = undefined
8+
export let labelPosition: "above" | "below" = "above"
9+
export let text: string | undefined = undefined
10+
export let disabled: boolean = false
11+
export let error: string | undefined = undefined
12+
export let size: "S" | "M" | "L" | "XL" = "M"
13+
export let helpText: string | undefined = undefined
1414
1515
const dispatch = createEventDispatcher()
16-
const onChange = e => {
16+
const onChange = (e: CustomEvent<boolean>) => {
1717
value = e.detail
1818
dispatch("change", e.detail)
1919
}
2020
</script>
2121

2222
<Field {helpText} {label} {labelPosition} {error}>
23-
<Checkbox {error} {disabled} {text} {value} {size} on:change={onChange} />
23+
<Checkbox {disabled} {text} {value} {size} on:change={onChange} />
2424
</Field>

packages/bbui/src/Link/Link.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
export let secondary: boolean = false
1111
export let overBackground: boolean = false
1212
export let target: string | undefined = undefined
13-
export let download: boolean | undefined = undefined
13+
export let download: string | undefined = undefined
1414
export let disabled: boolean = false
1515
export let tooltip: string | null = null
1616

packages/bbui/src/SideNavigation/Item.svelte

-63
This file was deleted.

packages/bbui/src/SideNavigation/Navigation.svelte

-13
This file was deleted.

packages/bbui/src/Stores/banner.js renamed to packages/bbui/src/Stores/banner.ts

+26-11
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,26 @@ export const BANNER_TYPES = {
77
WARNING: "warning",
88
}
99

10+
interface BannerConfig {
11+
message?: string
12+
type?: string
13+
extraButtonText?: string
14+
extraButtonAction?: () => void
15+
onChange?: () => void
16+
}
17+
18+
interface DefaultConfig {
19+
messages: BannerConfig[]
20+
}
21+
1022
export function createBannerStore() {
11-
const DEFAULT_CONFIG = {
23+
const DEFAULT_CONFIG: DefaultConfig = {
1224
messages: [],
1325
}
1426

15-
const banner = writable(DEFAULT_CONFIG)
27+
const banner = writable<DefaultConfig>(DEFAULT_CONFIG)
1628

17-
const show = async (
18-
// eslint-disable-next-line
19-
config = { message, type, extraButtonText, extraButtonAction, onChange }
20-
) => {
29+
const show = async (config: BannerConfig = {}) => {
2130
banner.update(store => {
2231
return {
2332
...store,
@@ -27,7 +36,7 @@ export function createBannerStore() {
2736
}
2837

2938
const showStatus = async () => {
30-
const config = {
39+
const config: BannerConfig = {
3140
message: "Some systems are experiencing issues",
3241
type: BANNER_TYPES.NEGATIVE,
3342
extraButtonText: "View Status",
@@ -37,18 +46,24 @@ export function createBannerStore() {
3746
await queue([config])
3847
}
3948

40-
const queue = async entries => {
41-
const priority = {
49+
const queue = async (entries: Array<BannerConfig>) => {
50+
const priority: Record<string, number> = {
4251
[BANNER_TYPES.NEGATIVE]: 0,
4352
[BANNER_TYPES.WARNING]: 1,
4453
[BANNER_TYPES.INFO]: 2,
4554
}
4655
banner.update(store => {
4756
const sorted = [...store.messages, ...entries].sort((a, b) => {
48-
if (priority[a.type] == priority[b.type]) {
57+
if (
58+
priority[a.type as keyof typeof priority] ===
59+
priority[b.type as keyof typeof priority]
60+
) {
4961
return 0
5062
}
51-
return priority[a.type] < priority[b.type] ? -1 : 1
63+
return priority[a.type as keyof typeof priority] <
64+
priority[b.type as keyof typeof priority]
65+
? -1
66+
: 1
5267
})
5368
return {
5469
...store,

packages/bbui/src/Stores/notifications.js renamed to packages/bbui/src/Stores/notifications.ts

+31-10
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,21 @@ import { writable } from "svelte/store"
22

33
const NOTIFICATION_TIMEOUT = 3000
44

5+
interface Notification {
6+
id: string
7+
type: string
8+
message: string
9+
icon: string
10+
dismissable: boolean
11+
action: (() => void) | null
12+
actionMessage: string | null
13+
wide: boolean
14+
dismissTimeout: number
15+
}
16+
517
export const createNotificationStore = () => {
6-
const timeoutIds = new Set()
7-
const _notifications = writable([], () => {
18+
const timeoutIds = new Set<ReturnType<typeof setTimeout>>()
19+
const _notifications = writable<Notification[]>([], () => {
820
return () => {
921
// clear all the timers
1022
timeoutIds.forEach(timeoutId => {
@@ -21,7 +33,7 @@ export const createNotificationStore = () => {
2133
}
2234

2335
const send = (
24-
message,
36+
message: string,
2537
{
2638
type = "default",
2739
icon = "",
@@ -30,7 +42,15 @@ export const createNotificationStore = () => {
3042
actionMessage = null,
3143
wide = false,
3244
dismissTimeout = NOTIFICATION_TIMEOUT,
33-
}
45+
}: {
46+
type?: string
47+
icon?: string
48+
autoDismiss?: boolean
49+
action?: (() => void) | null
50+
actionMessage?: string | null
51+
wide?: boolean
52+
dismissTimeout?: number
53+
} = {}
3454
) => {
3555
if (block) {
3656
return
@@ -60,7 +80,7 @@ export const createNotificationStore = () => {
6080
}
6181
}
6282

63-
const dismissNotification = id => {
83+
const dismissNotification = (id: string) => {
6484
_notifications.update(state => {
6585
return state.filter(n => n.id !== id)
6686
})
@@ -71,17 +91,18 @@ export const createNotificationStore = () => {
7191
return {
7292
subscribe,
7393
send,
74-
info: msg => send(msg, { type: "info", icon: "Info" }),
75-
error: msg =>
94+
info: (msg: string) => send(msg, { type: "info", icon: "Info" }),
95+
error: (msg: string) =>
7696
send(msg, { type: "error", icon: "Alert", autoDismiss: false }),
77-
warning: msg => send(msg, { type: "warning", icon: "Alert" }),
78-
success: msg => send(msg, { type: "success", icon: "CheckmarkCircle" }),
97+
warning: (msg: string) => send(msg, { type: "warning", icon: "Alert" }),
98+
success: (msg: string) =>
99+
send(msg, { type: "success", icon: "CheckmarkCircle" }),
79100
blockNotifications,
80101
dismiss: dismissNotification,
81102
}
82103
}
83104

84-
function id() {
105+
function id(): string {
85106
return "_" + Math.random().toString(36).slice(2, 9)
86107
}
87108

packages/bbui/src/Table/ArrayRenderer.svelte

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<script>
1+
<script lang="ts">
22
import "@spectrum-css/label/dist/index-vars.css"
33
import Badge from "../Badge/Badge.svelte"
44
5-
export let value
5+
export let value: string | string[]
66
77
const displayLimit = 5
88

packages/bbui/src/Table/AttachmentRenderer.svelte

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
<script>
1+
<script lang="ts">
22
import Link from "../Link/Link.svelte"
33
4-
export let value
4+
export let value: { name: string; url: string; extension: string }[]
55
66
const displayLimit = 5
77
$: attachments = value?.slice(0, displayLimit) ?? []
88
$: leftover = (value?.length ?? 0) - attachments.length
99
10-
const imageExtensions = ["png", "tiff", "gif", "raw", "jpg", "jpeg"]
11-
const isImage = extension => {
12-
return imageExtensions.includes(extension?.toLowerCase())
10+
const imageExtensions: string[] = ["png", "tiff", "gif", "raw", "jpg", "jpeg"]
11+
const isImage = (extension: string | undefined): boolean => {
12+
return imageExtensions.includes(extension?.toLowerCase() ?? "")
1313
}
1414
</script>
1515

packages/bbui/src/Table/BoldRenderer.svelte

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<script>
2-
export let value
1+
<script lang="ts">
2+
export let value: string
33
</script>
44

55
<div class="bold">{value}</div>

packages/bbui/src/Table/BooleanRenderer.svelte

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<script>
1+
<script lang="ts">
22
import "@spectrum-css/checkbox/dist/index-vars.css"
33
4-
export let value
4+
export let value: boolean
55
</script>
66

77
<label

packages/bbui/src/Table/CellRenderer.svelte

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<script>
1+
<script lang="ts">
22
import StringRenderer from "./StringRenderer.svelte"
33
import BooleanRenderer from "./BooleanRenderer.svelte"
44
import DateTimeRenderer from "./DateTimeRenderer.svelte"
@@ -8,14 +8,14 @@
88
import InternalRenderer from "./InternalRenderer.svelte"
99
import { processStringSync } from "@budibase/string-templates"
1010
11-
export let row
12-
export let schema
13-
export let value
14-
export let customRenderers = []
15-
export let snippets
11+
export let row: Record<string, any>
12+
export let schema: Record<string, any>
13+
export let value: Record<string, any>
14+
export let customRenderers: { column: string; component: any }[] = []
15+
export let snippets: any
1616
17-
let renderer
18-
const typeMap = {
17+
let renderer: any
18+
const typeMap: Record<string, any> = {
1919
boolean: BooleanRenderer,
2020
datetime: DateTimeRenderer,
2121
link: RelationshipRenderer,
@@ -33,15 +33,15 @@
3333
$: renderer = customRenderer?.component ?? typeMap[type] ?? StringRenderer
3434
$: cellValue = getCellValue(value, schema.template)
3535
36-
const getType = schema => {
36+
const getType = (schema: any): string => {
3737
// Use a string renderer for dates if we use a custom template
3838
if (schema?.type === "datetime" && schema?.template) {
3939
return "string"
4040
}
4141
return schema?.type || "string"
4242
}
4343
44-
const getCellValue = (value, template) => {
44+
const getCellValue = (value: any, template: string | undefined): any => {
4545
if (!template) {
4646
return value
4747
}

packages/bbui/src/Table/CodeRenderer.svelte

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<script>
2-
export let value
1+
<script lang="ts">
2+
export let value: string
33
</script>
44

55
<code>{value}</code>

0 commit comments

Comments
 (0)