ant-design-vue/components/icon/index.js

139 lines
3.7 KiB
JavaScript
Raw Normal View History

2019-01-12 03:33:27 +00: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 07:57:38 +00:00
import {
2019-01-12 03:33:27 +00:00
svgBaseProps,
withThemeSuffix,
removeTypeTheme,
getThemeFromTypeName,
2018-11-17 07:57:38 +00:00
alias,
2019-01-12 03:33:27 +00:00
} from './utils';
import warning from '../_util/warning';
import { getTwoToneColor, setTwoToneColor } from './twoTonePrimaryColor';
import { filterEmpty, getClass } from '../_util/props-util';
2018-11-17 07:57:38 +00:00
// Initial setting
2019-01-12 03:33:27 +00:00
VueIcon.add(...Object.keys(allIcons).map(key => allIcons[key]));
setTwoToneColor('#1890ff');
const defaultTheme = 'outlined';
let dangerousTheme;
2018-11-17 07:57:38 +00:00
const Icon = {
functional: true,
name: 'AIcon',
2018-11-27 10:25:38 +00:00
props: {
type: PropTypes.string,
component: PropTypes.any,
viewBox: PropTypes.any,
spin: PropTypes.bool.def(false),
theme: PropTypes.oneOf(['filled', 'outlined', 'twoTone']),
twoToneColor: PropTypes.string,
},
2019-01-12 03:33:27 +00:00
render(h, context) {
const { props, slots, listeners, data } = context;
2018-11-17 07:57:38 +00:00
const {
// affect inner <svg>...</svg>
type,
component: Component,
viewBox,
spin,
// other
theme, // default to outlined
twoToneColor,
2019-01-12 03:33:27 +00:00
} = props;
const slotsMap = slots();
let children = filterEmpty(slotsMap.default);
children = children.length === 0 ? undefined : children;
2018-11-17 07:57:38 +00:00
warning(
Boolean(type || Component || children),
'Icon should have `type` prop or `component` prop or `children`.',
2019-01-12 03:33:27 +00:00
);
2018-11-17 07:57:38 +00:00
const classString = classNames({
2018-11-29 14:20:17 +00:00
...getClass(context),
2018-11-17 07:57:38 +00:00
[`anticon`]: true,
[`anticon-${type}`]: !!type,
2019-01-12 03:33:27 +00:00
});
2018-11-17 07:57:38 +00:00
const svgClassString = classNames({
[`anticon-spin`]: !!spin || type === 'loading',
2019-01-12 03:33:27 +00:00
});
2018-11-17 07:57:38 +00:00
2019-01-12 03:33:27 +00:00
let innerNode;
2018-11-17 07:57:38 +00:00
// component > children > type
if (Component) {
const innerSvgProps = {
attrs: {
...svgBaseProps,
viewBox,
},
class: svgClassString,
2019-01-12 03:33:27 +00:00
};
2018-11-17 07:57:38 +00:00
if (!viewBox) {
2019-01-12 03:33:27 +00:00
delete innerSvgProps.attrs.viewBox;
2018-11-17 07:57:38 +00:00
}
2019-01-12 03:33:27 +00:00
innerNode = <Component {...innerSvgProps}>{children}</Component>;
2018-11-17 07:57:38 +00:00
}
if (children) {
warning(
2019-01-12 03:33:27 +00:00
Boolean(viewBox) || (children.length === 1 && children[0].tag === 'use'),
2018-11-17 07:57:38 +00:00
'Make sure that you provide correct `viewBox`' +
' prop (default `0 0 1024 1024`) to the icon.',
2019-01-12 03:33:27 +00:00
);
2018-11-17 07:57:38 +00:00
const innerSvgProps = {
attrs: {
...svgBaseProps,
},
class: svgClassString,
2019-01-12 03:33:27 +00:00
};
2018-11-17 07:57:38 +00:00
innerNode = (
<svg {...innerSvgProps} viewBox={viewBox}>
{children}
</svg>
2019-01-12 03:33:27 +00:00
);
2018-11-17 07:57:38 +00:00
}
if (typeof type === 'string') {
2019-01-12 03:33:27 +00:00
let computedType = type;
2018-11-17 07:57:38 +00:00
if (theme) {
2019-01-12 03:33:27 +00:00
const themeInName = getThemeFromTypeName(type);
warning(
!themeInName || theme === themeInName,
2018-11-17 07:57:38 +00:00
`The icon name '${type}' already specify a theme '${themeInName}',` +
2019-01-12 03:33:27 +00:00
` the 'theme' prop '${theme}' will be ignored.`,
);
2018-11-17 07:57:38 +00:00
}
computedType = withThemeSuffix(
2019-01-03 12:51:56 +00:00
removeTypeTheme(alias(computedType)),
2018-11-17 07:57:38 +00:00
dangerousTheme || theme || defaultTheme,
2019-01-12 03:33:27 +00:00
);
2018-11-17 07:57:38 +00:00
innerNode = (
2019-01-12 03:33:27 +00:00
<VueIcon class={svgClassString} type={computedType} primaryColor={twoToneColor} />
);
2018-11-17 07:57:38 +00:00
}
2018-12-19 12:48:42 +00:00
// functional component not support nativeOnhttps://github.com/vuejs/vue/issues/7526
2018-12-16 06:22:18 +00:00
const iProps = {
2019-01-12 03:33:27 +00:00
...data,
on: { ...listeners, ...data.nativeOn },
class: classString,
staticClass: '',
};
return <i {...iProps}>{innerNode}</i>;
2018-11-17 07:57:38 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-11-17 07:57:38 +00:00
2019-01-12 03:33:27 +00:00
Icon.createFromIconfontCN = createFromIconfontCN;
Icon.getTwoToneColor = getTwoToneColor;
Icon.setTwoToneColor = setTwoToneColor;
2017-10-26 07:18:08 +00:00
/* istanbul ignore next */
2019-01-12 03:33:27 +00:00
Icon.install = function(Vue) {
Vue.component(Icon.name, Icon);
};
2019-01-12 03:33:27 +00:00
export default Icon;