question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change in mode causes form submit

See original GitHub issue

Describe the bug

I have the editor embedded in a form (thanks to the changes in v2.1). I would like to allow switching between wysisyg and markdown modes, but clicking those buttons submits the parent form.

This may be a Formik integration issue and not a bug per se, but I’m struggling to find a way to intercept or change this behavior.

To Reproduce

Here is a simple form where I am seeing the behavior.

import { Formik } from "formik";
import React from "react";
import Form from "react-bootstrap/Form";
import * as yup from "yup";

import { Editor } from "@toast-ui/react-editor";

import SubmitButton from "../../components/SubmitButton";
import { PageContent } from "../../models/Policies";
import { IPageContentViewProps } from "./PageContentView";

export interface IPageContentEditProps extends IPageContentViewProps {
    Save: (policy: PageContent) => void;
}

const schema = yup.object({
    title: yup.string().max(120).required(),
    // content: yup.string().required(),
});

const PageContentEdit: React.FC<IPageContentEditProps> = (props) => {
    const pageContent = props.pageContent;
    const editorRef = React.createRef<Editor>();

    return (
        <div>
            <Formik
                validationSchema={schema}
                onSubmit={(values) => {
                    const newModel = new PageContent(values);
                    newModel.id = pageContent.id;
                    newModel.content = editorRef.current?.getInstance().getMarkdown() || pageContent.content;
                    props.Save(newModel);
                }}
                initialValues={pageContent}>
                {({ handleSubmit, handleChange, handleBlur, values, touched, errors }) => (
                    <Form noValidate onSubmit={handleSubmit}>
                        <Form.Group controlId="policy.Title">
                            <Form.Label>Title</Form.Label>
                            <Form.Control
                                name="title"
                                placeholder="Title"
                                value={values.title}
                                isValid={touched.title && !errors.title}
                                isInvalid={!!errors.title}
                                onChange={handleChange}
                                onBlur={handleBlur}
                            />
                            <Form.Control.Feedback type="invalid">{errors.title}</Form.Control.Feedback>
                        </Form.Group>
                        <Editor
                            initialValue={values.content}
                            previewStyle="tab"
                            height="300px"
                            initialEditType="wysiwyg"
                            useCommandShortcut={true}
                            useDefaultHTMLSanitizer={true}
                            hideModeSwitch={false}
                            ref={editorRef}
                        />
                        <SubmitButton />
                    </Form>
                )}
            </Formik>
        </div>
    );
};

export default PageContentEdit;

Expected behavior

An editor positioned within a form should not trigger submit when the mode is changed.

Additional context

tui.editor version: 2.1.0 formik version: 2.0.8

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
seonim-ryucommented, May 8, 2020

@finleysg In order to solve the problem you are struggling with, you need to be aware of the event that occur each time you click the button to change the WYSIWYG and Markdown modes in the editor and you can submit a form from that event.

If what I understand is correct, it is not exposed in the API documentation, but I think you can use the changeMode event. This event is used inside the Editor, but it can be used by calling it like this. Below is an example when using the Editor.

const editor = new Editor({
  el: document.querySelector('#editor'),
  previewStyle: 'vertical',
  height: '500px',
  initialValue: content
});

editor.on('changeMode', (ev) => {
  console.log(`current mode: ${ev}`);
});

If you use React wrapper, you can use it like this.

class MyComponent extends React.Component {
  handleChangeMode = ev => {
    console.log(`current mode: ${ev}`);
  };

  render() {
    return (
      <Editor
        previewStyle="vertical"
        height="400px"
        initialEditType="markdown"
        onChangeMode={this.handleChangeMode}
      />
    );
  }
}

Can you confirm that this is correct?

3reactions
loveminecommented, May 7, 2020

may be modeSwitch.js 93, 96 line button markup should use type=“button” attribute. like below unless it submit the form.

  <button type="button" class="te-switch-button markdown">${i18n.get('Markdown')}</button>

refer : MDN Button

Read more comments on GitHub >

github_iconTop Results From Across the Web

Making all form inputs to be in readonly mode while ...
When I click the submit button it hides the submit button and shows submitting text. But it doesn't change the input property to...
Read more >
Solved: Refresh form data with new data after submit witho...
Solved: Hi I have a form on a canvas and I have a button on the screen that lets the user edit the...
Read more >
Power Apps Form Modes - NewForm, EditForm and ...
We change the mode of a form by using the functions NewForm, EditForm, ViewForm and we reset a form with Reset Form function....
Read more >
<input type="submit"> - HTML: HyperText Markup Language
A string indicating the HTTP method to use when submitting the form's data; this value overrides any method attribute given on the owning...
Read more >
Power Apps Forms - Updates, Unsaved & Submit Confirmation
In this video, we will explore Power Apps Forms Properties Updates, Unsaved and how to showcase a summary of the Form data prior...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found