Skip to content

Commit

Permalink
Merge pull request #279 from TingYinHelen/next
Browse files Browse the repository at this point in the history
添加autoComplete
  • Loading branch information
yuu2lee4 authored Apr 2, 2018
2 parents 944b817 + 05cf846 commit 3d36194
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 2 deletions.
177 changes: 177 additions & 0 deletions src/docs/zh-cn/auto-complete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<script>
export default {
data: () => ({
options: [],
results: [],
dataSource: [
{ value: 'Burns Bay Road', label: 'Burns Bay Road' },
{ value: 'Downing Street', label: 'Downing Street' },
{ value: 'Wall Street', label: 'Wall Street'},
],
}),
methods: {
select(value) {
console.log('select', value)
},
search(value) {
this.options = !value ? [] : [
{ value: value, label: value},
{ value: value + value, label: value + value},
{ value: value + value + value, label: value + value + value},
]
},
handelSearch(value) {
if(!value || value.indexOf('@') >=0 ){
this.results = [];
} else {
this.results = ['gmail.com', '163.com', 'qq.com'].map(domain => ({value: `${value}@${domain}`, label: `${value}@${domain}`}));
}
},
filter(val, item){
const input = val.toLocaleUpperCase();
return item.value.toUpperCase().indexOf(input) !== -1;
},
}
}
</script>


# AutoComplete 自动完成

输入框自动完成功能。

## 何时使用

需要自动完成时。

## 代码演示

::: demo
<summary>
#### 基本
基本使用。
</summary>

```html
<template>
<div>
<v-auto-complete :data="options" @select="select" @search="search" style="width:200px;"></v-auto-complete>
</div>
</template>

<script>
export default {
data: ()=> ({
options: [],
}),
methods: {
search(val) {
this.options = [{
value: value,
label: value
}, {
value: value + value,
label: value + value
}, {
value: value + value + value,
label: value + value + value
}];
},
select(value) {
console.log('select value', value)
}
}
}
</script>
```
:::

::: demo
<summary>
#### 自定义选项
通过默认scopedSlot自定义选项显示内容
</summary>

```html
<template>
<v-auto-complete @search="handelSearch" :data="results" style="width:200px;">
<template slot-scope="{data}">
{{data.label}}-1
</template>
</v-auto-complete>
</template>

<script>
export default {
data: ()=> ({
results: [],
}),
methods: {
handelSearch(value) {
if(!value || value.indexOf('@') >=0 ){
this.results = [];
} else {
this.results = ['gmail.com', '163.com', 'qq.com'].map(domain => ({value: `${value}@${domain}`, label: `${value}@${domain}`}));
}
}
}
}
</script>
```
:::

::: demo
<summary>
#### 不区分大小写
不区分大小写的 AutoComplete
</summary>

```html
<template>
<v-auto-complete :data="dataSource" :filter="filter" style="width:200px;"></v-auto-complete>
</template>

<script>
export default {
data: ()=> ({
dataSource: [
{ value: 'Burns Bay Road', label: 'Burns Bay Road' },
{ value: 'Downing Street', label: 'Downing Street' },
{ value: 'Wall Street', label: 'Wall Street'},
],
}),
methods: {
filter(val, item) {
const input = val.toLocaleUpperCase();
return item.value.toUpperCase().indexOf(input) !== -1;
}
}
}
</script>
```
:::

## API

### AutoComplete Props
| 属性 | 说明 | 类型 | 默认值 |
|------------|----------------|-------------------|-------------|
| value | 指定默认选中的条目 | string/array | - |
| data | 自动完成的数据源 | array | [] |
| placeholder | 输入框提示 | string | - |
| filter | 搜索过滤函数,返回Boolean | 搜索过滤函数,返回Boolean | - |

### Data Props
| 属性 | 说明 | 类型 | 默认值 |
|------------|----------------|-------------------|-------------|
| label | 选项的标签或组名(作为标签时可通过select的label属性修改;作为组名时只支持一级,可通过select的groupLabel属性修改) | string | - |
| disabled | 是否禁用 | boolean | false |
| options | 分组的数据 | array | - |

