-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
keyUp/keyDown event dispatch and textarea style support #4
base: master
Are you sure you want to change the base?
Changes from 3 commits
4209d8c
5e79755
904e42a
b7298d5
8e76aee
1d06e84
ab96dbc
24134a3
d76572e
3d4324e
bcc2020
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,15 @@ type BaseTextareaAttrs = Omit< | |
|
||
export type Placement = 'top' | 'bottom'; | ||
|
||
export interface MentionKeyBoardEventPayload { | ||
measuring: MentionsState['measuring']; | ||
} | ||
|
||
export type MentionKeyBoardEventHandler = ( | ||
event: React.KeyboardEvent<HTMLTextAreaElement>, | ||
payload?: MentionKeyBoardEventPayload, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Omit 掉再自定义就行了。 |
||
) => void; | ||
|
||
export interface MentionsProps extends BaseTextareaAttrs { | ||
autoFocus?: boolean; | ||
className?: string; | ||
|
@@ -41,6 +50,8 @@ export interface MentionsProps extends BaseTextareaAttrs { | |
onChange?: (text: string) => void; | ||
onSelect?: (option: OptionProps, prefix: string) => void; | ||
onSearch?: (text: string, prefix: string) => void; | ||
onKeyDown?: MentionKeyBoardEventHandler; | ||
onKeyUp?: MentionKeyBoardEventHandler; | ||
onFocus?: React.FocusEventHandler<HTMLTextAreaElement>; | ||
onBlur?: React.FocusEventHandler<HTMLTextAreaElement>; | ||
} | ||
|
@@ -121,6 +132,11 @@ class Mentions extends React.Component<MentionsProps, MentionsState> { | |
public onKeyDown: React.KeyboardEventHandler<HTMLTextAreaElement> = event => { | ||
const { which } = event; | ||
const { activeIndex, measuring } = this.state; | ||
const { onKeyDown } = this.props; | ||
|
||
if (onKeyDown) { | ||
onKeyDown(event, { measuring }); | ||
} | ||
zombieJ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Skip if not measuring | ||
if (!measuring) { | ||
|
@@ -162,14 +178,18 @@ class Mentions extends React.Component<MentionsProps, MentionsState> { | |
public onKeyUp: React.KeyboardEventHandler<HTMLTextAreaElement> = event => { | ||
const { key, which } = event; | ||
const { measureText: prevMeasureText, measuring } = this.state; | ||
const { prefix = '', onSearch, validateSearch } = this.props; | ||
const { prefix = '', onSearch, validateSearch, onKeyUp } = this.props; | ||
const target = event.target as HTMLTextAreaElement; | ||
const selectionStartText = getBeforeSelectionText(target); | ||
const { location: measureIndex, prefix: measurePrefix } = getLastMeasureIndex( | ||
selectionStartText, | ||
prefix, | ||
); | ||
|
||
if (onKeyUp) { | ||
onKeyUp(event, { measuring }); | ||
} | ||
|
||
// Skip if match the white key list | ||
if ([KeyCode.ESC, KeyCode.UP, KeyCode.DOWN, KeyCode.ENTER].indexOf(which) !== -1) { | ||
return; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
boolean