From e860759f16b5f67dbd4e5b4871b77f4375ac96cf Mon Sep 17 00:00:00 2001 From: neverland Date: Mon, 18 Feb 2019 20:36:12 +0800 Subject: [PATCH] [improvement] Switch: tsx (#2784) --- packages/switch/{index.js => index.tsx} | 21 +++++++++++++++++---- packages/switch/shared.ts | 17 ++++++++++++++--- 2 files changed, 31 insertions(+), 7 deletions(-) rename packages/switch/{index.js => index.tsx} (65%) diff --git a/packages/switch/index.js b/packages/switch/index.tsx similarity index 65% rename from packages/switch/index.js rename to packages/switch/index.tsx index f594d288d..1354a8307 100644 --- a/packages/switch/index.js +++ b/packages/switch/index.tsx @@ -1,11 +1,24 @@ import { use } from '../utils'; import Loading from '../loading'; -import { switchProps } from './shared'; +import { switchProps, SharedSwitchProps } from './shared'; import { emit, inherit } from '../utils/functional'; +// Types +import { CreateElement, RenderContext } from 'vue/types'; +import { DefaultSlots } from '../utils/use/sfc'; + +export type SwitchEvents = { + onChange?(checked: boolean): void; +}; + const [sfc, bem] = use('switch'); -function Switch(h, props, slots, ctx) { +function Switch( + h: CreateElement, + props: SharedSwitchProps, + slots: DefaultSlots, + ctx: RenderContext +) { const { value, loading, disabled, activeValue, inactiveValue } = props; const style = { @@ -13,7 +26,7 @@ function Switch(h, props, slots, ctx) { backgroundColor: value ? props.activeColor : props.inactiveColor }; - const onClick = event => { + const onClick = () => { if (!disabled && !loading) { const newValue = value === activeValue ? inactiveValue : activeValue; emit(ctx, 'input', newValue); @@ -40,4 +53,4 @@ function Switch(h, props, slots, ctx) { Switch.props = switchProps; -export default sfc(Switch); +export default sfc(Switch); diff --git a/packages/switch/shared.ts b/packages/switch/shared.ts index c5443f849..f9ad0aae3 100644 --- a/packages/switch/shared.ts +++ b/packages/switch/shared.ts @@ -2,18 +2,29 @@ * Common Switch Props */ +export type SharedSwitchProps = { + size?: string; + value?: any; + loading?: boolean; + disabled?: boolean; + activeValue?: any; + inactiveValue?: any; + activeColor?: string; + inactiveColor?: string; +}; + export const switchProps = { - value: null, + value: null as any, loading: Boolean, disabled: Boolean, activeColor: String, inactiveColor: String, activeValue: { - type: null, + type: null as any, default: true }, inactiveValue: { - type: null, + type: null as any, default: false }, size: {