[improvement] use component name (#2450)

This commit is contained in:
neverland 2019-01-06 23:08:14 +08:00 committed by GitHub
parent e437e9c10d
commit bf50db9ea0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 87 additions and 76 deletions

View File

@ -1,13 +1,12 @@
import createSfc from '../utils/create'; import { use } from '../utils';
import createBem from '../utils/bem'; import Icon from '../icon';
import Popup from '../mixins/popup'; import Loading from '../loading';
import PopupMixin from '../mixins/popup';
const bem = createBem('van-actionsheet'); const [sfc, bem] = use('actionsheet');
export default createSfc({ export default sfc({
name: 'actionsheet', mixins: [PopupMixin],
mixins: [Popup],
props: { props: {
title: String, title: String,
@ -53,7 +52,7 @@ export default createSfc({
const Header = () => ( const Header = () => (
<div class={[bem('header'), 'van-hairline--top-bottom']}> <div class={[bem('header'), 'van-hairline--top-bottom']}>
{title} {title}
<icon name="close" onClick={onCancel} /> <Icon name="close" onClick={onCancel} />
</div> </div>
); );
@ -69,7 +68,7 @@ export default createSfc({
}} }}
> >
{item.loading ? ( {item.loading ? (
<loading class={bem('loading')} size="20px" /> <Loading class={bem('loading')} size="20px" />
) : ( ) : (
[ [
<span class={bem('name')}>{item.name}</span>, <span class={bem('name')}>{item.name}</span>,

View File

@ -1,10 +1,10 @@
import createSfc from '../utils/create'; import { use } from '../utils';
import Picker from '../picker'; import Picker from '../picker';
import PickerMixin from '../mixins/picker'; import PickerMixin from '../mixins/picker';
export default createSfc({ const [sfc, bem] = use('area');
name: 'area',
export default sfc({
mixins: [PickerMixin], mixins: [PickerMixin],
props: { props: {
@ -189,7 +189,7 @@ export default createSfc({
return ( return (
<Picker <Picker
ref="picker" ref="picker"
class="van-area" class={bem()}
show-toolbar show-toolbar
value-key="name" value-key="name"
title={this.title} title={this.title}

View File

@ -1,8 +1,8 @@
import createSfc from '../utils/create'; import { use } from '../utils';
export default createSfc({ const [sfc, bem] = use('badge-group');
name: 'badge-group',
export default sfc({
props: { props: {
activeKey: { activeKey: {
type: [Number, String], type: [Number, String],
@ -23,6 +23,6 @@ export default createSfc({
}, },
render(h) { render(h) {
return <div class="van-hairline--top-bottom van-badge-group">{this.$slots.default}</div>; return <div class={[bem(), 'van-hairline--top-bottom']}>{this.$slots.default}</div>;
} }
}); });

View File

@ -1,12 +1,9 @@
import { use } from '../utils';
import Info from '../info'; import Info from '../info';
import createSfc from '../utils/create';
import createBem from '../utils/bem';
const bem = createBem('van-badge'); const [sfc, bem] = use('badge');
export default createSfc({
name: 'badge',
export default sfc({
props: { props: {
url: String, url: String,
info: [String, Number], info: [String, Number],

View File

@ -3,7 +3,7 @@
exports[`renders demo correctly 1`] = ` exports[`renders demo correctly 1`] = `
<div> <div>
<div> <div>
<div class="van-hairline--top-bottom van-badge-group"><a class="van-badge van-badge--select van-hairline"> <div class="van-badge-group van-hairline--top-bottom"><a class="van-badge van-badge--select van-hairline">
<div class="van-badge__text">标签名称 <div class="van-badge__text">标签名称
<!----> <!---->
</div> </div>

View File

@ -1,11 +1,9 @@
import createSfc from '../utils/create'; import { use } from '../utils';
import createBem from '../utils/bem'; import Loading from '../loading';
const bem = createBem('van-button'); const [sfc, bem] = use('button');
export default createSfc({
name: 'button',
export default sfc({
props: { props: {
text: String, text: String,
block: Boolean, block: Boolean,
@ -59,7 +57,7 @@ export default createSfc({
onClick={this.onClick} onClick={this.onClick}
> >
{this.loading ? ( {this.loading ? (
<loading size="20px" color={this.type === 'default' ? undefined : ''} /> <Loading size="20px" color={this.type === 'default' ? undefined : ''} />
) : ( ) : (
<span class={bem('text')}>{this.$slots.default || this.text}</span> <span class={bem('text')}>{this.$slots.default || this.text}</span>
)} )}

View File

@ -1,12 +1,9 @@
import createSfc from '../utils/create'; import { use, isDef } from '../utils';
import createBem from '../utils/bem';
import Tag from '../tag'; import Tag from '../tag';
const bem = createBem('van-card'); const [sfc, bem] = use('card');
export default createSfc({
name: 'card',
export default sfc({
props: { props: {
tag: String, tag: String,
desc: String, desc: String,
@ -25,7 +22,7 @@ export default createSfc({
}, },
render(h) { render(h) {
const { thumb, isDef, $slots: slots } = this; const { thumb, $slots: slots } = this;
const Thumb = (slots.thumb || thumb) && ( const Thumb = (slots.thumb || thumb) && (
<a href={this.thumbLink} class={bem('thumb')}> <a href={this.thumbLink} class={bem('thumb')}>

View File

@ -1,8 +1,8 @@
import createSfc from '../utils/create-basic'; import { use } from '../utils';
export default createSfc({ const [sfc, bem] = use('cell-group');
name: 'cell-group',
export default sfc({
props: { props: {
border: { border: {
type: Boolean, type: Boolean,
@ -12,7 +12,7 @@ export default createSfc({
render(h) { render(h) {
return ( return (
<div class={['van-cell-group', { 'van-hairline--top-bottom': this.border }]}> <div class={[bem(), { 'van-hairline--top-bottom': this.border }]}>
{this.$slots.default} {this.$slots.default}
</div> </div>
); );

View File

@ -1,46 +1,18 @@
/** /**
* Create a basic component with common options * Create a basic component with common options
*/ */
import '../locale'; import useSfc from './use/sfc';
import createBem from './bem'; import useBem from './use/bem';
import i18n from '../mixins/i18n'; import i18n from '../mixins/i18n';
import { isDef, camelize } from '.'; import { isDef } from '.';
function install(Vue) {
const { name } = this;
Vue.component(name, this);
Vue.component((camelize(`-${name}`)), this);
}
function returnArray() {
return [];
}
function defaultProps(props) {
Object.keys(props).forEach(key => {
if (props[key] === Array) {
props[key] = {
type: Array,
default: returnArray
};
} else if (props[key] === Number) {
props[key] = {
type: Number,
default: 0
};
}
});
}
export default function (sfc) { export default function (sfc) {
sfc.name = 'van-' + sfc.name; sfc = useSfc('van-' + sfc.name)(sfc);
sfc.install = sfc.install || install;
sfc.mixins = sfc.mixins || []; sfc.mixins = sfc.mixins || [];
sfc.mixins.push(i18n); sfc.mixins.push(i18n);
sfc.methods = sfc.methods || {}; sfc.methods = sfc.methods || {};
sfc.methods.isDef = isDef; sfc.methods.isDef = isDef;
sfc.methods.b = createBem(sfc.name); sfc.methods.b = useBem(sfc.name);
sfc.props && defaultProps(sfc.props);
return sfc; return sfc;
} }

View File

@ -1,4 +1,5 @@
import Vue from 'vue'; import Vue from 'vue';
import use from './use';
const isServer = Vue.prototype.$isServer; const isServer = Vue.prototype.$isServer;
@ -37,6 +38,7 @@ function range(num, min, max) {
} }
export { export {
use,
get, get,
range, range,
isObj, isObj,

View File

@ -0,0 +1,7 @@
import useBem from './bem';
import useSfc from './sfc';
export default function (name) {
name = 'van-' + name;
return [useSfc(name), useBem(name)];
}

39
packages/utils/use/sfc.js Normal file
View File

@ -0,0 +1,39 @@
/**
* Create a basic component with common options
*/
import '../../locale';
import { camelize } from '..';
const arrayProp = {
type: Array,
default: () => []
};
const numberProp = {
type: Number,
default: 0
};
function defaultProps(props) {
Object.keys(props).forEach(key => {
if (props[key] === Array) {
props[key] = arrayProp;
} else if (props[key] === Number) {
props[key] = numberProp;
}
});
}
function install(Vue) {
const { name } = this;
Vue.component(name, this);
Vue.component((camelize(`-${name}`)), this);
}
export default name => sfc => {
sfc.name = name;
sfc.install = install;
sfc.props && defaultProps(sfc.props);
return sfc;
};