From ae631c7190c3e94cda77802e12d79538489382c1 Mon Sep 17 00:00:00 2001 From: bang Date: Wed, 26 Apr 2017 23:30:51 +0700 Subject: [PATCH] refactor(button): enable noImplicitAny #5627 --- components/button/button-group.tsx | 14 +++++++---- components/button/button.tsx | 37 +++++++++++++++++++----------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/components/button/button-group.tsx b/components/button/button-group.tsx index ac93afe947..b26824a9bf 100644 --- a/components/button/button-group.tsx +++ b/components/button/button-group.tsx @@ -15,10 +15,16 @@ export default function ButtonGroup(props: ButtonGroupProps) { // large => lg // small => sm - const sizeCls = ({ - large: 'lg', - small: 'sm', - })[size] || ''; + let sizeCls = ''; + switch (size) { + case 'large': + sizeCls = 'lg'; + break; + case 'small': + sizeCls = 'sm'; + default: + break; + } const classes = classNames(prefixCls, { [`${prefixCls}-${sizeCls}`]: sizeCls, diff --git a/components/button/button.tsx b/components/button/button.tsx index 473393dc5d..12e72c2268 100644 --- a/components/button/button.tsx +++ b/components/button/button.tsx @@ -6,21 +6,23 @@ import omit from 'omit.js'; const rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/; const isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar); -function isString(str) { +function isString(str: any) { return typeof str === 'string'; } // Insert one space between two chinese characters automatically. -function insertSpace(child) { +function insertSpace(child: React.ReactChild) { // Check the child if is undefined or null. if (child == null) { return; } - if (isString(child.type) && isTwoCNChar(child.props.children)) { + // strictNullChecks oops. + if (typeof child !== 'string' && typeof child !== 'number' && + isString(child.type) && isTwoCNChar(child.props.children)) { return React.cloneElement(child, {}, - child.props.children.split('').join(' ')); + child.props.children.split('').join(' ')); } - if (isString(child)) { + if (typeof child === 'string') { if (isTwoCNChar(child)) { child = child.split('').join(' '); } @@ -74,14 +76,14 @@ export default class Button extends React.Component { timeout: number; delayTimeout: number; - constructor(props) { + constructor(props: ButtonProps) { super(props); this.state = { loading: props.loading, }; } - componentWillReceiveProps(nextProps) { + componentWillReceiveProps(nextProps: ButtonProps) { const currentLoading = this.props.loading; const loading = nextProps.loading; @@ -89,7 +91,7 @@ export default class Button extends React.Component { clearTimeout(this.delayTimeout); } - if (loading && loading.delay) { + if (typeof loading !== 'boolean' && loading && loading.delay) { this.delayTimeout = setTimeout(() => this.setState({ loading }), loading.delay); } else { this.setState({ loading }); @@ -105,7 +107,7 @@ export default class Button extends React.Component { } } - handleClick = (e) => { + handleClick = (e: React.MouseEvent) => { // Add click effect this.setState({ clicked: true }); clearTimeout(this.timeout); @@ -118,7 +120,7 @@ export default class Button extends React.Component { } // Handle auto focus when click button in Chrome - handleMouseUp = (e) => { + handleMouseUp = (e: React.MouseEvent) => { if (this.props.onMouseUp) { this.props.onMouseUp(e); } @@ -130,12 +132,19 @@ export default class Button extends React.Component { } = this.props; const { loading, clicked } = this.state; + // large => lg // small => sm - const sizeCls = ({ - large: 'lg', - small: 'sm', - })[size] || ''; + let sizeCls = ''; + switch (size) { + case 'large': + sizeCls = 'lg'; + break; + case 'small': + sizeCls = 'sm'; + default: + break; + } const classes = classNames(prefixCls, { [`${prefixCls}-${type}`]: type,