Skip to content

Commit

Permalink
Fix #3454: Editor respect max length property (#3459)
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware authored Oct 10, 2022
1 parent 0d0bc80 commit 3d3c9c8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
6 changes: 6 additions & 0 deletions api-generator/components/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ const EditorProps = [
type: 'any',
default: 'null',
description: 'Style and modules of the toolbar.'
},
{
name: 'maxLength',
type: 'number',
default: 'null',
description: 'Maximum number of characters the editor will accept.'
}
];

Expand Down
12 changes: 9 additions & 3 deletions components/doc/editor/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { memo } from 'react';
import Link from 'next/link';
import { TabView, TabPanel } from '../../lib/tabview/TabView';
import { useLiveEditorTabs } from '../common/liveeditor';
import React, { memo } from 'react';
import { TabPanel, TabView } from '../../lib/tabview/TabView';
import { CodeHighlight } from '../common/codehighlight';
import { DevelopmentSection } from '../common/developmentsection';
import { useLiveEditorTabs } from '../common/liveeditor';

const EditorDoc = memo(() => {
const sources = {
Expand Down Expand Up @@ -287,6 +287,12 @@ const header = (
<td>null</td>
<td>Style and modules of the toolbar.</td>
</tr>
<tr>
<td>maxLength</td>
<td>number</td>
<td>null</td>
<td>Maximum number of characters the editor will accept.</td>
</tr>
</tbody>
</table>
</div>
Expand Down
11 changes: 10 additions & 1 deletion components/lib/editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ export const Editor = React.memo(
}
}

if (props.maxLength) {
const length = quill.current.getLength();

if (length > props.maxLength) {
quill.current.deleteText(props.maxLength, length);
}
}

if (props.onTextChange) {
props.onTextChange({
htmlValue: html,
Expand Down Expand Up @@ -213,5 +221,6 @@ Editor.defaultProps = {
headerTemplate: null,
onTextChange: null,
onSelectionChange: null,
onLoad: null
onLoad: null,
maxLength: null
};
3 changes: 0 additions & 3 deletions components/lib/editor/editor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ interface EditorSelectionChangeParams {
}

export interface EditorProps extends Omit<React.DetailedHTMLProps<React.InputHTMLAttributes<HTMLDivElement>, HTMLDivElement>, 'ref'> {
id?: string;
value?: string;
style?: React.CSSProperties;
className?: string;
placeholder?: string;
readOnly?: boolean;
modules?: any;
Expand Down

0 comments on commit 3d3c9c8

Please sign in to comment.