@@ -2,11 +2,12 @@ import InputLabel from '@material-ui/core/InputLabel';
22import Typography from '@material-ui/core/Typography' ;
33import FieldCoreBase , { IStateFieldBase } from '@react-form-fields/core/components/FieldCoreBase' ;
44import ValidationContextRegister from '@react-form-fields/core/components/ValidationContextRegister' ;
5- import { getConfig } from '@react-form-fields/core/config' ;
65import * as React from 'react' ;
76
7+ import { getConfig } from '../../config' ;
88import { WithStyles } from '../../decorators/withStyles' ;
99import { IBaseFieldProps } from '../../interfaces/props' ;
10+ import * as styles from './style.css' ;
1011
1112interface IState extends IStateFieldBase {
1213 focused : boolean ;
@@ -31,16 +32,75 @@ interface IProps extends IBaseFieldProps {
3132 }
3233} ) )
3334export default class FieldHtml extends FieldCoreBase < IProps , IState > {
35+ dependenciesLoaded : Promise < string > ;
36+ instance : any ;
37+ textareaRef = React . createRef < HTMLTextAreaElement > ( ) ;
3438
35- onChange = ( value : any ) => {
39+ constructor ( props : IProps ) {
40+ super ( props ) ;
41+ this . dependenciesLoaded = this . loadDependencies ( ) ;
42+ }
43+
44+ loadDependencies = async ( ) => {
45+ if ( ! window . jQuery ) {
46+ window . jQuery = window . $ = await import ( 'jquery' ) as any ;
47+ }
48+
49+ const { loadLocale, loadPlugins } = getConfig ( ) . trumbowyg ;
50+ const [ svgPath ] = await Promise . all ( [
51+ import ( 'trumbowyg/dist/ui/icons.svg' ) ,
52+ import ( 'trumbowyg/dist/trumbowyg.min.js' ) ,
53+ import ( 'trumbowyg/dist/ui/trumbowyg.min.css' ) ,
54+ ] ) ;
55+
56+ await Promise . all ( [
57+ import ( 'trumbowyg/dist/plugins/history/trumbowyg.history.min.js' ) ,
58+ import ( 'trumbowyg/dist/plugins/cleanpaste/trumbowyg.cleanpaste.min.js' ) ,
59+ loadLocale ? loadLocale ( ) : Promise . resolve ( ) ,
60+ ...( loadPlugins ? loadPlugins ( ) : [ ] ) ,
61+ ] ) ;
62+
63+ return svgPath ;
64+ }
65+
66+ async componentDidMount ( ) {
67+ const svgPath = await this . dependenciesLoaded ;
68+ this . instance = window . jQuery ( this . textareaRef . current ) ;
69+
70+ this . instance . trumbowyg ( {
71+ svgPath,
72+ useComposition : false ,
73+ autogrow : true ,
74+ ...getConfig ( ) . trumbowyg . config
75+ } ) . on ( 'tbwfocus' , ( ) => {
76+ this . onFocus ( ) ;
77+ } ) . on ( 'tbwchange' , ( ) => {
78+ this . onChange ( this . instance . trumbowyg ( 'html' ) ) ;
79+ } ) . on ( 'tbwblur' , ( ) => {
80+ this . onBlur ( this . instance . trumbowyg ( 'html' ) ) ;
81+ } ) ;
82+
83+ this . instance . trumbowyg ( 'html' , this . props . value ) ;
84+ }
85+
86+ componentDidUpdate ( ) {
87+ this . instance . trumbowyg ( this . props . disabled ? 'disable' : 'enable' ) ;
88+
89+ if ( this . instance . trumbowyg ( 'html' ) !== this . props . value ) {
90+ this . instance . trumbowyg ( 'html' , this . props . value ) ;
91+ }
92+ }
93+
94+ onChange = ( value : string ) => {
3695 getConfig ( ) . validationOn === 'onChange' && this . setState ( { showError : true } ) ;
3796 this . props . onChange ( value ) ;
3897 }
3998
40- onBlur = ( e : React . SyntheticEvent ) => {
99+ onBlur = ( value : string ) => {
41100 getConfig ( ) . validationOn === 'onBlur' && this . setState ( { showError : true } ) ;
42101 this . setState ( { focused : false } ) ;
43- this . props . onBlur && this . props . onBlur ( e ) ;
102+ this . props . onChange ( value ) ;
103+ this . props . onBlur && this . props . onBlur ( null ) ;
44104 }
45105
46106 onFocus = ( ) => {
@@ -49,11 +109,10 @@ export default class FieldHtml extends FieldCoreBase<IProps, IState> {
49109
50110 render ( ) {
51111 const { focused } = this . state ;
52- const { classes, label, helperText } = this . props ;
53- /* disabled, onChange, onBlur, placeholder */
112+ const { classes, label, helperText, placeholder, disabled } = this . props ;
54113
55114 return (
56- < div >
115+ < div className = { styles . component } >
57116
58117 < ValidationContextRegister field = { this } />
59118
@@ -67,6 +126,8 @@ export default class FieldHtml extends FieldCoreBase<IProps, IState> {
67126 }
68127 </ div >
69128
129+ < textarea ref = { this . textareaRef } placeholder = { placeholder } disabled = { disabled } />
130+
70131 </ div >
71132 ) ;
72133 }
0 commit comments