Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add react markdown to radio label #111

Merged
merged 3 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Questions/Radio/__tests__/radio.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ test('markdown is displayed', () => {
expect(getByText('question text to display'))
})

test('handles default markdown link', () => {
const questionWithLink = {
...question,
label: 'question text to display[radio link](https://www.google.es)'
}

const { getByRole } = render(
<QuestionRadio
question={questionWithLink}
useForm={{ errors: {}, register: () => {} }}
/>
)

const markDownLink = getByRole('link')
expect(markDownLink.href).toBe('https://www.google.es/')
expect(markDownLink.target).toBe('_blank')
})

test('radio labels are displayed', () => {
const { getByLabelText } = render(
<QuestionRadio
Expand Down
33 changes: 28 additions & 5 deletions src/Questions/Radio/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import ErrorMessage from '../../Fields/Error'
import Label from '../../Fields/Label'
import Radio from '../../Fields/Radio'
// import ReactMarkdown from '../../Fields/Markdown'
/** @jsx jsx */
/** @jsxRuntime classic */
import { jsx } from 'theme-ui'

const QuestionRadio = ({ question, useForm }) => {
import ReactMarkdown from '../../Fields/Markdown'

const QuestionRadio = ({ question, useForm, onLinkOpen }) => {
const styles = {
fieldset: {
border: '0',
m: '0',
p: '0'
},
markDown: {
alignSelf: 'center',
p: { m: '0px' }
}
}

const { register, errors } = useForm

const radioButtonGenerator = (question) => {
Expand Down Expand Up @@ -42,14 +55,24 @@ const QuestionRadio = ({ question, useForm }) => {
: 'forms.radioContainer'
}}
>
<fieldset sx={{ border: '0', m: '0', p: '0' }}>
<fieldset sx={styles.fieldset}>
{question.accessibility ? (
<legend sx={{ variant: 'forms.label' }} htmlFor={question.name}>
{question.label}
<ReactMarkdown
sx={styles.markDown}
source={question.label}
onLinkOpen={onLinkOpen}
modalLabel={question.modalLabel}
/>
</legend>
) : (
<Label htmlFor={question.label} key={question.label}>
{question.label}
<ReactMarkdown
sx={styles.markDown}
source={question.label}
onLinkOpen={onLinkOpen}
modalLabel={question.modalLabel}
/>
</Label>
)}

Expand Down