### AutoComplete Events
| 事件 | 说明 | 参数 |
|------------|----------------|------------|
| search | 搜索补全项的时候调用 | value |
| select | 被选中时调用,参数为选中项的 value 值 | value |
| blur | blur事件 | - |
| focus | focus事件 | - |
1 change: 1 addition & 0 deletions src/docs/zh-cn/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -527,3 +527,4 @@
| change | 选择的值发生变化的时候触发,默认返回value,如需返回整个option,请设置optionOnChange | value |
| focus | focus事件 | - |
| blur | blur事件 | - |
| search | 文本框值变化时回调 | value |
4 changes: 4 additions & 0 deletions src/routers.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export default [{
path: 'cascader',
name: 'cascader',
component: resolve => require(['./docs/zh-cn/cascader.md'], resolve),
}, {
path: 'autoComplete',
name: 'autoComplete',
component: resolve => require(['./docs/zh-cn/auto-complete.md'], resolve),
}, {
path: 'collapse',
name: 'collapse',
Expand Down
3 changes: 3 additions & 0 deletions src/views/components.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ export default {
}, {
groupName: 'Data Entry',
list: [{
name: 'AutoComplete 自动完成',
link: { name: 'autoComplete' },
}, {
name: 'Cascader 级联选择',
link: { name: 'cascader' },
}, {
Expand Down
60 changes: 60 additions & 0 deletions vb/components/auto-complete/auto-complete.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<template>
<v-select
ref="test"
search
:data="data"
v-model="value"
@search="search"
@change="select"
:placeholder="placeholder"
:filter="filter"
@blur="blur"
@focus="focus"
>
<template slot-scope="{data}">
<slot :data="data">{{data.label}}</slot>
</template>
</v-select>
</template>

<script lang="babel">
import { t } from '../../locale';
export default {
name: 'AutoComplete',
props: {
data: {
type: Array,
default: () => [],
},
placeholder: {
type: String,
default: () => t('autoComplete.placeholder'),
},
filter: Function,
},
data() {
return {
value: '',
};
},
created() {
const { value } = this.$attrs;
this.value = value;
},
methods: {
search(val) {
this.$emit('search', val);
},
select(val) {
this.$emit('select', val);
},
blur() {
this.$emit('blur');
},
focus() {
this.$emit('focus');
},
},
};
</script>
4 changes: 4 additions & 0 deletions vb/components/auto-complete/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import './style/index.less';
import autoComplete from './auto-complete';

export default autoComplete;
Empty file.
6 changes: 4 additions & 2 deletions vb/components/select/select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div :class="selectionCls" role="combobox" aria-autocomplete="list" aria-haspopup="true" aria-expanded="false" tabindex="0" @click="toggleDropdown">
<div class="ant-select-selection__rendered ant-select__dropdown" :tabindex="search ? false : '0'" @focus="$emit('focus')" @blur="$emit('blur')">
<template v-if="labels">
<ul v-if="multiple">
<ul v-if="multiple">
<li v-for="(text,i) in labels" unselectable="unselectable" class="ant-select-selection__choice" :title="text" style="user-select: none" :key="text">
<div class="ant-select-selection__choice__content">{{text}}</div>
<span class="ant-select-selection__choice__remove" @click="remove(i,text)"></span>
Expand Down Expand Up @@ -32,7 +32,7 @@
</span>
</div>
<transition name="slide-up">
<div ref="dropdown" v-show="show" style="overflow: auto" :style="dropdownStyle" :class="dropdownCls">
<div ref="dropdown" v-show="show" style="overflow: auto;" :style="dropdownStyle" :class="dropdownCls">
<div style="overflow: auto;">
<ul class="ant-select-dropdown-menu ant-select-dropdown-menu-vertical ant-select-dropdown-menu-root" role="menu" aria-activedescendant="">
<li v-if="loading" unselectable="unselectable" class="ant-select-dropdown-menu-item ant-select-dropdown-menu-item-disabled" role="menuitem" aria-selected="false" style="user-select: none;">{{loadingText}}</li>
Expand Down Expand Up @@ -188,6 +188,7 @@ export default {
innerValue(val) {
this.$emit('input', val);
this.dispatch('FormItem', 'form.change', [val]);
if (this.optionOnChange) {
this.$nextTick(() => {
this.$emit('change', this.getOption(val));
Expand All @@ -205,6 +206,7 @@ export default {
}
},
searchVal(val) {
this.$emit('search', val);
if (this.multiple) {
this.$nextTick(() => { this.multipleSearchStyle = val ? { width: `${this.$refs.searchMirror.offsetWidth + 2}px` } : {}; });
}
Expand Down
3 changes: 3 additions & 0 deletions vb/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import transfer from './components/transfer';
import timeline from './components/timeline';
import carousel from './components/carousel';
import treeSelect from './components/tree-select';
import autoComplete from './components/auto-complete';
import clickoutside from './directives/clickoutside';
import tooltipd from './directives/tooltip';
import { use, i18n } from './locale';
Expand Down Expand Up @@ -136,6 +137,7 @@ const components = {
carousel,
carouselItem: carousel.item,
treeSelect,
autoComplete,
};

for (const item of Object.values(components)) {
Expand Down Expand Up @@ -224,6 +226,7 @@ export {
timeline,
carousel,
treeSelect,
autoComplete,
};

export default {
Expand Down
3 changes: 3 additions & 0 deletions vb/locale/lang/zh-CN.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export default {
placeholder: '请选择',
loadingText: '加载中...',
},
autoComplete: {
placeholder: '请输入',
},
timePicker: {
placeholder: '选择时间',
},
Expand Down

0 comments on commit 3d36194

Please # to comment.