[英]How to clear react tags input field?
在react-tag-autocomplete
lib 中,需要添加手動建議。 因此,我為建議列表創建了名為ReactTagsSuggestions
的新組件。
如何清除ReactTags
的輸入字段
<ReactTags
id="form-share"
tags={selectedTags}
// suggestions={tagSuggestions}
handleAddition={this.props.handleAddition}
handleDelete={this.props.handleDelete}
placeholder={this.props.tags.length ? '' : tr('Share with users, groups, and channels')}
tagComponent={this.selectedTags}
handleInputChange={this.handleInputChange}
autofocus={false}
/>
<ReactTagsSuggestions
suggestionList={tagSuggestions}
showSuggestions={tagSuggestions.length > 0}
checkToShare={this.props.checkToShare}
/>
如果你創建一個這樣的函數怎么樣
handleDelete = () => {
this.setState({ tags: [] })
}
並將其傳遞給 ReactTags 組件
<ReactTags
.
.
handleDelete={ this.handleDelete }
/>
?
要清理ReactTags
的輸入字段,您可以將selectedTags
重置為空數組:
clearTag() {
this.setState({ selectedTags: [] });
}
// Call the clearTag somwhere in your app
<button type="button" onClick={this.clearTag}>Clear tags</button>
為時已晚,但可能對其他人有用。 我認為你的意思是清除輸入值而不是標簽。 這是ReactTags
的useState
和inputValue
道具的解決方案
const [inputValue, setInputValue] = useState('');
<ReactTags
inputValue={inputValue}
handleAddition={(...args) => {
myHandleAddition(args);
setInputValue('');
}}
handleInputChange={(tag) => {
if (tag.slice(-1) === ' ') {
myHandleAddition([{ id: tag, text: tag }]);
setInputValue('');
} else {
setInputValue(tag);
}
}}
/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.