Commit c62226ac authored by Nathan Friend's avatar Nathan Friend

Make `textareaValue` required in <markdown-field>

This commit updates the `textareaValue` prop of the `<markdown-field>`
component to always be required, and removes some logic that attempted
to get the value of the child `<textarea>` in a "fancier", alternate
way.
parent 144802d9
...@@ -71,6 +71,7 @@ export default { ...@@ -71,6 +71,7 @@ export default {
:markdown-docs-path="descriptionHelpPath" :markdown-docs-path="descriptionHelpPath"
:add-spacing-classes="false" :add-spacing-classes="false"
:show-suggest-popover="true" :show-suggest-popover="true"
:textarea-value="issuableDescription"
> >
<textarea <textarea
id="issuable-description" id="issuable-description"
......
...@@ -371,6 +371,7 @@ export default { ...@@ -371,6 +371,7 @@ export default {
:markdown-docs-path="markdownDocsPath" :markdown-docs-path="markdownDocsPath"
:quick-actions-docs-path="quickActionsDocsPath" :quick-actions-docs-path="quickActionsDocsPath"
:add-spacing-classes="false" :add-spacing-classes="false"
:textarea-value="note"
> >
<textarea <textarea
id="note-body" id="note-body"
......
...@@ -328,6 +328,7 @@ export default { ...@@ -328,6 +328,7 @@ export default {
:add-spacing-classes="false" :add-spacing-classes="false"
:help-page-path="helpPagePath" :help-page-path="helpPagePath"
:show-suggest-popover="showSuggestPopover" :show-suggest-popover="showSuggestPopover"
:textarea-value="updatedNoteBody"
@handleSuggestDismissed="() => $emit('handleSuggestDismissed')" @handleSuggestDismissed="() => $emit('handleSuggestDismissed')"
> >
<textarea <textarea
......
...@@ -25,6 +25,18 @@ export default { ...@@ -25,6 +25,18 @@ export default {
}, },
mixins: [glFeatureFlagsMixin()], mixins: [glFeatureFlagsMixin()],
props: { props: {
/**
* This prop should be bound to the value of the `<textarea>` element
* that is rendered as a child of this component (in the `textarea` slot)
*/
textareaValue: {
type: String,
required: true,
},
markdownDocsPath: {
type: String,
required: true,
},
isSubmitting: { isSubmitting: {
type: Boolean, type: Boolean,
required: false, required: false,
...@@ -35,10 +47,6 @@ export default { ...@@ -35,10 +47,6 @@ export default {
required: false, required: false,
default: '', default: '',
}, },
markdownDocsPath: {
type: String,
required: true,
},
addSpacingClasses: { addSpacingClasses: {
type: Boolean, type: Boolean,
required: false, required: false,
...@@ -84,42 +92,6 @@ export default { ...@@ -84,42 +92,6 @@ export default {
required: false, required: false,
default: false, default: false,
}, },
/**
* This prop is used as a fallback if the value of the textarea can't be
* retreived using `this.$slots.textarea[0]?.elm?`.
*
* This happens when the `textarea` slot is defined like this:
*
* ```html
* <markdown-field>
* <template #textarea>
* <textarea></textarea>
* </template>
* </markdown-field>
* ```
*
* ... as opposed to this:
*
* ```html
* <markdown-field>
* <textarea slot="textarea">
* </markdown-field>
* ```
*
* When using `<template #textarea>` as shown above in example #1,
* it's important to **always** provide a value to this prop.
* If `textareaValue` isn't provided, this component will not
* show a preview when the "Preview" tab is clicked - it
* will always show "Nothing to preview."
*
* For more info, see https://github.com/vuejs/vue/issues/10450.
*/
textareaValue: {
type: String,
required: false,
default: '',
},
}, },
data() { data() {
return { return {
...@@ -219,17 +191,11 @@ export default { ...@@ -219,17 +191,11 @@ export default {
this.previewMarkdown = true; this.previewMarkdown = true;
/* if (this.textareaValue) {
Can't use `$refs` as the component is technically in the parent component
so we access the VNode & then get the element
*/
const text = this.$slots.textarea[0]?.elm?.value || this.textareaValue;
if (text) {
this.markdownPreviewLoading = true; this.markdownPreviewLoading = true;
this.markdownPreview = __('Loading…'); this.markdownPreview = __('Loading…');
axios axios
.post(this.markdownPreviewPath, { text }) .post(this.markdownPreviewPath, { text: this.textareaValue })
.then(response => this.renderMarkdown(response.data)) .then(response => this.renderMarkdown(response.data))
.catch(() => new Flash(__('Error loading markdown preview'))); .catch(() => new Flash(__('Error loading markdown preview')));
} else { } else {
......
...@@ -79,6 +79,7 @@ describe('IssuableForm', () => { ...@@ -79,6 +79,7 @@ describe('IssuableForm', () => {
markdownDocsPath: wrapper.vm.descriptionHelpPath, markdownDocsPath: wrapper.vm.descriptionHelpPath,
addSpacingClasses: false, addSpacingClasses: false,
showSuggestPopover: true, showSuggestPopover: true,
textareaValue: '',
}); });
expect(descriptionFieldEl.find('textarea').exists()).toBe(true); expect(descriptionFieldEl.find('textarea').exists()).toBe(true);
expect(descriptionFieldEl.find('textarea').attributes('placeholder')).toBe( expect(descriptionFieldEl.find('textarea').attributes('placeholder')).toBe(
......
...@@ -7,6 +7,7 @@ import axios from '~/lib/utils/axios_utils'; ...@@ -7,6 +7,7 @@ import axios from '~/lib/utils/axios_utils';
const markdownPreviewPath = `${TEST_HOST}/preview`; const markdownPreviewPath = `${TEST_HOST}/preview`;
const markdownDocsPath = `${TEST_HOST}/docs`; const markdownDocsPath = `${TEST_HOST}/docs`;
const textareaValue = 'testing\n123';
function assertMarkdownTabs(isWrite, writeLink, previewLink, wrapper) { function assertMarkdownTabs(isWrite, writeLink, previewLink, wrapper) {
expect(writeLink.element.parentNode.classList.contains('active')).toBe(isWrite); expect(writeLink.element.parentNode.classList.contains('active')).toBe(isWrite);
...@@ -20,23 +21,11 @@ function createComponent() { ...@@ -20,23 +21,11 @@ function createComponent() {
markdownDocsPath, markdownDocsPath,
markdownPreviewPath, markdownPreviewPath,
isSubmitting: false, isSubmitting: false,
textareaValue,
}, },
slots: { slots: {
textarea: '<textarea>testing\n123</textarea>', textarea: `<textarea>${textareaValue}</textarea>`,
}, },
template: `
<field-component
markdown-preview-path="${markdownPreviewPath}"
markdown-docs-path="${markdownDocsPath}"
:isSubmitting="false"
>
<textarea
slot="textarea"
v-model="text">
<slot>this is a test</slot>
</textarea>
</field-component>
`,
}); });
return wrapper; return wrapper;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment