Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Commit

Permalink
feat(components-message-card): added support for text only survey
Browse files Browse the repository at this point in the history
  • Loading branch information
Charca committed Aug 16, 2022
1 parent 9db3986 commit 507b696
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 10 deletions.
20 changes: 20 additions & 0 deletions src/components/MessageCard/MessageCard.stories.mdx
Expand Up @@ -315,6 +315,26 @@ This component renders a Message Card Notification with (optional) Title, Subtit
</Story>
</Canvas>

# With Text survey

<Canvas>
<Story name="With Text survey">
<MessageCardStoryWrapperUI>
<MessageCard
title="Which pizza do you like the most?"
body="Your feedback is valuable to us. Let us know how we did so we can continue to improve our services."
action={() => (
<MessageCard.Survey
isTextOnlySurvey
feedbackFormPlaceholder="Tell us more..."
onSubmit={data => console.log(data)}
/>
)}
/>
</MessageCardStoryWrapperUI>
</Story>
</Canvas>

# With video

<Canvas>
Expand Down
23 changes: 23 additions & 0 deletions src/components/MessageCard/MessageCard.test.js
Expand Up @@ -519,6 +519,29 @@ describe('Surveys', () => {
expect(screen.queryByLabelText(formLabel)).toBeInTheDocument()
})

test('Renders a feedback form by default if isTextOnlySurvey is set', () => {
const onSubmit = jest.fn()
const formLabel = 'Tell us more...'

render(
<MessageCard.Survey
isTextOnlySurvey
feedbackFormPlaceholder={formLabel}
onSubmit={onSubmit}
/>
)

expect(screen.queryByLabelText(formLabel)).toBeInTheDocument()

userEvent.type(screen.getByLabelText(formLabel), 'Great question')
userEvent.click(screen.getByRole('button', { name: 'Send' }))

expect(onSubmit).toHaveBeenCalledWith({
feedback: 'Great question',
selected: null,
})
})

test('Renders a feedback form delayed after selection if withShowFeedbackFormDelay is set', () => {
const formLabel = 'Tell us more...'

Expand Down
Expand Up @@ -18,6 +18,13 @@ export const SurveyUI = styled('div')`
margin: 0px -16px -16px -16px;
padding: 15px;
position: relative;
${({ $isTextOnly }) =>
$isTextOnly &&
css`
padding-top: 0px;
background: transparent;
`};
`

export const SurveyOptionsUI = styled('div')`
Expand Down Expand Up @@ -181,6 +188,15 @@ export const FeedbackFormUI = styled('form')`
overflow: hidden;
animation: HeightAnimation 400ms;
${({ $isTextOnly }) =>
$isTextOnly &&
css`
// This essentially resets the padding and margin to 0, but leaving space for the focus state
// as mentioned in the comment above.
padding-top: 1px;
margin-top: -1px;
`};
@keyframes HeightAnimation {
0% {
max-height: 0;
Expand Down
38 changes: 28 additions & 10 deletions src/components/MessageCard/components/survey/MessageCard.Survey.jsx
Expand Up @@ -13,6 +13,7 @@ import Input from '../../../Input'
import Spinner from '../../../Spinner'
import Icon from '../../../Icon'
import Truncate from '../../../Truncate'
import VisuallyHidden from '../../../VisuallyHidden'

function noop() {}

Expand All @@ -23,18 +24,21 @@ export const MessageCardSurvey = ({
withFeedbackForm = false,
forceFeedbackForm = false,
feedbackFormText = '',
feedbackFormPlaceholder = '',
confirmationText = 'Thanks for the feedback',
submitButtonText = 'Send',
onSubmit = noop,
showSpinner = false,
showConfirmationMessage = false,
theme,
isTextOnlySurvey = false,
withShowFeedbackFormDelay = false,
}) => {
const [feedback, setFeedback] = React.useState('')
const [selected, setSelected] = React.useState(null)
const [showFeedbackForm, setShowFeedbackForm] = React.useState(false)
const shouldShowFeedbackForm = showFeedbackForm || forceFeedbackForm
const shouldShowFeedbackForm =
showFeedbackForm || forceFeedbackForm || isTextOnlySurvey
const isMounted = useRef(true)

useEffect(() => {
Expand Down Expand Up @@ -88,16 +92,27 @@ export const MessageCardSurvey = ({
}

return (
<SurveyUI data-cy="beacon-message-cta-survey">
<SurveyContext.Provider value={contextValue}>
{children}
</SurveyContext.Provider>
<SurveyUI
data-cy="beacon-message-cta-survey"
$isTextOnly={isTextOnlySurvey}
>
{children && (
<SurveyContext.Provider value={contextValue}>
{children}
</SurveyContext.Provider>
)}

{shouldShowFeedbackForm && (
<FeedbackFormUI onSubmit={handleSubmit}>
<FeedbackLabelUI htmlFor="survey-comment">
{feedbackFormText}
</FeedbackLabelUI>
<FeedbackFormUI onSubmit={handleSubmit} $isTextOnly={isTextOnlySurvey}>
{!isTextOnlySurvey ? (
<FeedbackLabelUI htmlFor="survey-comment">
{feedbackFormText}
</FeedbackLabelUI>
) : (
<VisuallyHidden>
<label htmlFor="survey-comment">{feedbackFormPlaceholder}</label>
</VisuallyHidden>
)}
<Input
id="survey-comment"
name="survey-comment"
Expand All @@ -106,6 +121,7 @@ export const MessageCardSurvey = ({
multiline={3}
value={feedback}
onChange={value => setFeedback(value)}
placeholder={feedbackFormPlaceholder}
/>
<SubmitFeedbackFormButtonUI
data-cy="beacon-message-cta-survey-submit"
Expand All @@ -128,12 +144,14 @@ export const MessageCardSurvey = ({
}

MessageCardSurvey.propTypes = {
children: PropTypes.node.isRequired,
children: PropTypes.node,
withFeedbackForm: PropTypes.bool,
forceFeedbackForm: PropTypes.bool,
feedbackFormText: PropTypes.string,
feedbackFormPlaceholder: PropTypes.string,
confirmationText: PropTypes.string,
onSubmit: PropTypes.func,
showSpinner: PropTypes.bool,
showConfirmationMessage: PropTypes.bool,
isTextOnlySurvey: PropTypes.bool,
}

0 comments on commit 507b696

Please sign in to comment.