Last active
April 5, 2021 19:55
-
-
Save JoshCheek/c45c41065542abbe7ebd2aa866a7577f to your computer and use it in GitHub Desktop.
TrixEditor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component, Fragment } from 'react' | |
import PropTypes from 'prop-types' | |
const randomString = () => | |
Math.random().toString(36).substring(2, 15) | |
export default class TrixEditor extends Component { | |
static propTypes = { | |
customRef: PropTypes.shape({ current: PropTypes.instanceOf(Element) }), | |
name: PropTypes.string, | |
trixHtml: PropTypes.string, | |
placeholder: PropTypes.string.isRequired, | |
directUploadUrl: PropTypes.string.isRequired, | |
blobUrlTemplate: PropTypes.string.isRequired, | |
} | |
constructor(props) { | |
super(props) | |
this.domId = randomString() | |
} | |
render() { | |
return <Fragment> | |
{/* taken from the result of running `f.rich_text_area :text, placeholder: "Write about it here"` */} | |
<input | |
ref={this.props.customRef} | |
type="hidden" | |
name={this.props.name} | |
id={this.domId} | |
value={this.getValue()} | |
/> | |
<trix-editor | |
placeholder={this.props.placeholder} | |
input={this.domId} | |
className="trix-content form-control" | |
data-direct-upload-url={this.props.directUploadUrl} | |
data-blob-url-template={this.props.blobUrlTemplate} | |
></trix-editor> | |
</Fragment> | |
} | |
getValue() { | |
const ref = this.props.customRef | |
if(ref && ref.current) | |
return ref.current.value | |
else | |
return this.props.trixHtml | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment