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

181 lines
4.5 KiB
JavaScript
Raw Normal View History

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';
import { filterEmpty, getClass } from '../_util/props-util';
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) {
const { props, slots, listeners, data } = context;
const {
// affect inner <svg>...</svg>
type,
component: Component,
viewBox,
spin,
// other
theme, // default to outlined
twoToneColor,
rotate,
tabIndex,
} = props;
const slotsMap = slots();
let children = filterEmpty(slotsMap.default);
children = children.length === 0 ? undefined : children;
warning(
Boolean(type || Component || children),
'Icon should have `type` prop or `component` prop or `children`.',
);
const classString = classNames({
...getClass(context),
[`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
let innerNode;
// component > children > type
if (Component) {
const innerSvgProps = {
attrs: {
...svgBaseProps,
viewBox,
},
class: svgClassString,
2019-09-10 21:54:22 +08:00
style: svgStyle,
2019-04-20 16:03:57 +08:00
};
if (!viewBox) {
delete innerSvgProps.attrs.viewBox;
}
innerNode = <Component {...innerSvgProps}>{children}</Component>;
}
if (children) {
warning(
Boolean(viewBox) || (children.length === 1 && children[0].tag === 'use'),
'Make sure that you provide correct `viewBox`' +
' prop (default `0 0 1024 1024`) to the icon.',
);
const innerSvgProps = {
attrs: {
...svgBaseProps,
},
class: svgClassString,
2019-09-10 21:54:22 +08:00
style: svgStyle,
2019-04-20 16:03:57 +08:00
};
innerNode = (
<svg {...innerSvgProps} viewBox={viewBox}>
{children}
</svg>
);
}
if (typeof type === 'string') {
let computedType = type;
if (theme) {
const themeInName = getThemeFromTypeName(type);
warning(
!themeInName || theme === themeInName,
`The icon name '${type}' already specify a theme '${themeInName}',` +
` the 'theme' prop '${theme}' will be ignored.`,
);
}
computedType = withThemeSuffix(
removeTypeTheme(alias(computedType)),
dangerousTheme || theme || defaultTheme,
);
innerNode = (
<VueIcon
2019-10-30 16:42:01 +08:00
focusable="false"
2019-04-20 16:03:57 +08:00
class={svgClassString}
type={computedType}
primaryColor={twoToneColor}
style={svgStyle}
/>
);
}
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;
}
const { attrs, ...restDataProps } = data;
// functional component not support nativeOnhttps://github.com/vuejs/vue/issues/7526
const iProps = {
...restDataProps,
attrs: {
...attrs,
'aria-label': type && `${locale.icon}: ${type}`,
tabIndex: iconTabIndex,
},
on: { ...listeners, ...data.nativeOn },
class: classString,
staticClass: '',
};
return <i {...iProps}>{innerNode}</i>;
}
2018-11-17 15:57:38 +08:00
const Icon = {
functional: true,
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
},
2019-01-12 11:33:27 +08:00
render(h, context) {
2019-04-20 16:03:57 +08:00
return (
<LocaleReceiver
componentName="Icon"
2019-05-28 11:37:38 +08:00
scopedSlots={{ default: locale => renderIcon(h, locale, context) }}
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
/* istanbul ignore next */
2019-01-12 11:33:27 +08:00
Icon.install = function(Vue) {
Vue.use(Base);
2019-01-12 11:33:27 +08:00
Vue.component(Icon.name, Icon);
};
2019-01-12 11:33:27 +08:00
export default Icon;