forked from HospitalRun/components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(richtext): adds RichText component
fix HospitalRun#29
- Loading branch information
1 parent
0dad0ca
commit d541c93
Showing
7 changed files
with
123 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = {} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React from 'react' | ||
import { Editor } from '@tinymce/tinymce-react' | ||
|
||
import 'tinymce/tinymce' | ||
|
||
// Basic tinyMCE theme & skins required for editor to display | ||
import 'tinymce/themes/silver/theme.min' | ||
import 'tinymce/skins/ui/oxide/skin.min.css' | ||
import 'tinymce/skins/ui/oxide/content.min.css' | ||
|
||
// Import required plugins | ||
import 'tinymce/plugins/autolink/plugin.min' | ||
import 'tinymce/plugins/lists/plugin.min' | ||
import 'tinymce/plugins/link/plugin.min' | ||
import 'tinymce/plugins/table/plugin.min' | ||
import 'tinymce/plugins/paste/plugin.min' | ||
import 'tinymce/plugins/charmap/plugin.min' | ||
|
||
interface Props { | ||
// id of the rich text editor component | ||
id?: string | ||
// Initial value of the rich text editor | ||
value?: string | ||
// Defines whether the rich text editor should be enabled/disabled (default = false) | ||
disabled?: boolean | ||
// Height of the rich text editor | ||
height?: number | ||
// Method run on the editors onEditorChange event. Returns editor content as HTML. | ||
onChange?: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void | ||
} | ||
|
||
const RichText = (props: Props) => { | ||
const { id, value, disabled, height, onChange } = props | ||
|
||
return ( | ||
<Editor | ||
id={id} | ||
initialValue={value} | ||
init={{ | ||
height: height || 500, | ||
menubar: true, | ||
statusbar: false, | ||
// skin & content_css are set to 'false' to avoid tinyMCE looking to fetch files when they are already imported above. | ||
skin: false, | ||
// eslint disabled on next line due to TinyMCE option attribute naming. | ||
// eslint-disable-next-line @typescript-eslint/camelcase | ||
content_css: false, | ||
plugins: [`autolink lists link table paste charmap`], | ||
toolbar: [ | ||
`undo redo | formatselect fontselect fontsizeselect | bold italic underline sub sup backcolor | | ||
alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table link removeformat`, | ||
], | ||
branding: false, | ||
}} | ||
disabled={disabled} | ||
onEditorChange={(content) => onChange && onChange(content)} | ||
/> | ||
) | ||
} | ||
|
||
export { RichText } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './RichText' |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React from 'react' | ||
|
||
import { storiesOf } from '@storybook/react' | ||
|
||
import { RichText } from '../src' | ||
|
||
storiesOf('RichText', module) | ||
.addParameters({ | ||
info: { | ||
inline: true, | ||
}, | ||
}) | ||
.addDecorator((storyFn) => <div style={{ textAlign: 'center' }}>{storyFn()}</div>) | ||
.add('Default rich text editor', () => <RichText />) | ||
.add('Disabled rich text editor', () => <RichText disabled />) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as React from 'react' | ||
import { shallow } from 'enzyme' | ||
import { Editor } from '@tinymce/tinymce-react' | ||
import { RichText } from '../src' | ||
|
||
describe('TextField', () => { | ||
it('renders a RichText without crashing', () => { | ||
const wrapper = shallow(<RichText />) | ||
|
||
expect(wrapper.find(Editor)).toHaveLength(1) | ||
}) | ||
|
||
it('renders a RichText with attributes', () => { | ||
const id = 'richtext' | ||
const wrapper = shallow(<RichText id={id} />) | ||
|
||
expect(wrapper.find(Editor)).toHaveLength(1) | ||
expect(wrapper.find(Editor).filterWhere((item) => item.prop('id') === id)).toHaveLength(1) | ||
}) | ||
|
||
it('renders a non-disabled RichText', () => { | ||
const wrapper = shallow(<RichText value="richtext" />) | ||
const input = wrapper.find(Editor) | ||
|
||
expect(input.prop('disabled')).not.toBe(true) | ||
}) | ||
|
||
it('renders a disabled RichText', () => { | ||
const wrapper = shallow(<RichText value="richtext" disabled />) | ||
const input = wrapper.find(Editor) | ||
|
||
expect(input.prop('disabled')).toBe(true) | ||
}) | ||
}) |