-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
feat: support mentions onece delete #48389
Open
CooperHash
wants to merge
7
commits into
ant-design:feature
Choose a base branch
from
CooperHash:feat-mention
base: feature
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d6c7b4e
feat: support mentions onece delete
CooperHash 53323e9
fix
CooperHash 78d6fb3
fix
CooperHash 2ebf56e
add docs
CooperHash b6050fc
Improve deletion logic
CooperHash d09f34c
fix lint
CooperHash 733cc88
Merge branch 'feature' into feat-mention
afc163 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
## zh-CN | ||
|
||
一次删除按键删除。 | ||
|
||
## en-US | ||
|
||
Once Delete. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Mentions } from 'antd'; | ||
import React from 'react'; | ||
|
||
const App: React.FC = () => ( | ||
<Mentions | ||
style={{ width: '100%' }} | ||
defaultValue="@afc163@zombieJ" | ||
itemOnceDelete | ||
options={[ | ||
{ | ||
value: 'afc163', | ||
label: 'afc163', | ||
}, | ||
{ | ||
value: 'zombieJ', | ||
label: 'zombieJ', | ||
}, | ||
{ | ||
value: 'yesmeck', | ||
label: 'yesmeck', | ||
}, | ||
]} | ||
/> | ||
); | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ | |
import useVariant from '../form/hooks/useVariants'; | ||
import Spin from '../spin'; | ||
import useStyle from './style'; | ||
import KeyCode from 'rc-util/lib/KeyCode'; | ||
import useMergedState from 'rc-util/lib/hooks/useMergedState'; | ||
|
||
export const { Option } = RcMentions; | ||
|
||
|
@@ -45,14 +47,15 @@ | |
status?: InputStatus; | ||
options?: MentionsOptionProps[]; | ||
popupClassName?: string; | ||
itemOnceDelete?: boolean; | ||
/** | ||
* @since 5.13.0 | ||
* @default "outlined" | ||
*/ | ||
variant?: Variant; | ||
} | ||
|
||
export interface MentionsRef extends RcMentionsRef {} | ||
export interface MentionsRef extends RcMentionsRef { } | ||
|
||
interface MentionsConfig { | ||
prefix?: string | string[]; | ||
|
@@ -75,13 +78,21 @@ | |
children, | ||
notFoundContent, | ||
options, | ||
itemOnceDelete = false, | ||
status: customStatus, | ||
allowClear = false, | ||
popupClassName, | ||
style, | ||
variant: customVariant, | ||
value, | ||
defaultValue, | ||
onChange, | ||
...restProps | ||
} = props; | ||
const [mergedValue, setMergedValue] = useMergedState('', { | ||
defaultValue, | ||
value, | ||
}); | ||
const [focused, setFocused] = React.useState(false); | ||
const innerRef = React.useRef<MentionsRef>(null); | ||
const mergedRef = composeRef(ref, innerRef); | ||
|
@@ -121,6 +132,58 @@ | |
setFocused(false); | ||
}; | ||
|
||
const onInternalChange = (data: string) => { | ||
setMergedValue(data) | ||
onChange?.(data); | ||
} | ||
|
||
const splitValue = (data: string) => { | ||
if (props.prefix) { | ||
const prefix = Array.isArray(props.prefix) ? props.prefix : [props.prefix]; | ||
prefix.forEach((pre) => { | ||
if (pre) { | ||
data = data.split(pre).join(` ${pre}`); | ||
} | ||
}); | ||
} else { | ||
data = data.split('@').join(' @'); | ||
} | ||
return data.trim(); | ||
} | ||
|
||
const onKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => { | ||
if (e.keyCode === KeyCode.BACKSPACE && itemOnceDelete) { | ||
if (mergedValue) { | ||
const data = splitValue(mergedValue) | ||
const cursorPosition = e.currentTarget.selectionStart; | ||
const valueList = data.trimEnd().split(props.split ?? ' '); | ||
let length = 0; | ||
let itemIndex = -1; | ||
for (let i = 0; i < valueList.length; i++) { | ||
length += valueList[i].length + (props.split?.length ?? 1); | ||
if (length >= cursorPosition) { | ||
itemIndex = i; | ||
break; | ||
} | ||
} | ||
const len = valueList.slice(itemIndex + 1, valueList.length).join('').length | ||
valueList.splice(itemIndex, 1); | ||
if (valueList.length !== 0) { | ||
const res = valueList.join(props.split ?? ' ') + (props.split ?? ' '); | ||
setMergedValue(res); | ||
setTimeout(() => { | ||
if (innerRef.current && innerRef.current.textarea) { | ||
innerRef.current.textarea.selectionStart -= len | ||
innerRef.current.textarea.selectionEnd -= len | ||
} | ||
}) | ||
} else { | ||
setMergedValue(''); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const notFoundContentEle = React.useMemo<React.ReactNode>(() => { | ||
if (notFoundContent !== undefined) { | ||
return notFoundContent; | ||
|
@@ -181,8 +244,11 @@ | |
allowClear={mergedAllowClear} | ||
direction={direction} | ||
style={{ ...contextMentions?.style, ...style }} | ||
value={mergedValue} | ||
{...restProps} | ||
filterOption={mentionsfilterOption} | ||
onKeyDown={onKeyDown} | ||
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. 事件丢了,应该还需要透一下 |
||
onChange={onInternalChange} | ||
onFocus={onFocus} | ||
onBlur={onBlur} | ||
dropdownClassName={classNames(popupClassName, rootClassName, hashId, cssVarCls, rootCls)} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
这个 prop 不符合 antd 的 naming standard,可以先看看现有的 API 命名有没有可以复用的