-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add property button component (#12428)
- Loading branch information
Showing
13 changed files
with
171 additions
and
65 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
...ibs/studio-components/src/components/StudioPropertyButton/StudioPropertyButton.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,39 @@ | ||
.propertyButton { | ||
border-radius: 0; | ||
display: flex; | ||
padding: var(--studio-property-button-vertical-spacing) var(--fds-spacing-5); | ||
justify-content: flex-start; | ||
overflow: hidden; | ||
} | ||
|
||
.propertyButton.withValue .content { | ||
color: var(--fds-semantic-text-neutral-default); | ||
text-align: left; | ||
} | ||
|
||
.propertyButton.withValue .property { | ||
font-weight: bold; | ||
} | ||
|
||
.content { | ||
overflow: hidden; | ||
white-space: nowrap; | ||
} | ||
|
||
.property, | ||
.value { | ||
display: block; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
} | ||
|
||
.editIconWrapper { | ||
flex: 1; | ||
text-align: right; | ||
display: none; | ||
} | ||
|
||
.propertyButton:hover .editIconWrapper, | ||
.propertyButton:focus .editIconWrapper { | ||
display: inline-block; | ||
} |
58 changes: 58 additions & 0 deletions
58
.../libs/studio-components/src/components/StudioPropertyButton/StudioPropertyButton.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,58 @@ | ||
import type { RefObject } from 'react'; | ||
import React from 'react'; | ||
import type { StudioPropertyButtonProps } from './StudioPropertyButton'; | ||
import { StudioPropertyButton } from './StudioPropertyButton'; | ||
import { act, render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
// Test data: | ||
const property = 'Test property'; | ||
const defaultProps: StudioPropertyButtonProps = { | ||
property, | ||
}; | ||
|
||
describe('StudioPropertyButton', () => { | ||
it('Renders a button with the property name', () => { | ||
renderButton(); | ||
expect(screen.getByRole('button', { name: property })).toHaveTextContent(property); | ||
}); | ||
|
||
it('Renders both the property and the value when a value is given', () => { | ||
const value = 'Test value'; | ||
renderButton({ value }); | ||
expect(screen.getByRole('button', { name: property })).toHaveTextContent(property); | ||
expect(screen.getByRole('button', { name: property })).toHaveTextContent(value); | ||
}); | ||
|
||
it('Overrides the icon when a custom icon is given', () => { | ||
const iconTestId = 'custom-icon'; | ||
const icon = <svg data-testid={iconTestId} />; | ||
renderButton({ icon }); | ||
expect(screen.getByTestId(iconTestId)).toBeInTheDocument(); | ||
}); | ||
|
||
it('Appends the given class name', () => { | ||
const className = 'test-class'; | ||
renderButton({ className }); | ||
expect(screen.getByRole('button', { name: property })).toHaveClass(className); | ||
}); | ||
|
||
it('Forwards a ref to the button', () => { | ||
const ref = React.createRef<HTMLButtonElement>(); | ||
renderButton({}, ref); | ||
expect(ref.current).toBe(screen.getByRole('button')); | ||
}); | ||
|
||
it('Calls the onClick function when the button is clicked', async () => { | ||
const user = userEvent.setup(); | ||
const onClick = jest.fn(); | ||
renderButton({ onClick }); | ||
await act(() => user.click(screen.getByRole('button'))); | ||
expect(onClick).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
const renderButton = ( | ||
props: Partial<StudioPropertyButtonProps> = {}, | ||
ref?: RefObject<HTMLButtonElement>, | ||
) => render(<StudioPropertyButton {...defaultProps} {...props} ref={ref} />); |
50 changes: 50 additions & 0 deletions
50
frontend/libs/studio-components/src/components/StudioPropertyButton/StudioPropertyButton.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,50 @@ | ||
import type { ReactNode } from 'react'; | ||
import React, { forwardRef } from 'react'; | ||
import type { StudioButtonProps } from '../StudioButton'; | ||
import { StudioButton } from '../StudioButton'; | ||
import classes from './StudioPropertyButton.module.css'; | ||
import { PlusCircleIcon, PencilIcon } from '@studio/icons'; | ||
import cn from 'classnames'; | ||
|
||
export type StudioPropertyButtonProps = { | ||
property: string; | ||
value?: ReactNode; | ||
} & Omit<StudioButtonProps, 'children' | 'value'>; | ||
|
||
const StudioPropertyButton = forwardRef<HTMLButtonElement, StudioPropertyButtonProps>( | ||
({ property, value, icon: givenIcon, className: givenClass, ...rest }, ref) => { | ||
const hasValue = !!value; | ||
|
||
const icon = hasValue || givenIcon ? givenIcon : <PlusCircleIcon />; | ||
|
||
const className = cn(classes.propertyButton, hasValue && classes.withValue, givenClass); | ||
|
||
return ( | ||
<StudioButton | ||
aria-label={property} | ||
className={className} | ||
fullWidth | ||
icon={icon} | ||
ref={ref} | ||
size='small' | ||
title={property} | ||
variant='tertiary' | ||
{...rest} | ||
> | ||
<span className={classes.content}> | ||
<span className={classes.property}>{property}</span> | ||
<span className={classes.value}>{value}</span> | ||
</span> | ||
{hasValue && ( | ||
<span className={classes.editIconWrapper}> | ||
<PencilIcon /> | ||
</span> | ||
)} | ||
</StudioButton> | ||
); | ||
}, | ||
); | ||
|
||
StudioPropertyButton.displayName = 'StudioPropertyButton'; | ||
|
||
export { StudioPropertyButton }; |
2 changes: 2 additions & 0 deletions
2
frontend/libs/studio-components/src/components/StudioPropertyButton/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,2 @@ | ||
export { StudioPropertyButton } from './StudioPropertyButton'; | ||
export type { StudioPropertyButtonProps } from './StudioPropertyButton'; |
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
1 change: 0 additions & 1 deletion
1
frontend/packages/ux-editor/src/components/Properties/DataModelBindings.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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
.container { | ||
display: grid; | ||
gap: 1rem; | ||
} | ||
|
||
.switch { | ||
|
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
24 changes: 0 additions & 24 deletions
24
...omponents/config/editModal/EditDataModelBindings/DefinedBinding/DefinedBinding.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 |
---|---|---|
@@ -1,29 +1,5 @@ | ||
.definedBinding { | ||
border-radius: 0; | ||
padding-left: var(--fds-spacing-5); | ||
padding-right: var(--fds-spacing-5); | ||
} | ||
|
||
.mainContent { | ||
align-items: flex-start; | ||
color: var(--fds-semantic-text-neutral-default); | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
||
.selectedOption { | ||
align-items: center; | ||
display: flex; | ||
gap: var(--fds-spacing-1); | ||
} | ||
|
||
.pencilIcon { | ||
visibility: hidden; | ||
flex: 1; | ||
text-align: right; | ||
} | ||
|
||
.definedBinding:hover .pencilIcon, | ||
.definedBinding:focus .pencilIcon { | ||
visibility: visible; | ||
} |
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
6 changes: 0 additions & 6 deletions
6
...nents/config/editModal/EditDataModelBindings/UndefinedBinding/UndefinedBinding.module.css
This file was deleted.
Oops, something went wrong.
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