-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into chore/update-storybook
- Loading branch information
Showing
44 changed files
with
2,854 additions
and
667 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
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
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 |
---|---|---|
@@ -1,20 +1,40 @@ | ||
import { fetchI18nBundle, getI18nBundle } from '@ui5/webcomponents-base/dist/i18nBundle'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
type TextWithDefault = { key: string; defaultText: string }; | ||
type TextWithPlaceholders = [TextWithDefault, ...string[]]; | ||
|
||
interface I18nBundle { | ||
getText: (textObj: { key: string; defaultText: string }, ...args: any) => string; | ||
getText: (textObj: TextWithDefault, ...args: any) => string; | ||
} | ||
|
||
export const useI18nBundle = (bundleName): I18nBundle => { | ||
const [bundle, setBundle] = useState(getI18nBundle(bundleName)); | ||
const resolveTranslations = (bundle, texts) => { | ||
return texts.map((text) => { | ||
if (Array.isArray(text)) { | ||
const [key, ...replacements] = text; | ||
return bundle.getText(key, replacements); | ||
} | ||
return bundle.getText(text); | ||
}); | ||
}; | ||
|
||
export const useI18nText = (bundleName: string, ...texts: (TextWithDefault | TextWithPlaceholders)[]): string[] => { | ||
const i18nBundle: I18nBundle = getI18nBundle(bundleName); | ||
const [translations, setTranslations] = useState(resolveTranslations(i18nBundle, texts)); | ||
|
||
useEffect(() => { | ||
const fetchAndLoadBundle = async () => { | ||
await fetchI18nBundle(bundleName); | ||
setBundle(getI18nBundle(bundleName)); | ||
setTranslations((prev) => { | ||
const next = resolveTranslations(i18nBundle, texts); | ||
if (prev.length === next.length && prev.every((translation, index) => next[index] === translation)) { | ||
return prev; | ||
} | ||
return next; | ||
}); | ||
}; | ||
fetchAndLoadBundle(); | ||
}, []); | ||
}, [fetchI18nBundle, bundleName, texts]); | ||
|
||
return bundle; | ||
return translations; | ||
}; |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { useConsolidatedRef } from '../hooks/useConsolidatedRef'; | ||
import { useI18nBundle } from '../hooks/useI18nBundle'; | ||
import { useI18nText } from '../hooks/useI18nBundle'; | ||
import { usePassThroughHtmlProps } from '../hooks/usePassThroughHtmlProps'; | ||
import { useViewportRange } from '../hooks/useViewportRange'; | ||
|
||
export { useConsolidatedRef, usePassThroughHtmlProps, useViewportRange, useI18nBundle }; | ||
export { useConsolidatedRef, usePassThroughHtmlProps, useViewportRange, useI18nText }; |
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,31 @@ | ||
import { StyleClassHelper } from './StyleClassHelper'; | ||
|
||
describe('StyleClassHelper', () => { | ||
test('add class names', () => { | ||
const helper = StyleClassHelper.of('class1', 'class2'); | ||
expect(helper.className).toEqual(`class1 class2`); | ||
}); | ||
|
||
test('put new classes', () => { | ||
const helper = StyleClassHelper.of('firstClass'); | ||
helper.put('secondClass'); | ||
expect(helper.className).toEqual(`firstClass secondClass`); | ||
}); | ||
|
||
test('putIfPresent', () => { | ||
const helper = StyleClassHelper.of('firstClass'); | ||
helper.put('secondClass'); | ||
helper.putIfPresent('ifPresent1'); | ||
helper.putIfPresent('ifPresent2', 'ifPresent3'); | ||
helper.putIfPresent(true === false && 'shouldNotBeThere1', 'a' === 'a' && 'ifPresent4'); | ||
|
||
expect(helper.className).toEqual('firstClass secondClass ifPresent1 ifPresent2 ifPresent3 ifPresent4'); | ||
}); | ||
|
||
test('className, toString and valueOf return same value', () => { | ||
const helper = StyleClassHelper.of('class1', 'class2'); | ||
expect(helper.toString()).toEqual(`class1 class2`); | ||
expect(helper.valueOf()).toEqual(`class1 class2`); | ||
expect(helper.className).toEqual(`class1 class2`); | ||
}); | ||
}); |
Oops, something went wrong.