2019-01-12 11:33:27 +08:00
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
import * as allIcons from '@ant-design/icons/lib/dist';
|
|
|
|
|
import VueIcon from '@ant-design/icons-vue';
|
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
|
import createFromIconfontCN from './IconFont';
|
2018-11-17 15:57:38 +08:00
|
|
|
|
import {
|
2019-01-12 11:33:27 +08:00
|
|
|
|
svgBaseProps,
|
|
|
|
|
withThemeSuffix,
|
|
|
|
|
removeTypeTheme,
|
|
|
|
|
getThemeFromTypeName,
|
2018-11-17 15:57:38 +08:00
|
|
|
|
alias,
|
2019-01-12 11:33:27 +08:00
|
|
|
|
} from './utils';
|
|
|
|
|
import warning from '../_util/warning';
|
2019-04-20 16:03:57 +08:00
|
|
|
|
import LocaleReceiver from '../locale-provider/LocaleReceiver';
|
2019-01-12 11:33:27 +08:00
|
|
|
|
import { getTwoToneColor, setTwoToneColor } from './twoTonePrimaryColor';
|
2020-03-26 17:25:10 +08:00
|
|
|
|
import { filterEmpty, getListeners } from '../_util/props-util';
|
2019-08-28 10:50:19 +08:00
|
|
|
|
import Base from '../base';
|
2018-11-17 15:57:38 +08:00
|
|
|
|
|
|
|
|
|
// Initial setting
|
2019-01-12 11:33:27 +08:00
|
|
|
|
VueIcon.add(...Object.keys(allIcons).map(key => allIcons[key]));
|
|
|
|
|
setTwoToneColor('#1890ff');
|
|
|
|
|
const defaultTheme = 'outlined';
|
|
|
|
|
let dangerousTheme;
|
2018-11-17 15:57:38 +08:00
|
|
|
|
|
2019-04-20 16:03:57 +08:00
|
|
|
|
function renderIcon(h, locale, context) {
|
2020-03-26 17:25:10 +08:00
|
|
|
|
const { $props: props, $slots } = context;
|
|
|
|
|
const listeners = getListeners(context);
|
2019-04-20 16:03:57 +08:00
|
|
|
|
const {
|
|
|
|
|
// affect inner <svg>...</svg>
|
|
|
|
|
type,
|
|
|
|
|
component: Component,
|
|
|
|
|
viewBox,
|
|
|
|
|
spin,
|
|
|
|
|
// other
|
|
|
|
|
theme, // default to outlined
|
|
|
|
|
twoToneColor,
|
|
|
|
|
rotate,
|
|
|
|
|
tabIndex,
|
|
|
|
|
} = props;
|
2020-03-26 17:25:10 +08:00
|
|
|
|
let children = filterEmpty($slots.default);
|
2019-04-20 16:03:57 +08:00
|
|
|
|
children = children.length === 0 ? undefined : children;
|
|
|
|
|
warning(
|
|
|
|
|
Boolean(type || Component || children),
|
2020-03-07 19:45:13 +08:00
|
|
|
|
'Icon',
|
2019-04-20 16:03:57 +08:00
|
|
|
|
'Icon should have `type` prop or `component` prop or `children`.',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const classString = classNames({
|
|
|
|
|
[`anticon`]: true,
|
|
|
|
|
[`anticon-${type}`]: !!type,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const svgClassString = classNames({
|
|
|
|
|
[`anticon-spin`]: !!spin || type === 'loading',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const svgStyle = rotate
|
2019-05-28 11:37:38 +08:00
|
|
|
|
? {
|
|
|
|
|
msTransform: `rotate(${rotate}deg)`,
|
|
|
|
|
transform: `rotate(${rotate}deg)`,
|
|
|
|
|
}
|
|
|
|
|
: undefined;
|
2019-04-20 16:03:57 +08:00
|
|
|
|
|
2020-03-07 19:45:13 +08:00
|
|
|
|
const innerSvgProps = {
|
|
|
|
|
attrs: {
|
|
|
|
|
...svgBaseProps,
|
|
|
|
|
viewBox,
|
|
|
|
|
},
|
|
|
|
|
class: svgClassString,
|
|
|
|
|
style: svgStyle,
|
|
|
|
|
};
|
|
|
|
|
if (!viewBox) {
|
|
|
|
|
delete innerSvgProps.attrs.viewBox;
|
|
|
|
|
}
|
2019-04-20 16:03:57 +08:00
|
|
|
|
|
2020-03-07 19:45:13 +08:00
|
|
|
|
const renderInnerNode = () => {
|
|
|
|
|
// component > children > type
|
|
|
|
|
if (Component) {
|
|
|
|
|
return <Component {...innerSvgProps}>{children}</Component>;
|
|
|
|
|
}
|
|
|
|
|
if (children) {
|
|
|
|
|
warning(
|
|
|
|
|
Boolean(viewBox) || (children.length === 1 && children[0].tag === 'use'),
|
|
|
|
|
'Icon',
|
|
|
|
|
'Make sure that you provide correct `viewBox`' +
|
|
|
|
|
' prop (default `0 0 1024 1024`) to the icon.',
|
|
|
|
|
);
|
|
|
|
|
const innerSvgProps = {
|
|
|
|
|
attrs: {
|
|
|
|
|
...svgBaseProps,
|
|
|
|
|
},
|
|
|
|
|
class: svgClassString,
|
|
|
|
|
style: svgStyle,
|
|
|
|
|
};
|
|
|
|
|
return (
|
|
|
|
|
<svg {...innerSvgProps} viewBox={viewBox}>
|
|
|
|
|
{children}
|
|
|
|
|
</svg>
|
|
|
|
|
);
|
2019-04-20 16:03:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 19:45:13 +08:00
|
|
|
|
if (typeof type === 'string') {
|
|
|
|
|
let computedType = type;
|
|
|
|
|
if (theme) {
|
|
|
|
|
const themeInName = getThemeFromTypeName(type);
|
|
|
|
|
warning(
|
|
|
|
|
!themeInName || theme === themeInName,
|
|
|
|
|
'Icon',
|
|
|
|
|
`The icon name '${type}' already specify a theme '${themeInName}',` +
|
|
|
|
|
` the 'theme' prop '${theme}' will be ignored.`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
computedType = withThemeSuffix(
|
|
|
|
|
removeTypeTheme(alias(computedType)),
|
|
|
|
|
dangerousTheme || theme || defaultTheme,
|
|
|
|
|
);
|
2019-04-20 16:03:57 +08:00
|
|
|
|
|
2020-03-07 19:45:13 +08:00
|
|
|
|
return (
|
|
|
|
|
<VueIcon
|
|
|
|
|
focusable="false"
|
|
|
|
|
class={svgClassString}
|
|
|
|
|
type={computedType}
|
|
|
|
|
primaryColor={twoToneColor}
|
|
|
|
|
style={svgStyle}
|
|
|
|
|
/>
|
2019-04-20 16:03:57 +08:00
|
|
|
|
);
|
|
|
|
|
}
|
2020-03-07 19:45:13 +08:00
|
|
|
|
};
|
2019-04-20 16:03:57 +08:00
|
|
|
|
let iconTabIndex = tabIndex;
|
2019-05-28 11:37:38 +08:00
|
|
|
|
if (iconTabIndex === undefined && 'click' in listeners) {
|
2019-04-20 16:03:57 +08:00
|
|
|
|
iconTabIndex = -1;
|
|
|
|
|
}
|
|
|
|
|
// functional component not support nativeOn,https://github.com/vuejs/vue/issues/7526
|
|
|
|
|
const iProps = {
|
|
|
|
|
attrs: {
|
|
|
|
|
'aria-label': type && `${locale.icon}: ${type}`,
|
|
|
|
|
tabIndex: iconTabIndex,
|
|
|
|
|
},
|
2020-03-26 17:25:10 +08:00
|
|
|
|
on: listeners,
|
2019-04-20 16:03:57 +08:00
|
|
|
|
class: classString,
|
|
|
|
|
staticClass: '',
|
|
|
|
|
};
|
2020-03-07 19:45:13 +08:00
|
|
|
|
return <i {...iProps}>{renderInnerNode()}</i>;
|
2019-04-20 16:03:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-17 15:57:38 +08:00
|
|
|
|
const Icon = {
|
|
|
|
|
name: 'AIcon',
|
2018-11-27 18:25:38 +08:00
|
|
|
|
props: {
|
2019-04-20 16:03:57 +08:00
|
|
|
|
tabIndex: PropTypes.number,
|
2018-11-27 18:25:38 +08:00
|
|
|
|
type: PropTypes.string,
|
|
|
|
|
component: PropTypes.any,
|
|
|
|
|
viewBox: PropTypes.any,
|
|
|
|
|
spin: PropTypes.bool.def(false),
|
2019-04-20 16:03:57 +08:00
|
|
|
|
rotate: PropTypes.number,
|
2018-11-27 18:25:38 +08:00
|
|
|
|
theme: PropTypes.oneOf(['filled', 'outlined', 'twoTone']),
|
|
|
|
|
twoToneColor: PropTypes.string,
|
2019-04-20 16:03:57 +08:00
|
|
|
|
role: PropTypes.string,
|
2018-11-27 18:25:38 +08:00
|
|
|
|
},
|
2020-03-26 17:25:10 +08:00
|
|
|
|
render(h) {
|
2019-04-20 16:03:57 +08:00
|
|
|
|
return (
|
|
|
|
|
<LocaleReceiver
|
|
|
|
|
componentName="Icon"
|
2020-03-26 17:25:10 +08:00
|
|
|
|
scopedSlots={{ default: locale => renderIcon(h, locale, this) }}
|
2019-04-20 16:03:57 +08:00
|
|
|
|
/>
|
2019-01-12 11:33:27 +08:00
|
|
|
|
);
|
2018-11-17 15:57:38 +08:00
|
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
|
};
|
2018-11-17 15:57:38 +08:00
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
|
Icon.createFromIconfontCN = createFromIconfontCN;
|
|
|
|
|
Icon.getTwoToneColor = getTwoToneColor;
|
|
|
|
|
Icon.setTwoToneColor = setTwoToneColor;
|
2017-10-26 15:18:08 +08:00
|
|
|
|
|
2018-09-19 13:21:57 +08:00
|
|
|
|
/* istanbul ignore next */
|
2019-01-12 11:33:27 +08:00
|
|
|
|
Icon.install = function(Vue) {
|
2019-08-28 10:50:19 +08:00
|
|
|
|
Vue.use(Base);
|
2019-01-12 11:33:27 +08:00
|
|
|
|
Vue.component(Icon.name, Icon);
|
|
|
|
|
};
|
2018-09-19 13:21:57 +08:00
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
|
export default Icon;
|