[英]Selecting all text inside react-bootstrap-typeahead on inner input focus
沒有公共的API或規范的方法可以做到這一點,但是這肯定是可能的。 您需要做的是獲取輸入節點,然后設置選擇范圍 。 這是一個例子:
class MyTypeahead extends React.Component {
render() {
return (
<Typeahead
onFocus={(event) => {
const inputNode = document.getElementById('typeahead-input');
inputNode.setSelectionRange(0, inputNode.value.length);
}}
inputProps={{id: 'typeahead-input'}}
/>
);
}
}
編輯:獲取輸入節點的替代方法:
從事件:
class MyTypeahead extends React.Component {
render() {
return (
<Typeahead
onFocus={(event) => {
const inputNode = event.target;
inputNode.setSelectionRange(0, inputNode.value.length);
}}
...
/>
);
}
}
使用參考:
class MyTypeahead extends React.Component {
render() {
return (
<Typeahead
onFocus={(event) => {
const inputNode = this.typeahead.getInstance().getInput();
inputNode.setSelectionRange(0, inputNode.value.length);
}}
ref={(el) => this.typeahead = el}
...
/>
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.