簡體   English   中英

按下搜索圖標后如何展開搜索欄

[英]How to expand search bar after presses on search icon

我通過某種方式創建了一個搜索圖標,我只是想知道當有人按下搜索欄圖標時如何展開它。

  import { TouchableOpacity,View, Image} from 'react-native';
  import { SearchBar } from 'react-native-elements';
  export default class Search extends Component{
     onClick(){
      return <SearchBar/> // [![Seach Image][1]][1]not working
}

render(){
    // not worrking
    let search = <SearchBar/>;
    return(
        <View>
            <TouchableOpacity
                onPress={() => {
                    return search
                }}
            >
                <Image
                    source={require('../images/tabs/search.png')}
                    style={{height: 40, width: 60}}
                    resizeMode={'contain'}
                />
            </TouchableOpacity>
        </View>
     )
   }
 }

SearchImage

您應該在組件中添加狀態以控制標題的行為

import { TouchableOpacity, View, Image } from 'react-native';
import { SearchBar } from 'react-native-elements';
export default class Search extends Component {
  constructor(props) {
    super(props);
    this.onClick = this.onClick.bind(this);
    this.state = {
      showSearchBar: false, // control what ever to render the searchbar or just the icon
    };
  }
  onClick() {
    let { showSearchBar } = this.state;
    this.setState({
      showSearchBar: !showSearchBar,
    });
  }

  render() {
    const { showSearchBar } = this.state;
    return (
      <View>
        {!showSearchBar ? (
          <TouchableOpacity onPress={this.onClick}>
            <Image
              source={require('../images/tabs/search.png')}
              style={{ height: 40, width: 60 }}
              resizeMode={'contain'}
            />
          </TouchableOpacity>
        ) : (
          <SearchBar />
        )}
      </View>
    );
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM