-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
display component name in designview + refactor retrieving name logic
- Loading branch information
1 parent
42da9d8
commit f81b1fc
Showing
19 changed files
with
137 additions
and
188 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
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
33 changes: 33 additions & 0 deletions
33
frontend/packages/ux-editor/src/hooks/useComponentTitle.test.ts
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,33 @@ | ||
import { ComponentType, InternalComponentType } from 'app-shared/types/ComponentType'; | ||
import { renderHook } from '@testing-library/react'; | ||
import { useComponentTitle } from './useComponentTitle'; | ||
import { textMock } from '@studio/testing/mocks/i18nMock'; | ||
import { componentMocks } from '../testing/componentMocks'; | ||
|
||
// Test data: | ||
const inputText = textMock(`ux_editor.component_title.${ComponentType.Input}`); | ||
const paragraphText = textMock(`ux_editor.component_title.${ComponentType.Paragraph}`); | ||
const headerText = textMock(`ux_editor.component_title.${ComponentType.Header}`); | ||
const checkboxesText = textMock(`ux_editor.component_title.${ComponentType.Checkboxes}`); | ||
const customButtonText = textMock(`ux_editor.component_title.${ComponentType.CustomButton}`); | ||
const closeSubformButtonText = textMock( | ||
`ux_editor.component_title.${InternalComponentType.CloseSubformButton}`, | ||
); | ||
|
||
describe('useComponentTypeName', () => { | ||
const { result } = renderHook(useComponentTitle); | ||
|
||
it('Returns the correct text if it exists', () => { | ||
expect(result.current(componentMocks[ComponentType.Input])).toBe(inputText); | ||
expect(result.current(componentMocks[ComponentType.Paragraph])).toBe(paragraphText); | ||
expect(result.current(componentMocks[ComponentType.CustomButton])).toBe(customButtonText); | ||
expect(result.current(componentMocks[InternalComponentType.CloseSubformButton])).toBe( | ||
closeSubformButtonText, | ||
); | ||
}); | ||
|
||
it('Returns the component type if the text does not exist', () => { | ||
expect(result.current(componentMocks[ComponentType.Header])).toBe(headerText); | ||
expect(result.current(componentMocks[ComponentType.Checkboxes])).toBe(checkboxesText); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
frontend/packages/ux-editor/src/hooks/useComponentTitle.ts
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,41 @@ | ||
import { useTranslation } from 'react-i18next'; | ||
import { useCallback } from 'react'; | ||
import type { FormContainer } from '../types/FormContainer'; | ||
import type { FormComponent } from '../types/FormComponent'; | ||
import { formItemConfigs } from '../data/formItemConfig'; | ||
import type { IToolbarElement } from '../types/global'; | ||
import { textResourcesByLanguageSelector } from '../selectors/textResourceSelectors'; | ||
import { DEFAULT_LANGUAGE } from 'app-shared/constants'; | ||
import { useTextResourcesSelector } from './useTextResourcesSelector'; | ||
import type { ITextResource } from 'app-shared/types/global'; | ||
import { getTextResource, getTitleByComponentType } from '../utils/language'; | ||
|
||
export function useComponentTitle(): ( | ||
formItem: FormComponent | FormContainer | IToolbarElement, | ||
) => string { | ||
const { t } = useTranslation(); | ||
const getTitleByTextResource = useTextResourceTitle(); | ||
|
||
return useCallback( | ||
(formItem: FormComponent | FormContainer | IToolbarElement) => { | ||
if ('textResourceBindings' in formItem && getTitleByTextResource(formItem)) | ||
return getTitleByTextResource(formItem); | ||
|
||
const getDisplayName = formItemConfigs[formItem.type]?.getDisplayName; | ||
const componentType = getDisplayName ? getDisplayName(formItem) : formItem.type; | ||
return getTitleByComponentType(componentType, t); | ||
}, | ||
[t, getTitleByTextResource], | ||
); | ||
} | ||
|
||
const useTextResourceTitle = (): ((item: FormComponent | FormContainer) => string) => { | ||
const textResources: ITextResource[] = useTextResourcesSelector<ITextResource[]>( | ||
textResourcesByLanguageSelector(DEFAULT_LANGUAGE), | ||
); | ||
return useCallback( | ||
(item: FormComponent | FormContainer) => | ||
getTextResource(item.textResourceBindings?.title, textResources), | ||
[textResources], | ||
); | ||
}; |
Oops, something went wrong.