Skip to content

Commit

Permalink
Merge pull request #206 from PrestaShopCorp/improvement/snackbar-add-…
Browse files Browse the repository at this point in the history
…a-success-state

Improvement/snackbar add a success state
  • Loading branch information
mattgoud authored Oct 11, 2023
2 parents fb51f2f + 897d62a commit 7b79976
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/components/snackbar/src/snackbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { buildProps } from '@puik/utils'
import type { ExtractPropTypes, PropType } from 'vue'
import type Snackbar from './snackbar.vue'

export const snackbarVariants = ['default', 'danger'] as const
export const snackbarVariants = ['default', 'danger', 'success'] as const
export type PuikSnackbarVariant = (typeof snackbarVariants)[number]

export interface SnackbarAction {
Expand Down
51 changes: 49 additions & 2 deletions packages/components/snackbar/stories/snackbar.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,18 @@ const WithoutActionTemplate: StoryFn = () => ({
text: 'Unable to update settings.',
variant: 'danger',
})
return { displaySnackbar, displayErrorSnackbar }
const displaySuccessSnackbar = () =>
PuikSnackbar({
text: 'Settings updated successfully.',
variant: 'success',
})
return { displaySnackbar, displayErrorSnackbar, displaySuccessSnackbar }
},
template: `
<div class="space-x-4">
<puik-button @click="displaySnackbar">Display Snackbar</puik-button>
<puik-button @click="displayErrorSnackbar">Display Error Snackbar</puik-button>
<puik-button @click="displaySuccessSnackbar">Display Success Snackbar</puik-button>
</div>
`,
})
Expand All @@ -139,6 +145,7 @@ export const WithoutAction: StoryObj = {
<template>
<puik-button @click="displaySnackbar">Display Snackbar</puik-button>
<puik-button @click="displayErrorSnackbar">Display Error Snackbar</puik-button>
<puik-button @click="displaySuccessSnackbar">Display Success Snackbar</puik-button>
</template>
<script lang="ts" setup>
Expand All @@ -151,6 +158,11 @@ const displayErrorSnackbar = () =>
text: 'Unable to update settings.',
variant: 'danger',
})
const displaySuccessSnackbar = () =>
PuikSnackbar({
text: 'Settings updated successfully.',
variant: 'success',
})
</script>
<!--HTML/CSS Snippet-->
Expand Down Expand Up @@ -179,6 +191,13 @@ const displayErrorSnackbar = () =>
close
</button>
</div>
<div class="puik-snackbar puik-snackbar--success" style="bottom: 168px">
<span class="puik-snackbar__text">Settings updated successfully.</span>
<button class="puik-snackbar__close-button">
close
</button>
</div>
`,
language: 'html',
},
Expand Down Expand Up @@ -208,12 +227,22 @@ const WithActionTemplate: StoryFn = () => ({
callback: action('Error snackbar action callback function'),
},
})
return { displaySnackbar, displayErrorSnackbar }
const displaySuccessSnackbar = () =>
PuikSnackbar({
text: 'Settings updated successfully.',
variant: 'success',
action: {
label: 'Cancel',
callback: action('Success snackbar action callback function'),
},
})
return { displaySnackbar, displayErrorSnackbar, displaySuccessSnackbar }
},
template: `
<div class="space-x-4">
<puik-button @click="displaySnackbar">Display Snackbar</puik-button>
<puik-button @click="displayErrorSnackbar">Display Error Snackbar</puik-button>
<puik-button @click="displaySuccessSnackbar">Display Success Snackbar</puik-button>
</div>
`,
})
Expand All @@ -228,6 +257,7 @@ export const WithAction: StoryObj = {
<template>
<puik-button @click="displaySnackbar">Display Snackbar</puik-button>
<puik-button @click="displayErrorSnackbar">Display Error Snackbar</puik-button>
<puik-button @click="displaySuccessSnackbar">Display Success Snackbar</puik-button>
</template>
<script lang="ts" setup>
Expand All @@ -248,6 +278,15 @@ export const WithAction: StoryObj = {
callback: action('Error snackbar action callback function'),
},
})
const displaySuccessSnackbar = () =>
PuikSnackbar({
text: 'Unable to update settings.',
variant: 'success',
action: {
label: 'Cancel',
callback: action('Success snackbar action callback function'),
},
})
</script>
<!--HTML/CSS Snippet-->
Expand Down Expand Up @@ -278,6 +317,14 @@ export const WithAction: StoryObj = {
close
</button>
</div>
<div class="puik-snackbar puik-snackbar--success" style="bottom: 168px">
<span class="puik-snackbar__text">Settings updated successfully.</span>
<button class="puik-snackbar__action">Retry</button>
<button class="puik-snackbar__close-button">
close
</button>
</div>
`,
language: 'html',
},
Expand Down
28 changes: 28 additions & 0 deletions packages/components/snackbar/test/snackbar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ describe('Snackbar tests', () => {
expect(findAction().exists()).toBeFalsy()
})

it('should be a success snackbar without action', () => {
const text = faker.lorem.sentence()
factory({ text, variant: 'success' })
expect(findText().text()).toEqual(text)
expect(findSnackbar().classes()).toContain('puik-snackbar--success')
expect(findAction().exists()).toBeFalsy()
})

it('should be a dangerous snackbar with action', async () => {
const text = faker.lorem.sentence()
const label = faker.lorem.word()
Expand All @@ -86,6 +94,26 @@ describe('Snackbar tests', () => {
expect(callback).toBeCalled()
})

it('should be a success snackbar with action', async () => {
const text = faker.lorem.sentence()
const label = faker.lorem.word()
const callback = vi.fn()
factory({
text,
action: {
label,
callback,
},
variant: 'success',
})
expect(findAction().exists()).toBeTruthy()
expect(findText().text()).toEqual(text)
expect(findSnackbar().classes()).toContain('puik-snackbar--success')
expect(findAction().text()).toEqual(label)
await findAction().trigger('click')
expect(callback).toBeCalled()
})

it('should close the snackbar when clicking on the close button', async () => {
const text = faker.lorem.sentence()
await factory({ text })
Expand Down
4 changes: 4 additions & 0 deletions packages/theme/src/snackbar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
@apply bg-red-500;
}

&--success {
@apply bg-green-500;
}

&__text {
@apply font-primary flex-grow;
}
Expand Down

0 comments on commit 7b79976

Please sign in to comment.