summernote.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import $ from 'jquery';
  4. export class SummerNote extends Component {
  5. static propTypes = {
  6. options: PropTypes.object,
  7. onChange: PropTypes.func,
  8. value: PropTypes.string,
  9. };
  10. static defaultProps = {
  11. options: {},
  12. onChange: () => {},
  13. value: '',
  14. };
  15. componentDidMount() {
  16. require('react-summernote');
  17. require('react-summernote/dist/react-summernote.css');
  18. require('bootstrap/dist/js/bootstrap');
  19. $(this.element).summernote({
  20. ...this.props.options,
  21. callbacks: {
  22. onChange: this.handleChange,
  23. },
  24. });
  25. $(this.element).summernote('code', this.props.value);
  26. }
  27. componentDidUpdate(prevProps) {
  28. if (this.props.value !== prevProps.value) {
  29. $(this.element).summernote('code', this.props.value);
  30. }
  31. }
  32. componentWillUnmount() {
  33. $(this.element).summernote('destroy');
  34. }
  35. handleChange = (value) => {
  36. this.props.onChange(value);
  37. };
  38. setRef = (node) => this.element = node;
  39. render() {
  40. return (
  41. <div ref={this.setRef}></div>
  42. );
  43. }
  44. }
  45. export default SummerNote;