|
|
|
/* eslint-disable react/button-has-type */
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import omit from 'rc-util/lib/omit';
|
|
|
|
import * as React from 'react';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
|
|
|
import DisabledContext from '../config-provider/DisabledContext';
|
|
|
|
import type { SizeType } from '../config-provider/SizeContext';
|
|
|
|
import SizeContext from '../config-provider/SizeContext';
|
|
|
|
import { useCompactItemContext } from '../space/Compact';
|
|
|
|
import { cloneElement, isFragment } from '../_util/reactNode';
|
|
|
|
import warning from '../_util/warning';
|
|
|
|
import Wave from '../_util/wave';
|
|
|
|
import Group, { GroupSizeContext } from './button-group';
|
|
|
|
import LoadingIcon from './LoadingIcon';
|
|
|
|
|
|
|
|
// CSSINJS
|
|
|
|
import useStyle from './style';
|
|
|
|
|
|
|
|
const rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/;
|
|
|
|
const isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar);
|
|
|
|
function isString(str: any) {
|
|
|
|
return typeof str === 'string';
|
|
|
|
}
|
|
|
|
|
|
|
|
function isUnBorderedButtonType(type: ButtonType | undefined) {
|
|
|
|
return type === 'text' || type === 'link';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert one space between two chinese characters automatically.
|
|
|
|
function insertSpace(child: React.ReactElement | string | number, needInserted: boolean) {
|
|
|
|
// Check the child if is undefined or null.
|
|
|
|
if (child === null || child === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const SPACE = needInserted ? ' ' : '';
|
|
|
|
// strictNullChecks oops.
|
|
|
|
if (
|
|
|
|
typeof child !== 'string' &&
|
|
|
|
typeof child !== 'number' &&
|
|
|
|
isString(child.type) &&
|
|
|
|
isTwoCNChar(child.props.children)
|
|
|
|
) {
|
|
|
|
return cloneElement(child, {
|
|
|
|
children: child.props.children.split('').join(SPACE),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (typeof child === 'string') {
|
|
|
|
return isTwoCNChar(child) ? <span>{child.split('').join(SPACE)}</span> : <span>{child}</span>;
|
|
|
|
}
|
|
|
|
if (isFragment(child)) {
|
|
|
|
return <span>{child}</span>;
|
|
|
|
}
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
function spaceChildren(children: React.ReactNode, needInserted: boolean) {
|
|
|
|
let isPrevChildPure: boolean = false;
|
|
|
|
const childList: React.ReactNode[] = [];
|
|
|
|
React.Children.forEach(children, (child) => {
|
|
|
|
const type = typeof child;
|
|
|
|
const isCurrentChildPure = type === 'string' || type === 'number';
|
|
|
|
if (isPrevChildPure && isCurrentChildPure) {
|
|
|
|
const lastIndex = childList.length - 1;
|
|
|
|
const lastChild = childList[lastIndex];
|
|
|
|
childList[lastIndex] = `${lastChild}${child}`;
|
|
|
|
} else {
|
|
|
|
childList.push(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
isPrevChildPure = isCurrentChildPure;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Pass to React.Children.map to auto fill key
|
|
|
|
return React.Children.map(childList, (child) =>
|
|
|
|
insertSpace(child as React.ReactElement | string | number, needInserted),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ButtonTypes = ['default', 'primary', 'ghost', 'dashed', 'link', 'text'] as const;
|
|
|
|
export type ButtonType = typeof ButtonTypes[number];
|
|
|
|
|
|
|
|
const ButtonShapes = ['default', 'circle', 'round'] as const;
|
|
|
|
export type ButtonShape = typeof ButtonShapes[number];
|
|
|
|
|
|
|
|
const ButtonHTMLTypes = ['submit', 'button', 'reset'] as const;
|
|
|
|
export type ButtonHTMLType = typeof ButtonHTMLTypes[number];
|
|
|
|
|
|
|
|
export type LegacyButtonType = ButtonType | 'danger';
|
|
|
|
export function convertLegacyProps(type?: LegacyButtonType): ButtonProps {
|
|
|
|
if (type === 'danger') {
|
|
|
|
return { danger: true }; |