Skip to content

Commit

Permalink
Merge pull request #27 from niuware/development
Browse files Browse the repository at this point in the history
Refactor 'EditorControls' component
  • Loading branch information
niuware authored Oct 5, 2019
2 parents 93abf87 + 3d48471 commit e7f71e7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mui-rte",
"version": "1.3.0",
"version": "1.3.1",
"description": "Material-UI Rich Text Editor and Viewer",
"keywords": [
"material-ui",
Expand Down
1 change: 0 additions & 1 deletion src/MUIRichTextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ class MUIRichTextEditor extends React.Component<IMUIRichTextEditorProps, IMUIRic
url = linkInstance.getData().url
urlKey = linkKey
}
console.log(document.getElementById("mui-rte-image-control-toolbar"))
this.setState({
urlValue: url,
urlKey: urlKey,
Expand Down
23 changes: 14 additions & 9 deletions src/components/EditorControls.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FunctionComponent } from 'react'
import React, { FunctionComponent, useState, useEffect } from 'react'
import { EditorState } from 'draft-js'
import FormatBoldIcon from '@material-ui/icons/FormatBold'
import FormatItalicIcon from '@material-ui/icons/FormatItalic'
Expand Down Expand Up @@ -175,7 +175,6 @@ const STYLE_TYPES: TStyleType[] = [
]

interface IBlockStyleControlsProps extends KeyString {
children?: React.ReactNode
editorState: EditorState
controls?: Array<TEditorControl>
customControls?: TCustomControl[]
Expand All @@ -192,14 +191,18 @@ interface IBlockStyleControlsProps extends KeyString {
className?: string
}

const EditorControls: FunctionComponent<IBlockStyleControlsProps> = (props: IBlockStyleControlsProps) => {
const EditorControls: FunctionComponent<IBlockStyleControlsProps> = (props) => {
const [availableControls, setAvailableControls] = useState(props.controls ? [] : STYLE_TYPES)
const selectionInfo = getSelectionInfo(props.editorState)
let filteredControls = STYLE_TYPES
if (props.controls) {
filteredControls = []

useEffect(() => {
if (!props.controls) {
return
}
const filteredControls: TStyleType[] = []
const controls = props.controls.filter((control, index) => props.controls!.indexOf(control) >= index)
controls.forEach(name => {
let style = STYLE_TYPES.find(style => style.name === name)
const style = STYLE_TYPES.find(style => style.name === name)
if (style) {
filteredControls.push(style)
}
Expand All @@ -218,10 +221,12 @@ const EditorControls: FunctionComponent<IBlockStyleControlsProps> = (props: IBlo
}
}
})
}
setAvailableControls(filteredControls)
}, [props.controls, props.customControls])

return (
<div className={props.className}>
{filteredControls.map(style => {
{availableControls.map(style => {
if (props.toolbarMode &&
(style.type !== "inline" && (style.name !== "link" && style.name !== "clear"))) {
return null
Expand Down
8 changes: 4 additions & 4 deletions test/EditorControls.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { shallow } from 'enzyme'
import { shallow, mount } from 'enzyme'
import { assert, expect } from 'chai'
import { EditorState } from 'draft-js'
import EditorControls, { TEditorControl } from '../src/components/EditorControls'
Expand All @@ -13,7 +13,7 @@ describe('<EditorControls />', () => {
})

it('should render all controls', () => {
const wrapper = shallow(
const wrapper = mount(
<EditorControls
editorState={editorState}
onClear={() => {}}
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('<EditorControls />', () => {
"Code Block",
"Underline"
]
const wrapper = shallow(
const wrapper = mount(
<EditorControls
editorState={editorState}
controls={controls}
Expand All @@ -64,7 +64,7 @@ describe('<EditorControls />', () => {
})

it('should not render controls', () => {
const wrapper = shallow(
const wrapper = mount(
<EditorControls
editorState={editorState}
controls={[]}
Expand Down

0 comments on commit e7f71e7

Please sign in to comment.