From df771a8fc66e261f85154a1095d640c7cf1f1c27 Mon Sep 17 00:00:00 2001 From: Amour1688 Date: Sat, 17 Oct 2020 12:13:55 +0800 Subject: [PATCH] chore: refactor space --- components/space/index.tsx | 111 ++++++++++-------- components/space/style/{index.js => index.ts} | 0 2 files changed, 60 insertions(+), 51 deletions(-) rename components/space/style/{index.js => index.ts} (100%) diff --git a/components/space/index.tsx b/components/space/index.tsx index 8cd65fa33..410103d5d 100644 --- a/components/space/index.tsx +++ b/components/space/index.tsx @@ -1,6 +1,8 @@ -import { inject, App, CSSProperties, SetupContext } from 'vue'; +import { inject, App, defineComponent, PropType } from 'vue'; +import PropTypes from '../_util/vue-types'; import { filterEmpty } from '../_util/props-util'; import { defaultConfigProvider, SizeType } from '../config-provider'; +import { tuple } from '../_util/type'; const spaceSize = { small: 8, @@ -8,67 +10,74 @@ const spaceSize = { large: 24, }; -export interface SpaceProps { - prefixCls?: string; - className?: string; - style?: CSSProperties; - size?: SizeType | number; - direction?: 'horizontal' | 'vertical'; - // No `stretch` since many components do not support that. - align?: 'start' | 'end' | 'center' | 'baseline'; -} +const Space = defineComponent({ + name: 'ASpace', + props: { + prefixCls: PropTypes.string, + size: { + type: [String, Number] as PropType, + }, + direction: PropTypes.oneOf(tuple('horizontal', 'vertical')), + align: PropTypes.oneOf(tuple('start', 'end', 'center', 'baseline')), + }, + setup(props, { slots }) { + const configProvider = inject('configProvider', defaultConfigProvider); + const { + align, + size = 'small', + direction = 'horizontal', + prefixCls: customizePrefixCls, + } = props; -const Space = (props: SpaceProps, { slots }: SetupContext) => { - const configProvider = inject('configProvider', defaultConfigProvider); - const { align, size = 'small', direction = 'horizontal', prefixCls: customizePrefixCls } = props; + const { getPrefixCls } = configProvider; - const { getPrefixCls } = configProvider; - const prefixCls = getPrefixCls('space', customizePrefixCls); - const items = filterEmpty(slots.default?.()); - const len = items.length; + return () => { + const prefixCls = getPrefixCls('space', customizePrefixCls); + const items = filterEmpty(slots.default?.()); + const len = items.length; - if (len === 0) { - return null; - } + if (len === 0) { + return null; + } - const mergedAlign = align === undefined && direction === 'horizontal' ? 'center' : align; + const mergedAlign = align === undefined && direction === 'horizontal' ? 'center' : align; - const someSpaceClass = { - [prefixCls]: true, - [`${prefixCls}-${direction}`]: true, - [`${prefixCls}-align-${mergedAlign}`]: mergedAlign, - }; + const someSpaceClass = { + [prefixCls]: true, + [`${prefixCls}-${direction}`]: true, + [`${prefixCls}-align-${mergedAlign}`]: mergedAlign, + }; - const itemClassName = `${prefixCls}-item`; - const marginDirection = 'marginRight'; // directionConfig === 'rtl' ? 'marginLeft' : 'marginRight'; + const itemClassName = `${prefixCls}-item`; + const marginDirection = 'marginRight'; // directionConfig === 'rtl' ? 'marginLeft' : 'marginRight'; - return ( -
- {items.map((child, i) => ( -
- {child} + return ( +
+ {items.map((child, i) => ( +
+ {child} +
+ ))}
- ))} -
- ); -}; - -Space.displayName = 'ASpace'; + ); + }; + }, +}); /* istanbul ignore next */ Space.install = function(app: App) { - app.component(Space.displayName, Space); + app.component(Space.name, Space); return app; }; diff --git a/components/space/style/index.js b/components/space/style/index.ts similarity index 100% rename from components/space/style/index.js rename to components/space/style/index.ts