-
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.
11230 editdatamodelbindings (#11333)
* edit data model binding
- Loading branch information
1 parent
64d10f8
commit e3af968
Showing
10 changed files
with
519 additions
and
52 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
6 changes: 6 additions & 0 deletions
6
frontend/packages/shared/src/components/InputActionWrapper/InputActionWrapper.module.css
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,6 @@ | ||
.container { | ||
margin-top: var(--fds-spacing-3); | ||
display: flex; | ||
align-items: center; | ||
min-height: var(--fds-component-mode-height-medium); | ||
} |
95 changes: 95 additions & 0 deletions
95
frontend/packages/shared/src/components/InputActionWrapper/InputActionWrapper.test.tsx
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,95 @@ | ||
import React from 'react'; | ||
import { act, render as rtlRender, screen } from '@testing-library/react'; | ||
import type { InputActionWrapperProps } from './InputActionWrapper'; | ||
import { InputActionWrapper } from './InputActionWrapper'; | ||
import { mockUseTranslation } from '../../../../../testing/mocks/i18nMock'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
jest.mock('react-i18next', () => ({ useTranslation: () => mockUseTranslation() })); | ||
|
||
const user = userEvent.setup(); | ||
|
||
const mockProps: InputActionWrapperProps = { | ||
mode: 'editMode', | ||
onEditClick: jest.fn(), | ||
onDeleteClick: jest.fn(), | ||
onSaveClick: jest.fn(), | ||
children: <input />, | ||
}; | ||
|
||
describe('InputActionWrapper', () => { | ||
it('renders save and delete buttons when mode is (editMode)', async () => { | ||
await rtlRender(<InputActionWrapper {...mockProps} mode='editMode' />); | ||
expect(screen.getByLabelText('general.save')).toBeInTheDocument(); | ||
expect(screen.getByLabelText('general.delete')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders edit button when mode is (hoverMode)', async () => { | ||
await rtlRender(<InputActionWrapper {...mockProps} mode='hoverMode' />); | ||
expect(screen.getByLabelText('general.edit')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders delete button when mode is (hoverMode)', async () => { | ||
await rtlRender(<InputActionWrapper {...mockProps} mode='hoverMode' />); | ||
expect(screen.getByLabelText('general.delete')).toBeInTheDocument(); | ||
}); | ||
|
||
it('does not render save button when mode is (hoverMode)', async () => { | ||
await rtlRender(<InputActionWrapper {...mockProps} mode='hoverMode' />); | ||
expect(screen.queryByLabelText('general.save')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('does not renders edit button when mode is (editMode)', async () => { | ||
await rtlRender(<InputActionWrapper {...mockProps} mode='editMode' />); | ||
expect(screen.queryByLabelText('general.edit')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('renders delete button when mode is (editMode)', async () => { | ||
await rtlRender(<InputActionWrapper {...mockProps} mode='editMode' />); | ||
expect(screen.getByLabelText('general.delete')).toBeInTheDocument(); | ||
}); | ||
|
||
it('triggers save click on save button click', async () => { | ||
rtlRender(<InputActionWrapper {...mockProps} />); | ||
const saveButton = screen.getByLabelText('general.save'); | ||
await act(() => user.click(saveButton)); | ||
expect(mockProps.onSaveClick).toBeCalledTimes(1); | ||
}); | ||
|
||
it('triggers delete click on delete button click', async () => { | ||
rtlRender(<InputActionWrapper {...mockProps} />); | ||
const deleteButton = screen.getByLabelText('general.delete'); | ||
await act(() => user.click(deleteButton)); | ||
expect(mockProps.onDeleteClick).toBeCalledTimes(1); | ||
}); | ||
|
||
it('check that handleActionClick is called when edit button is clicked', async () => { | ||
rtlRender(<InputActionWrapper {...mockProps} mode='hoverMode' />); | ||
const editButton = screen.getByLabelText('general.edit'); | ||
await act(() => user.click(editButton)); | ||
expect(mockProps.onEditClick).toBeCalledTimes(1); | ||
}); | ||
|
||
it('check that handleHover is called when onMouseOver is called ', async () => { | ||
rtlRender(<InputActionWrapper {...mockProps} mode='standBy' />); | ||
const input = screen.getByRole('textbox'); | ||
await act(() => user.hover(input)); | ||
expect(screen.getByLabelText('general.edit')).toBeInTheDocument(); | ||
}); | ||
|
||
it('check that handleBlur is called when onMouseLeave is called ', async () => { | ||
rtlRender(<InputActionWrapper {...mockProps} mode='standBy' />); | ||
const input = screen.getByRole('textbox'); | ||
await act(() => user.hover(input)); | ||
expect(screen.getByLabelText('general.edit')).toBeInTheDocument(); | ||
await act(() => user.unhover(input)); | ||
expect(screen.queryByLabelText('general.edit')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('check that handleFocus is called when onFocus is called ', async () => { | ||
rtlRender(<InputActionWrapper {...mockProps} mode='standBy' />); | ||
const input = screen.getByRole('textbox'); | ||
await act(() => user.hover(input)); | ||
expect(screen.getByLabelText('general.edit')).toBeInTheDocument(); | ||
}); | ||
}); |
111 changes: 111 additions & 0 deletions
111
frontend/packages/shared/src/components/InputActionWrapper/InputActionWrapper.tsx
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,111 @@ | ||
import React, { useState } from 'react'; | ||
import { CheckmarkIcon, TrashIcon, PencilWritingIcon } from '@altinn/icons'; | ||
import { useText } from '../../../../ux-editor/src/hooks/index'; | ||
import { Button, ButtonProps } from '@digdir/design-system-react'; | ||
import classes from './InputActionWrapper.module.css'; | ||
|
||
type AvailableAction = 'edit' | 'save' | 'delete'; | ||
export type ActionGroup = 'editMode' | 'hoverMode' | 'standBy'; | ||
|
||
const actionGroupMap: Record<ActionGroup, AvailableAction[]> = { | ||
editMode: ['save', 'delete'], | ||
hoverMode: ['edit', 'delete'], | ||
standBy: [], | ||
}; | ||
|
||
const actionToIconMap: Record<AvailableAction, React.ReactNode> = { | ||
edit: <PencilWritingIcon />, | ||
save: <CheckmarkIcon />, | ||
delete: <TrashIcon />, | ||
}; | ||
|
||
export type InputActionWrapperProps = { | ||
children: React.ReactElement; | ||
mode?: ActionGroup; | ||
onEditClick: () => void; | ||
onDeleteClick: () => void; | ||
onSaveClick: () => void; | ||
}; | ||
|
||
export const InputActionWrapper = ({ | ||
mode, | ||
children, | ||
onEditClick, | ||
onDeleteClick, | ||
onSaveClick, | ||
...rest | ||
}: InputActionWrapperProps): JSX.Element => { | ||
const t = useText(); | ||
const [actions, setActions] = useState<AvailableAction[]>(actionGroupMap[mode || 'standBy']); | ||
|
||
const handleFocus = (): void => { | ||
setActions(actionGroupMap['editMode']); | ||
}; | ||
|
||
const handleBlur = (): void => { | ||
setActions(actionGroupMap['standBy']); | ||
}; | ||
|
||
const handleMouseLeave = (): void => { | ||
const isEditMode = actions.includes('save'); | ||
if (isEditMode) return; | ||
handleBlur(); | ||
}; | ||
|
||
const handleActionClick = (action: AvailableAction): void => { | ||
switch (action) { | ||
case 'delete': | ||
onDeleteClick(); | ||
break; | ||
case 'save': | ||
onSaveClick(); | ||
handleBlur(); | ||
break; | ||
case 'edit': | ||
onEditClick(); | ||
setActions(actionGroupMap['editMode']); | ||
default: | ||
break; | ||
} | ||
}; | ||
|
||
const handleHover = (): void => { | ||
const isInStandByMode = mode === 'standBy'; | ||
if (isInStandByMode) { | ||
setActions(actionGroupMap['hoverMode']); | ||
} | ||
}; | ||
|
||
const actionToAriaLabelMap: Record<AvailableAction, string> = { | ||
edit: t('general.edit'), | ||
delete: t('general.delete'), | ||
save: t('general.save'), | ||
}; | ||
|
||
const actionToColorMap: Record<AvailableAction, ButtonProps['color']> = { | ||
edit: 'first', | ||
save: 'success', | ||
delete: 'danger', | ||
}; | ||
|
||
return ( | ||
<div className={classes.container} onMouseOver={handleHover} onMouseLeave={handleMouseLeave}> | ||
{React.cloneElement(children, { | ||
...rest, | ||
onFocus: handleFocus, | ||
})} | ||
{actions.map((action) => ( | ||
<Button | ||
variant='quiet' | ||
size='medium' | ||
color={actionToColorMap[action]} | ||
key={action} | ||
onClick={() => handleActionClick(action)} | ||
aria-label={actionToAriaLabelMap[action]} | ||
> | ||
{actionToIconMap[action]} | ||
</Button> | ||
))} | ||
</div> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
frontend/packages/shared/src/components/InputActionWrapper/index.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 @@ | ||
export { InputActionWrapper } from './InputActionWrapper'; |
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
25 changes: 25 additions & 0 deletions
25
frontend/packages/ux-editor/src/components/config/editModal/EditDataModelBindings.module.css
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,25 @@ | ||
.datamodelLink { | ||
display: flex; | ||
align-items: center; | ||
} | ||
|
||
.linkIcon { | ||
font-size: 1.5rem; | ||
} | ||
|
||
.SelectDataModelComponent { | ||
margin-top: -25px; | ||
width: 100%; | ||
} | ||
|
||
.linkedDatamodelContainer { | ||
margin-top: 25px; | ||
display: flex; | ||
align-items: center; | ||
gap: var(--fds-spacing-1); | ||
} | ||
|
||
.selectedOption { | ||
gap: var(--fds-spacing-1); | ||
width: 50%; | ||
} |
Oops, something went wrong.