Skip to content

Commit

Permalink
feat: added close button to alert component
Browse files Browse the repository at this point in the history
  • Loading branch information
aAmorim27 committed Oct 30, 2023
1 parent 44585ad commit 1c2ae06
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 18 deletions.
11 changes: 11 additions & 0 deletions packages/components/alert/src/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ export const alertProps = buildProps({
required: false,
default: undefined,
},
secondaryButtonLabel: {
type: String,
required: false,
default: undefined,
},
closable: {
type: Boolean,
required: false,
default: false,
},
ariaLive: {
type: String as PropType<'polite' | 'assertive'>,
required: false,
Expand All @@ -48,6 +58,7 @@ export type AlertProps = ExtractPropTypes<typeof alertProps>

export const alertEmits = {
click: (event: Event) => event instanceof Event,
close: (event: Event) => event instanceof Event,
}

export type AlertEmits = typeof alertEmits
Expand Down
45 changes: 28 additions & 17 deletions packages/components/alert/src/alert.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,35 @@
]"
:aria-live="ariaLive"
>
<div class="puik-alert__content">
<puik-icon :icon="icon" font-size="1.25rem" class="puik-alert__icon" />
<div class="puik-alert__text">
<p v-if="title" class="puik-alert__title">{{ title }}</p>
<span
v-if="$slots.default || description"
class="puik-alert__description"
><slot>{{ description }}</slot></span
>
<div class="puik-alert__container">
<div class="puik-alert__content">
<PuikIcon :icon="icon" font-size="1.25rem" class="puik-alert__icon" />
<div class="puik-alert__text">
<p v-if="title" class="puik-alert__title">{{ title }}</p>
<span
v-if="$slots.default || description"
class="puik-alert__description"
><slot>{{ description }}</slot></span
>
</div>
</div>
<PuikButton
v-if="buttonLabel"
:variant="variant"
class="puik-alert__button"
@click="click"
>
{{ buttonLabel }}
</PuikButton>
</div>
<puik-button
v-if="buttonLabel"
:variant="variant"
class="puik-alert__button"
@click="click"
>
{{ buttonLabel }}
</puik-button>

<PuikIcon
v-if="closable"
icon="close"
font-size="1.25rem"
class="puik-alert__close"
@click="close"
/>
</div>
</template>

Expand All @@ -44,4 +54,5 @@ const emit = defineEmits(alertEmits)
const icon = computed(() => ICONS[props.variant])
const click = (event: Event) => emit('click', event)
const close = (event: Event) => emit('close', event)
</script>
6 changes: 6 additions & 0 deletions packages/components/alert/stories/alert.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export default {
buttonLabel: {
description: 'Label of the button',
},
closable: {
description: 'Display a close button',
},
default: {
control: 'none',
description: 'Set the alert description',
Expand All @@ -51,6 +54,7 @@ export default {
variant: 'success',
disableBorders: false,
buttonLabel: 'Button',
closable: false,
},
} as Meta

Expand All @@ -63,11 +67,13 @@ const Template: StoryFn = (args: Args) => ({
},
methods: {
click: action('click'),
close: action('close'),
},
template: `
<puik-alert
v-bind="args"
@click="click"
@close="close"
></puik-alert>`,
})

Expand Down
12 changes: 12 additions & 0 deletions packages/components/alert/test/alert.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('Alert tests', () => {
const findButton = () => wrapper.find('.puik-alert__button')
const findTitle = () => wrapper.find('.puik-alert__title')
const findDesc = () => wrapper.find('.puik-alert__description')
const findCloseButton = () => wrapper.find('.puik-alert__close')

const factory = (
propsData: Record<string, any> = {},
Expand Down Expand Up @@ -63,4 +64,15 @@ describe('Alert tests', () => {
})
expect(findAlert().classes()).toContain('puik-alert--no-borders')
})

it('should display a close icon and emit a close event on click', async () => {
factory({
title: faker.lorem.word(2),
description: faker.lorem.sentence(60),
closable: true,
})
expect(findCloseButton()).toBeTruthy()
await findCloseButton().trigger('click')
expect(wrapper.emitted('close')).toBeTruthy()
})
})
8 changes: 7 additions & 1 deletion packages/theme/src/alert.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@use './common/typography.scss';

.puik-alert {
@apply relative flex flex-col md:flex-row p-4 border text-primary-800 items-start;
@apply relative flex flex-row p-4 border text-primary-800 items-start;
&--success {
@apply bg-green-50 border border-green;
.puik-alert__icon {
Expand Down Expand Up @@ -29,6 +29,9 @@
&--no-borders {
@apply border-0;
}
&__container {
@apply flex flex-col md:flex-row w-full;
}
&__content {
@apply flex flex-row flex-grow;
}
Expand All @@ -48,4 +51,7 @@
&__icon {
@apply mt-0.5 flex-shrink-0;
}
&__close {
@apply ml-4 cursor-pointer leading-none;
}
}

0 comments on commit 1c2ae06

Please sign in to comment.