@thekestongroup/ui 0.7.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/Breadcrumb/Breadcrumb.vue.d.ts +0 -1
- package/dist/components/Checkbox/Checkbox.vue.d.ts +2 -0
- package/dist/components/CheckboxGroup/CheckboxGroup.vue.d.ts +45 -0
- package/dist/components/Chip/Chip.vue.d.ts +0 -1
- package/dist/components/Header/Header.vue.d.ts +0 -1
- package/dist/components/InputDate/InputDate.vue.d.ts +1 -1
- package/dist/components/InputTextArea/InputTextArea.vue.d.ts +49 -0
- package/dist/components/LanguageSwitcher/LanguageSwitcher.vue.d.ts +24 -0
- package/dist/components/Notification/NotificationPane.vue.d.ts +1 -4
- package/dist/components/Select/Select.vue.d.ts +0 -1
- package/dist/components/SettingsRow/SettingsRow.vue.d.ts +28 -0
- package/dist/components/Sidebar/Sidebar.vue.d.ts +0 -1
- package/dist/components/Slideover/Slideover.vue.d.ts +0 -1
- package/dist/components/Spinner/Spinner.vue.d.ts +0 -1
- package/dist/components/Steps/Steps.vue.d.ts +20 -0
- package/dist/components/Switch/Switch.vue.d.ts +2 -0
- package/dist/components/Table/Table.vue.d.ts +21 -1
- package/dist/i18n/en.d.ts +146 -0
- package/dist/i18n/index.d.ts +32 -0
- package/dist/index.d.ts +12 -0
- package/dist/keston-ui.js +2189 -1691
- package/package.json +4 -2
- package/src/components/Age/Age.vue +4 -2
- package/src/components/AppShell/AppShell.vue +4 -2
- package/src/components/AvatarGroup/AvatarGroup.vue +4 -2
- package/src/components/Breadcrumb/Breadcrumb.vue +3 -2
- package/src/components/Calendar/Calendar.vue +8 -6
- package/src/components/Card/Card.vue +3 -1
- package/src/components/Checkbox/Checkbox.stories.ts +3 -0
- package/src/components/Checkbox/Checkbox.vue +3 -1
- package/src/components/CheckboxGroup/CheckboxGroup.stories.ts +48 -0
- package/src/components/CheckboxGroup/CheckboxGroup.vue +96 -0
- package/src/components/Chip/Chip.vue +3 -2
- package/src/components/FormField/FormField.vue +3 -1
- package/src/components/Header/Header.vue +6 -5
- package/src/components/Input/Input.vue +4 -2
- package/src/components/InputCode/InputCode.vue +4 -2
- package/src/components/InputDate/InputDate.vue +4 -2
- package/src/components/InputPassword/InputPassword.vue +4 -2
- package/src/components/InputRichText/InputRichText.vue +6 -4
- package/src/components/InputTextArea/InputTextArea.stories.ts +53 -0
- package/src/components/InputTextArea/InputTextArea.vue +89 -0
- package/src/components/LanguageSwitcher/LanguageSwitcher.stories.ts +21 -0
- package/src/components/LanguageSwitcher/LanguageSwitcher.vue +57 -0
- package/src/components/Link/Link.vue +1 -0
- package/src/components/Notification/Notification.vue +3 -1
- package/src/components/Notification/NotificationPane.vue +10 -8
- package/src/components/Notification/Toaster.vue +9 -7
- package/src/components/PageHeader/PageHeader.vue +13 -2
- package/src/components/Progress/Progress.vue +3 -1
- package/src/components/RadioGroup/RadioGroup.vue +3 -1
- package/src/components/Select/Select.vue +6 -5
- package/src/components/SettingsRow/SettingsRow.stories.ts +59 -0
- package/src/components/SettingsRow/SettingsRow.vue +39 -0
- package/src/components/Sidebar/Sidebar.vue +5 -4
- package/src/components/Slideover/Slideover.vue +8 -7
- package/src/components/Spinner/Spinner.vue +4 -3
- package/src/components/StatTile/StatTile.vue +3 -1
- package/src/components/Steps/Steps.stories.ts +38 -0
- package/src/components/Steps/Steps.vue +38 -0
- package/src/components/SummaryList/SummaryList.vue +5 -4
- package/src/components/Switch/Switch.vue +3 -1
- package/src/components/Table/Table.stories.ts +34 -0
- package/src/components/Table/Table.vue +98 -8
- package/src/i18n/en.ts +127 -0
- package/src/i18n/i18n.test.ts +56 -0
- package/src/i18n/index.ts +89 -0
- package/src/index.ts +14 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import { createKestonI18n, en } from './index'
|
|
3
|
+
|
|
4
|
+
// Tests run in Node: stand in for the browser's storage and <html>.
|
|
5
|
+
const store = new Map<string, string>()
|
|
6
|
+
const html = { lang: '' }
|
|
7
|
+
vi.stubGlobal('localStorage', {
|
|
8
|
+
getItem: (k: string) => store.get(k) ?? null,
|
|
9
|
+
setItem: (k: string, v: string) => void store.set(k, v),
|
|
10
|
+
clear: () => store.clear()
|
|
11
|
+
})
|
|
12
|
+
vi.stubGlobal('document', { documentElement: html })
|
|
13
|
+
|
|
14
|
+
describe('createKestonI18n', () => {
|
|
15
|
+
beforeEach(() => store.clear())
|
|
16
|
+
|
|
17
|
+
it('has Keston UI’s words and the app’s own', () => {
|
|
18
|
+
const i18n = createKestonI18n({ messages: { en: { users: { title: 'Users' } } } })
|
|
19
|
+
expect(i18n.global.t('ui.slideover.close')).toBe('Close')
|
|
20
|
+
expect(i18n.global.t('users.title')).toBe('Users')
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('falls back to English for a language that lacks a word', () => {
|
|
24
|
+
const i18n = createKestonI18n({ locale: 'cy', messages: { cy: { users: { title: 'Defnyddwyr' } } } })
|
|
25
|
+
expect(i18n.global.t('users.title')).toBe('Defnyddwyr')
|
|
26
|
+
expect(i18n.global.t('ui.slideover.close')).toBe('Close')
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('remembers the language and sets <html lang>', async () => {
|
|
30
|
+
const i18n = createKestonI18n({ messages: { cy: {} } })
|
|
31
|
+
i18n.global.locale.value = 'cy'
|
|
32
|
+
await Promise.resolve()
|
|
33
|
+
expect(localStorage.getItem('keston-locale')).toBe('cy')
|
|
34
|
+
expect(html.lang).toBe('cy')
|
|
35
|
+
expect(createKestonI18n({ messages: { cy: {} } }).global.locale.value).toBe('cy')
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('ignores a saved language the app no longer has', () => {
|
|
39
|
+
localStorage.setItem('keston-locale', 'fr')
|
|
40
|
+
expect(createKestonI18n().global.locale.value).toBe('en')
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('has a working message for every key', () => {
|
|
44
|
+
const i18n = createKestonI18n()
|
|
45
|
+
const leaves = (node: Record<string, unknown>, path = ''): string[] =>
|
|
46
|
+
Object.entries(node).flatMap(([k, v]) =>
|
|
47
|
+
typeof v === 'object' && v ? leaves(v as Record<string, unknown>, `${path}${k}.`) : [`${path}${k}`]
|
|
48
|
+
)
|
|
49
|
+
const keys = leaves(en)
|
|
50
|
+
expect(keys.length).toBeGreaterThan(50)
|
|
51
|
+
for (const key of keys) {
|
|
52
|
+
const text = i18n.global.t(key, { count: 2, n: 2, current: 1, total: 3, name: 'X', label: 'X', title: 'X', names: 'X', hotkey: 'X', example: 'X', initials: 'X', date: 'X', days: 2 }, 2)
|
|
53
|
+
expect(text, key).not.toBe(key)
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
})
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { watch } from 'vue'
|
|
2
|
+
import { createI18n } from 'vue-i18n'
|
|
3
|
+
import { en } from './en'
|
|
4
|
+
|
|
5
|
+
export { en } from './en'
|
|
6
|
+
export type { UiMessages } from './en'
|
|
7
|
+
|
|
8
|
+
/** Words by language code, then by key (nested objects are fine). */
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
+
export type Messages = Record<string, Record<string, any>>
|
|
11
|
+
|
|
12
|
+
/** Keston UI's own words, by language. Today: English. */
|
|
13
|
+
export const uiMessages: Messages = { en }
|
|
14
|
+
|
|
15
|
+
export interface KestonI18nOptions {
|
|
16
|
+
/**
|
|
17
|
+
* The app's own words, by language code: `{ en: { users: { title: 'Users' } } }`.
|
|
18
|
+
* Keston UI's words are added to each language (the app's win if a key
|
|
19
|
+
* is repeated), so only languages that Keston UI also has — or that the
|
|
20
|
+
* app lists here — are available.
|
|
21
|
+
*/
|
|
22
|
+
messages?: Messages
|
|
23
|
+
/** The language to start in. Default: the one saved last time, else the browser's, else `fallbackLocale`. */
|
|
24
|
+
locale?: string
|
|
25
|
+
/** Used for any word a language is missing. Default `en`. */
|
|
26
|
+
fallbackLocale?: string
|
|
27
|
+
/** Where the chosen language is remembered (localStorage). Default `keston-locale`. */
|
|
28
|
+
storageKey?: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Sets up vue-i18n the way every Keston app should: Keston UI's words plus
|
|
33
|
+
* the app's own, English as the fallback, the language remembered between
|
|
34
|
+
* visits, and `<html lang>` kept in step. Install it with `app.use(i18n)`.
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* const i18n = createKestonI18n({ messages: { en } })
|
|
38
|
+
* createApp(App).use(i18n).mount('#app')
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export function createKestonI18n(options: KestonI18nOptions = {}) {
|
|
42
|
+
const { messages = {}, fallbackLocale = 'en', storageKey = 'keston-locale' } = options
|
|
43
|
+
|
|
44
|
+
const codes = [...new Set([...Object.keys(uiMessages), ...Object.keys(messages)])]
|
|
45
|
+
const merged: Messages = {}
|
|
46
|
+
for (const code of codes) merged[code] = { ...uiMessages[code], ...messages[code] }
|
|
47
|
+
|
|
48
|
+
const i18n = createI18n({
|
|
49
|
+
legacy: false,
|
|
50
|
+
locale: options.locale ?? initialLocale(codes, fallbackLocale, storageKey),
|
|
51
|
+
fallbackLocale,
|
|
52
|
+
messages: merged
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
// Remember the choice, and tell the browser (screen readers pick the
|
|
56
|
+
// voice by it, browsers offer translation by it).
|
|
57
|
+
watch(
|
|
58
|
+
i18n.global.locale,
|
|
59
|
+
(locale) => {
|
|
60
|
+
try {
|
|
61
|
+
localStorage.setItem(storageKey, locale)
|
|
62
|
+
} catch {
|
|
63
|
+
// Private windows can refuse storage; the choice just isn't remembered.
|
|
64
|
+
}
|
|
65
|
+
if (typeof document !== 'undefined') document.documentElement.lang = locale
|
|
66
|
+
},
|
|
67
|
+
{ immediate: true }
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
return i18n
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Saved choice, else the browser's languages in order, else the fallback. */
|
|
74
|
+
function initialLocale(codes: string[], fallback: string, storageKey: string): string {
|
|
75
|
+
try {
|
|
76
|
+
const saved = localStorage.getItem(storageKey)
|
|
77
|
+
if (saved && codes.includes(saved)) return saved
|
|
78
|
+
} catch {
|
|
79
|
+
// No storage: carry on.
|
|
80
|
+
}
|
|
81
|
+
if (typeof navigator !== 'undefined') {
|
|
82
|
+
for (const wanted of navigator.languages ?? []) {
|
|
83
|
+
const exact = codes.find((c) => c.toLowerCase() === wanted.toLowerCase())
|
|
84
|
+
const base = codes.find((c) => c.toLowerCase() === wanted.split('-')[0]!.toLowerCase())
|
|
85
|
+
if (exact ?? base) return (exact ?? base)!
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return fallback
|
|
89
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -12,6 +12,8 @@ export { default as Header } from './components/Header/Header.vue'
|
|
|
12
12
|
export type { HeaderProps, HeaderUser, HeaderUserAction } from './components/Header/Header.vue'
|
|
13
13
|
export { default as PageHeader } from './components/PageHeader/PageHeader.vue'
|
|
14
14
|
export type { PageHeaderAction, PageHeaderProps } from './components/PageHeader/PageHeader.vue'
|
|
15
|
+
export { default as SettingsRow } from './components/SettingsRow/SettingsRow.vue'
|
|
16
|
+
export type { SettingsRowProps } from './components/SettingsRow/SettingsRow.vue'
|
|
15
17
|
|
|
16
18
|
// Element
|
|
17
19
|
export { default as Avatar } from './components/Avatar/Avatar.vue'
|
|
@@ -58,6 +60,12 @@ export { default as Switch } from './components/Switch/Switch.vue'
|
|
|
58
60
|
export type { SwitchProps } from './components/Switch/Switch.vue'
|
|
59
61
|
export { default as Calendar } from './components/Calendar/Calendar.vue'
|
|
60
62
|
export type { CalendarProps } from './components/Calendar/Calendar.vue'
|
|
63
|
+
export { default as InputTextArea } from './components/InputTextArea/InputTextArea.vue'
|
|
64
|
+
export type { InputTextAreaProps } from './components/InputTextArea/InputTextArea.vue'
|
|
65
|
+
export { default as CheckboxGroup } from './components/CheckboxGroup/CheckboxGroup.vue'
|
|
66
|
+
export type { CheckboxGroupProps, CheckboxOption, CheckboxGroupValue } from './components/CheckboxGroup/CheckboxGroup.vue'
|
|
67
|
+
export { default as LanguageSwitcher } from './components/LanguageSwitcher/LanguageSwitcher.vue'
|
|
68
|
+
export type { LanguageSwitcherProps } from './components/LanguageSwitcher/LanguageSwitcher.vue'
|
|
61
69
|
|
|
62
70
|
// Data
|
|
63
71
|
export { default as Table } from './components/Table/Table.vue'
|
|
@@ -85,6 +93,8 @@ export type { SidebarProps, SidebarItem, SidebarSection } from './components/Sid
|
|
|
85
93
|
export { sidebarTrail } from './components/Sidebar/sidebar'
|
|
86
94
|
export { default as Link } from './components/Link/Link.vue'
|
|
87
95
|
export type { LinkProps } from './components/Link/Link.vue'
|
|
96
|
+
export { default as Steps } from './components/Steps/Steps.vue'
|
|
97
|
+
export type { StepsProps } from './components/Steps/Steps.vue'
|
|
88
98
|
|
|
89
99
|
// Overlay
|
|
90
100
|
export { default as Popover } from './components/Popover/Popover.vue'
|
|
@@ -115,6 +125,10 @@ export type {
|
|
|
115
125
|
ToastOptions
|
|
116
126
|
} from './components/Notification/notifications'
|
|
117
127
|
|
|
128
|
+
// Languages
|
|
129
|
+
export { createKestonI18n, uiMessages, en } from './i18n'
|
|
130
|
+
export type { KestonI18nOptions, Messages, UiMessages } from './i18n'
|
|
131
|
+
|
|
118
132
|
// Helpers
|
|
119
133
|
export { age, daysToBirthday, formatDate, formatDateTime, toDate } from './lib/dates'
|
|
120
134
|
export type { DateInput } from './lib/dates'
|