Angular2.0实现的搜索框(重新按UX封装了样式)()

之前封成了个指令,本来想本模块自己用,后来别的同事也要用,干脆封装成一个模块,供外部调用

使用(加了一个属性)

<ky-search [disabled]="true" (searchContent)="searchContentChange($event)"> </ky-search>


html

<div class="tableSearch">
  <input class="inputSearch" type="text" #term (keyup)="search(term.value)" placeholder="请输入需要搜索内容" required/>
  <span  (click)="delSearch()"></span>
</div>

less部分--这部分请穆兄指导了下,写的很精彩,学习了

.tableSearch {
  width: 240px;
  position: relative;
  float: right;
}

.inputPrime {
  border-radius: 2px;
  padding-left: 6px;
  height: 30px;
  width: 240px;
  font-size: 12px;
  color: #666666;
}

.isPrimeForSpan {
  position: absolute;
  right: 10px;
  top: 7px;
  width: 16px;
  height: 16px;
}

.inputSearchIcon(@icon){
  background: url(@icon) no-repeat right 0;
  cursor: pointer;
}

.tableSearch .inputSearch:extend(.inputPrime) {
  border: 1px solid @stroke-gray;
  background: @default-white;
  &:disabled {
    background: @fill-gray;
  }
  &:focus {
    border: 1px solid @default-blue;
  }
  & + span:extend(.isPrimeForSpan){
    .inputSearchIcon("images/icon_search_mirror.png");
  }
  &:valid + span:extend(.isPrimeForSpan){
    .inputSearchIcon("images/icon_search_delete.png");
  }
  &:invalid:focus + span:extend(.isPrimeForSpan) {
    .inputSearchIcon("images/icon_search_mirror.png");
  }
  &:valid:focus + span:hover:extend(.isPrimeForSpan) {
    .inputSearchIcon("images/icon_search_delete_hover.png");
  }
}

ts部分

import { Component, Output, EventEmitter, OnInit } from '@angular/core';
import { Subject } from 'rxjs/Subject';

@Component({
  moduleId: module.id,
  selector: 'ky-search',
  styleUrls: ['./ky-search.css'],
  templateUrl: './ky-search.html',
})

export class KySearchComponent implements OnInit {
  //@Input() placeholder:string;
  @Output() searchContent = new EventEmitter<Object>();

  searchTermStream = new Subject<string>();


  search(term:string) {
    this.searchTermStream.next(term);
  }


  delSearch() {
    this.searchTermStream.next('');
    $('.inputSearch').val('');
  }

  getSearchStream() {
    let that = this;
    return that.searchTermStream
      .debounceTime(300)
      .distinctUntilChanged();
  }

  ngOnInit() {
    let that = this;

    that.getSearchStream().subscribe((res)=> {
      this.searchContent.emit(res);
    });

  }


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值