Skip to content

Commit

Permalink
fix searchBar onClear #1721 (#1731)
Browse files Browse the repository at this point in the history
* fix searchBar onClear #1721

* optimize code

* remove unused comment

* modify changelog
  • Loading branch information
pingan1927 authored Aug 28, 2017
1 parent 7fedf89 commit 42003c4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ timeline: true
* Major version release is not included in this schedule for breadking change and new features.

---

- **Bug fix**
- Fix `SeachBar` in specific Client when `onClear` event was triggered, the `SearchBar` can't be auto focused. ([#1721](https://github.com/ant-design/ant-design-mobile/issues/1721))

## 1.6.3

`2017-08-15`
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ timeline: true
---


## 1.6.4

- **Bug fix**
- 修复 `SeachBar` 在特定客户端内`onClear`事件触发时无法自动`focus`到搜索栏的问题。([#1721](https://github.com/ant-design/ant-design-mobile/issues/1721))

## 1.6.3

Expand Down
39 changes: 20 additions & 19 deletions components/search-bar/index.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export default class SearchBar extends React.Component<SearchBarProps, SearchBar
rightBtnInitMarginleft: any;
firstFocus: any;
scrollIntoViewTimeout: any;
blurFromOnClear: any;
onBlurTimeout: any;

constructor(props) {
super(props);
Expand All @@ -31,7 +33,7 @@ export default class SearchBar extends React.Component<SearchBarProps, SearchBar
const initBtn = window.getComputedStyle(this.refs.rightBtn);
this.rightBtnInitMarginleft = initBtn['margin-left'];
if ((this.props.autoFocus || this.state.focused) && navigator.userAgent.indexOf('AlipayClient') > 0) {
(this.refs as any).searchInput.focus();
this.refs.searchInput.focus();
}
this.componentDidUpdate();
}
Expand All @@ -52,7 +54,7 @@ export default class SearchBar extends React.Component<SearchBarProps, SearchBar
}
}
if (this.state.focused) {
(this.refs as any).searchInput.focus();
this.refs.searchInput.focus();
}
}

Expand All @@ -74,14 +76,18 @@ export default class SearchBar extends React.Component<SearchBarProps, SearchBar
clearTimeout(this.scrollIntoViewTimeout);
this.scrollIntoViewTimeout = null;
}
if (this.onBlurTimeout) {
clearTimeout(this.onBlurTimeout);
this.onBlurTimeout = null;
}
}

onSubmit = (e) => {
e.preventDefault();
if (this.props.onSubmit) {
this.props.onSubmit(this.state.value);
}
(this.refs as any).searchInput.blur();
this.refs.searchInput.blur();
}

onChange = (e) => {
Expand Down Expand Up @@ -125,11 +131,14 @@ export default class SearchBar extends React.Component<SearchBarProps, SearchBar
}

onBlur = () => {
setTimeout(() => {
this.setState({
focus: false,
});
}, 0);
this.onBlurTimeout = setTimeout(() => {
if (!this.blurFromOnClear) {
this.setState({
focus: false,
});
}
this.blurFromOnClear = false;
}, 50);
if (!('focused' in this.props)) {
this.setState({
focused: false,
Expand All @@ -141,6 +150,8 @@ export default class SearchBar extends React.Component<SearchBarProps, SearchBar
}

onClear = () => {
this.blurFromOnClear = true;

if (!('value' in this.props)) {
this.setState({ value: '' });
}
Expand All @@ -150,17 +161,7 @@ export default class SearchBar extends React.Component<SearchBarProps, SearchBar
if (this.props.onChange) {
this.props.onChange('');
}
// 加上setTimeout 为了解决Android的兼容性问题。
// https://github.com/ant-design/ant-design-mobile/issues/1341
// 只有支付宝系客户端才完美支持
const ua = navigator.userAgent;
if (ua.indexOf('AlipayClient') > 0 && (ua.match(/Android/i) || ua.indexOf('AliApp(AM') < 0)) {
// 口碑掌柜iOS 只有5.2以上版本才支持
setTimeout(() => {
(this.refs as any).searchInput.focus();
this.componentDidUpdate();
}, 300);
}
this.refs.searchInput.focus();
}

onCancel = () => {
Expand Down

0 comments on commit 42003c4

Please sign in to comment.