Skip to content

Commit 78ef567

Browse files
committed
fix(ui-alerts): fix Alert border radius override and add new prop for custom icon
INSTUI-4572
1 parent a74236a commit 78ef567

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

packages/ui-alerts/src/Alert/__tests__/Alert.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import '@testing-library/jest-dom'
2929
import userEvent from '@testing-library/user-event'
3030
import { Alert } from '../index'
3131
import type { AlertProps } from '../props'
32+
import { IconGroupLine } from '@instructure/ui-icons'
3233

3334
describe('<Alert />', () => {
3435
let srdiv: HTMLDivElement | null
@@ -147,6 +148,14 @@ describe('<Alert />', () => {
147148
expect(liveRegion).toHaveAttribute('aria-live', 'polite')
148149
})
149150

151+
it('should render an icon when provided as the `renderIcon` prop', () => {
152+
const { container } = render(<Alert renderCustomIcon={<IconGroupLine />} />)
153+
const icon = container.querySelector('svg[class$="-svgIcon"]')
154+
155+
expect(icon).toHaveAttribute('name', 'IconGroup')
156+
expect(icon).toBeInTheDocument()
157+
})
158+
150159
describe('with `screenReaderOnly', () => {
151160
it('should not render anything when using `liveRegion`', async () => {
152161
const liveRegion = document.getElementById('_alertLiveRegion')!

packages/ui-alerts/src/Alert/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,11 @@ class Alert extends Component<AlertProps, AlertState> {
237237
}
238238

239239
renderIcon() {
240-
const Icon = this.variantUI[this.props.variant!]
240+
const { renderCustomIcon, variant, styles } = this.props
241+
const Icon = this.variantUI[variant!]
241242
return (
242-
<div css={this.props.styles?.icon}>
243-
<Icon />
243+
<div css={styles?.icon}>
244+
{renderCustomIcon ? callRenderProp(renderCustomIcon) : <Icon />}
244245
</div>
245246
)
246247
}

packages/ui-alerts/src/Alert/props.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ type AlertOwnProps = {
109109
* If the alert should have a shadow.
110110
*/
111111
hasShadow: boolean
112+
113+
/**
114+
* An icon, or function that returns an icon. Setting it will override the variant's icon.
115+
*/
116+
renderCustomIcon?: Renderable
112117
}
113118

114119
type PropKeys = keyof AlertOwnProps
@@ -137,7 +142,8 @@ const propTypes: PropValidators<PropKeys> = {
137142
transition: PropTypes.oneOf(['none', 'fade']),
138143
open: PropTypes.bool,
139144
hasShadow: PropTypes.bool,
140-
variantScreenReaderLabel: PropTypes.string
145+
variantScreenReaderLabel: PropTypes.string,
146+
renderCustomIcon: PropTypes.oneOfType([PropTypes.node, PropTypes.func])
141147
}
142148

143149
const allowedProps: AllowedPropKeys = [
@@ -153,7 +159,8 @@ const allowedProps: AllowedPropKeys = [
153159
'onDismiss',
154160
'transition',
155161
'open',
156-
'hasShadow'
162+
'hasShadow',
163+
'renderCustomIcon'
157164
]
158165

159166
type AlertState = {

packages/ui-alerts/src/Alert/styles.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ const generateStyle = (
9595
justifyContent: 'center',
9696
fontSize: '1.125rem',
9797
borderRight: `${componentTheme.borderWidth} ${componentTheme.borderStyle}`,
98+
margin: -1,
99+
borderStartStartRadius: componentTheme.borderRadius,
100+
borderEndStartRadius: componentTheme.borderRadius,
98101
...variantStyles[variant!].icon
99102
},
100103
closeButton: {

0 commit comments

Comments
 (0)