SvgIcon.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* eslint-disable no-underscore-dangle */
  2. import * as React from 'react';
  3. import Svg, { Path, Defs, Stop, LinearGradient } from 'react-native-svg';
  4. import { View } from 'react-native';
  5. import { withTheme, Badge } from 'react-native-paper';
  6. import svgMap from '../Utils/SvgUtilsNew';
  7. function Icon(props) {
  8. const { name, type, theme, fillAll, Flip, style, badge } = props;
  9. let { color, width, height } = props;
  10. const { colors } = theme;
  11. if (type) {
  12. color = colors[type];
  13. }
  14. if (!width) {
  15. width = 32;
  16. }
  17. if (!height) {
  18. height = 32;
  19. }
  20. const svgInfo = svgMap.get(name);
  21. const viewBox = svgInfo
  22. ? svgInfo.viewBox || '0 0 1024 1024'
  23. : '0 0 1024 1024';
  24. const pathList = svgInfo
  25. ? svgInfo.pathList || [
  26. {
  27. ...svgInfo,
  28. },
  29. ]
  30. : [];
  31. let transform = '';
  32. if (Flip) {
  33. transform = `rotate(180 ${(svgInfo.defaultWidth || 1024) / 2} ${
  34. (svgInfo.defaultWidth || 1024) / 2
  35. })`;
  36. }
  37. const pathComList = () => {
  38. return pathList.map((item, index) => {
  39. const pathProps = { ...item };
  40. if ((fillAll || item.changeFill) && color) {
  41. pathProps.fill = color;
  42. }
  43. if (item.strokeWidth && !item.changeFill && color) {
  44. pathProps.stroke = color;
  45. }
  46. delete pathProps.viewBox;
  47. delete pathProps.defaultWidth;
  48. delete pathProps.changeFill;
  49. return <Path {...pathProps} key={index} transform={transform} />;
  50. });
  51. };
  52. return (
  53. <View>
  54. <Svg width={width} height={height} viewBox={viewBox} style={style}>
  55. <Defs>
  56. <LinearGradient id="orange_red" x1="0%" y1="0%" x2="0%" y2="90%">
  57. <Stop offset="0%" style={{ stopColor: '#000', stopOpacity: 1 }} />
  58. <Stop
  59. offset="50%"
  60. style={{ stopColor: 'rgb(255, 216, 0)', stopOpacity: 1 }}
  61. />
  62. </LinearGradient>
  63. </Defs>
  64. {pathComList()}
  65. </Svg>
  66. {badge > 0 && (
  67. <Badge size={15} style={{ position: 'absolute', right: 0, top: -5 }}>
  68. {badge}
  69. </Badge>
  70. )}
  71. </View>
  72. );
  73. }
  74. export default withTheme(Icon);