Skip to content

Commit

Permalink
Add 'maxLength' property (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
niuware authored Mar 4, 2020
1 parent 61c7bdc commit a2f49a4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ Object.assign(defaultTheme, {
|inlineToolbarControls|`string[]`|optional|List of controls to display in the inline toolbar. Available values are: "bold", "italic", "underline", "strikethrough", "highlight", "link", "clear", and user defined inline controls. If not provided and `inlineToolbar` is `true` the following inline styles will be displayed: bold, italic, underline and clear.|
|keyCommands|`TKeyCommand[]`|optional|Defines an array of `TKeyCommand` objects for adding key bindings to the editor.|
|draftEditorProps|`TDraftEditorProps`|optional|Defines an object containing specific `draft-js` `Editor` properties.|
|maxLength|`number`|optional|Sets the maximum characters count that can be input into the editor.|


<br />
Expand Down
2 changes: 2 additions & 0 deletions examples/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import LoadHTML from './load-html'
import ResetValue from './reset-value'
import AtomicCustomBlock from './atomic-custom-block'
import KeyBindings from './key-bindings'
import MaxLength from './max-length'

const App = () => {

Expand All @@ -38,6 +39,7 @@ const App = () => {
<button onClick={() => setSample(<LoadHTML />)}>Load from HTML</button>
<button onClick={() => setSample(<ResetValue />)}>Reset value</button>
<button onClick={() => setSample(<KeyBindings />)}>Key Bindings</button>
<button onClick={() => setSample(<MaxLength />)}>Max length</button>
<div style={{
margin: "20px 0"
}}>
Expand Down
18 changes: 18 additions & 0 deletions examples/max-length/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react'
import MUIRichTextEditor from '../../'

const save = (data: string) => {
console.log(data)
}

const MaxLength = () => {
return (
<MUIRichTextEditor
label="You can only type 10 characters..."
maxLength={10}
onSave={save}
/>
)
}

export default MaxLength
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.13.0",
"version": "1.14.0",
"description": "Material-UI Rich Text Editor and Viewer",
"keywords": [
"material-ui",
Expand Down
14 changes: 12 additions & 2 deletions src/MUIRichTextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ type TKeyCommand = {
callback: (state: EditorState) => EditorState
}



interface IMUIRichTextEditorProps extends WithStyles<typeof styles> {
id?: string
value?: any
Expand All @@ -106,6 +104,7 @@ interface IMUIRichTextEditorProps extends WithStyles<typeof styles> {
inlineToolbarControls?: Array<TToolbarControl>
draftEditorProps?: TDraftEditorProps
keyCommands?: TKeyCommand[]
maxLength?: number
onSave?: (data: string) => void
onChange?: (state: EditorState) => void
}
Expand Down Expand Up @@ -319,6 +318,16 @@ const MUIRichTextEditor: RefForwardingComponent<any, IMUIRichTextEditorProps> =
}
}

const handleBeforeInput = (): DraftHandleValue => {
if (props.maxLength) {
const length = editorState.getCurrentContent().getPlainText('').length
if (length >= props.maxLength) {
return "handled"
}
}
return "not-handled"
}

const handleFocus = () => {
setFocus(true)
setTimeout(() => (editorRef.current as any).focus(), 0)
Expand Down Expand Up @@ -752,6 +761,7 @@ const MUIRichTextEditor: RefForwardingComponent<any, IMUIRichTextEditorProps> =
onChange={handleChange}
readOnly={props.readOnly}
handleKeyCommand={handleKeyCommand}
handleBeforeInput={handleBeforeInput}
keyBindingFn={keyBindingFn}
ref={editorRef}
{...props.draftEditorProps}
Expand Down

0 comments on commit a2f49a4

Please sign in to comment.