Skip to content

Commit

Permalink
Un-revert commit from @vitorqb
Browse files Browse the repository at this point in the history
  • Loading branch information
andrerpena committed Apr 27, 2020
1 parent 11ae7fc commit 9caab66
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
6 changes: 6 additions & 0 deletions demo/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,14 @@ export class App extends React.Component<{}, AppState> {
classes={{
suggestionsDropdown: "bbbb"
}}
textAreaComponent={CustomTextArea}
/>
</div>
);
}
}

const CustomTextArea = React.forwardRef((props: any, ref: any) => {
const style = { ...props.style, backgroundColor: "blue" };
return <textarea {...props} style={style} ref={ref} />;
});
19 changes: 18 additions & 1 deletion src/components/ReactMde.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { classNames } from "../util/ClassNames";
import { ChildProps } from "../child-props";
import { CommandOrchestrator } from "../commandOrchestrator";
import { Refs } from "../refs";
import { DetailedHTMLFactory, TextareaHTMLAttributes } from "react";

export interface ReactMdeProps {
value: string;
Expand All @@ -37,6 +38,20 @@ export interface ReactMdeProps {
loadSuggestions?: (text: string) => Promise<Suggestion[]>;
childProps?: ChildProps;
l18n?: L18n;
/**
* Custom textarea component. "textAreaComponent" can be any React component which
* props are a subset of the props of an HTMLTextAreaElement
*/
textAreaComponent?: React.ClassType<
Partial<
DetailedHTMLFactory<
TextareaHTMLAttributes<HTMLTextAreaElement>,
HTMLTextAreaElement
>
>,
any,
any
>;
}

export interface ReactMdeState {
Expand Down Expand Up @@ -155,7 +170,8 @@ export class ReactMde extends React.Component<ReactMdeProps, ReactMdeState> {
selectedTab,
generateMarkdownPreview,
loadSuggestions,
suggestionTriggerCharacters
suggestionTriggerCharacters,
textAreaComponent
} = this.props;

const finalChildProps = childProps || {};
Expand Down Expand Up @@ -189,6 +205,7 @@ export class ReactMde extends React.Component<ReactMdeProps, ReactMdeState> {
refObject={this.finalRefs.textarea}
onChange={this.handleTextChange}
readOnly={readOnly}
textAreaComponent={textAreaComponent}
textAreaProps={childProps && childProps.textArea}
height={this.state.editorHeight}
value={value}
Expand Down
28 changes: 25 additions & 3 deletions src/components/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Suggestion } from "../types";
import { insertText } from "../util/InsertTextAtPosition";
import { mod } from "../util/Math";
import { SuggestionsDropdown } from "./SuggestionsDropdown";
import { DetailedHTMLFactory, TextareaHTMLAttributes } from "react";

export interface MentionState {
status: "active" | "inactive" | "loading";
Expand Down Expand Up @@ -41,13 +42,26 @@ export interface TextAreaProps {
text: string,
triggeredBy: string
) => Promise<Suggestion[]>;
/**
* Custom textarea component. "textAreaComponent" can be any React component which
* props are a subset of the props of an HTMLTextAreaElement
*/
textAreaComponent?: React.ClassType<
Partial<
DetailedHTMLFactory<
TextareaHTMLAttributes<HTMLTextAreaElement>,
HTMLTextAreaElement
>
>,
any,
any
>;
textAreaProps?: Partial<
React.DetailedHTMLProps<
React.TextareaHTMLAttributes<HTMLTextAreaElement>,
HTMLTextAreaElement
>
>;

/**
* On keydown, the TextArea will trigger "onPossibleKeyCommand" as an opportunity for React-Mde to
* execute a command. If a command is executed, React-Mde should return true, otherwise, false.
Expand Down Expand Up @@ -344,7 +358,8 @@ export class TextArea extends React.Component<TextAreaProps, TextAreaState> {
value,
suggestionTriggerCharacters,
loadSuggestions,
suggestionsDropdownClasses
suggestionsDropdownClasses,
textAreaComponent
} = this.props;

const suggestionsEnabled =
Expand All @@ -353,9 +368,16 @@ export class TextArea extends React.Component<TextAreaProps, TextAreaState> {
loadSuggestions;

const { mention } = this.state;

const TextAreaComponent = (textAreaComponent ||
"textarea") as DetailedHTMLFactory<
TextareaHTMLAttributes<HTMLTextAreaElement>,
HTMLTextAreaElement
>;

return (
<div className="mde-textarea-wrapper">
<textarea
<TextAreaComponent
className={classNames("mde-text", classes)}
style={{ height }}
ref={this.props.refObject}
Expand Down

0 comments on commit 9caab66

Please sign in to comment.