• SearchBar搜索栏
    • 规则
  • 代码演示
  • API
  • SearchBar Instance methods

    SearchBar搜索栏

    一般可位于 NavBar 下方,通过『取消按钮』退出激活状态。

    规则

    • 应该在 placeholder 里提供提示文字,提醒用户输入相关内容,比如:双11特卖。
    • 在搜索栏下方,可提供有用的标签文案,帮助用户通过点击直接完成输入,比如:列出一些最近搜索的关键词。

    代码演示

    基本用法

    最简单的用法。

    1. import { Component, ElementRef, Renderer } from '@angular/core';
    2. @Component({
    3. selector: 'demo-search-bar-basic',
    4. template: `
    5. <div class="am-demo-page">
    6. <div class="am-demo-bd">
    7. <div class="am-wingblank am-wingblank-lg">
    8. <div class="sub-title">Normal</div>
    9. </div>
    10. <div style="border-bottom: 1px solid #ddd;">
    11. <SearchBar [placeholder]="'Search'" [maxLength]="8" ></SearchBar>
    12. </div>
    13. <div class="am-whitespace am-whitespace-md"></div>
    14. <div class="am-wingblank am-wingblank-lg">
    15. <div class="sub-title">AutoFocus when enter page</div>
    16. </div>
    17. <div style="border-bottom: 1px solid #ddd;">
    18. <SearchBar [placeholder]="'自动获取光标'" [setFocus]="autoFocus"></SearchBar>
    19. </div>
    20. <div class="am-wingblank am-wingblank-lg">
    21. <div class="sub-title">Focus by operation</div>
    22. </div>
    23. <div style="border-bottom: 1px solid #ddd;">
    24. <SearchBar [placeholder]="'手动获取获取光标'" [setFocus]="focusObj"></SearchBar>
    25. </div>
    26. <a role="button" class="am-button" (click)="handleClick()"><span>click to focus</span></a>
    27. <div class="am-wingblank am-wingblank-lg">
    28. <div class="sub-title">Show cancel button</div>
    29. </div>
    30. <div style="border-bottom: 1px solid #ddd;">
    31. <SearchBar [(ngModel)]="value"
    32. [placeholder]="'Search'"
    33. [showCancelButton]="true"
    34. (onBlur)="blur()"
    35. (onFocus)="focus()"
    36. (onCancel)="cancel()"
    37. (onClear)="clear(value)"
    38. (onSubmit)="submit(value)"
    39. (onChange)="change($event)"
    40. ></SearchBar>
    41. </div>
    42. </div>
    43. </div>
    44. `,
    45. styles: [
    46. `
    47. .am-search {
    48. border-bottom: 1px solid #ddd;
    49. }
    50. .sub-title {
    51. color: #888;
    52. font-size: 14px;
    53. padding: 30px 0 18px 0;
    54. }
    55. .am-wingblank .am-wingblank-lg {
    56. margin-left: 15px;
    57. margin-right: 15px;
    58. }
    59. .am-wingblank {
    60. margin-left: 8px;
    61. margin-right: 8px;
    62. }
    63. .am-button {
    64. display: block;
    65. outline: 0 none;
    66. -webkit-appearance: none;
    67. -webkit-box-sizing: border-box;
    68. box-sizing: border-box;
    69. padding: 0;
    70. text-align: center;
    71. font-size: 18px;
    72. height: 47px;
    73. line-height: 47px;
    74. overflow: hidden;
    75. text-overflow: ellipsis;
    76. word-break: break-word;
    77. white-space: nowrap;
    78. color: #000;
    79. background-color: #fff;
    80. border: 1px solid #ddd;
    81. border-radius: 5px;
    82. margin: 0 15px 15px 15px;
    83. }
    84. `
    85. ]
    86. })
    87. export class DemoSearchBarBasicComponent {
    88. value: string = '美食';
    89. autoFocus = {
    90. focusValue: true
    91. };
    92. focusObj = {
    93. focusValue: false,
    94. date: new Date()
    95. };
    96. constructor(private _element: ElementRef, private _renderer: Renderer) {}
    97. change($event) {
    98. console.log($event, 'onChange');
    99. }
    100. submit(value) {
    101. console.log(value, 'onSubmit');
    102. }
    103. clear(value) {
    104. console.log(value, 'onClear');
    105. }
    106. focus() {
    107. console.log('onFocus');
    108. }
    109. blur() {
    110. console.log('onBlur');
    111. }
    112. cancel() {
    113. console.log('onCancel');
    114. }
    115. handleClick() {
    116. this.focusObj = {
    117. focusValue: true,
    118. date: new Date()
    119. };
    120. }
    121. }

    SearchBar 搜索栏 - 图1

    API

    属性说明类型默认值
    ngModel当前值,可双向绑定String
    ngModelChange值改变时回调(val: string): void
    defaultValue搜索框的默认值String
    value搜索框的当前值String
    placeholderplaceholderString
    onSubmitsubmit 事件 (点击键盘的 enter)(val: string): void
    onChangechange 事件的回调(val: string): void
    onFocusfocus 事件的回调(): void
    onBlurblur 事件的回调(): void
    onCancel点击取消按钮触发 (不再自动清除输入框的文字)(val: string): void
    showCancelButton是否一直显示取消按钮boolfalse
    cancelText定制取消按钮的文字String取消
    disabled设置禁用boolfalse
    onClear点击 clear 图标触发(val: string): void
    maxLength最多允许输入的字符个数number-

    SearchBar Instance methods

    属性说明类型默认值
    focus使 SearchBar 聚焦(): void-