Icon 图标

在 React 项目中使用 Icon 需要引入 @fortawesome/fontawesome-free,然后在代码中 import '@fortawesome/fontawesome-free/css/all.css',还有相关的 webpack 配置,具体请参考 amis-react-starter 里的配置

基本使用

  1. {
  2. "type": "page",
  3. "body": {
  4. "type": "icon",
  5. "icon": "cloud"
  6. }
  7. }

颜色及大小调整

icon 基于字体实现,所以可以通过文字颜色大小来控制它。

  1. {
  2. "type": "page",
  3. "body": {
  4. "type": "icon",
  5. "icon": "cloud",
  6. "className": "text-info text-xl"
  7. }
  8. }

使用图标链接

icon 还可以使用 URL 地址,可以从 iconfont 等网站下载图表的 svg 文件上传到服务器,然后使用对应的地址,比如

  1. {
  2. "type": "page",
  3. "body": {
  4. "type": "icon",
  5. "icon": "https://suda.cdn.bcebos.com/images%2F2021-01%2Fdiamond.svg"
  6. }
  7. }

使用 v5/v6 版本的 fontawesome

icon默认支持fontawesome v4,如果想要支持 v5 以及 v6 版本的 fontawesome 请设置vendor为空字符串。

fontawesome v5 版本

v5 用 far/fas 等表示前缀,可参考官网示例

  1. {
  2. "type":"page",
  3. "body":[
  4. {
  5. "type":"icon",
  6. "icon":"far fa-address-book",
  7. "vendor":""
  8. },
  9. {
  10. "type":"icon",
  11. "icon":"fas fa-address-book",
  12. "vendor":""
  13. },
  14. {
  15. "type":"icon",
  16. "icon":"far fa-address-book",
  17. "vendor":"",
  18. "className": "text-info text-xl"
  19. },
  20. {
  21. "type":"icon",
  22. "icon":"fas fa-address-book",
  23. "vendor":"",
  24. "className": "text-info text-xl"
  25. },
  26. {
  27. type: "divider",
  28. },
  29. {
  30. "type":"icon",
  31. "icon":"far fa-bell",
  32. "vendor":""
  33. },
  34. {
  35. "type":"icon",
  36. "icon":"fas fa-bell",
  37. "vendor":""
  38. },
  39. {
  40. "type":"icon",
  41. "icon":"far fa-bell",
  42. "vendor":"",
  43. "className": "text-info text-xl"
  44. },
  45. {
  46. "type":"icon",
  47. "icon":"fas fa-bell",
  48. "vendor":"",
  49. "className": "text-info text-xl"
  50. },
  51. {
  52. type: "divider",
  53. },
  54. {
  55. "type":"icon",
  56. "icon":"far fa-plus",
  57. "vendor":""
  58. },
  59. {
  60. "type":"icon",
  61. "icon":"fas fa-plus",
  62. "vendor":""
  63. },
  64. {
  65. "type":"icon",
  66. "icon":"far fa-plus",
  67. "vendor":"",
  68. "className": "text-info text-xl"
  69. },
  70. {
  71. "type":"icon",
  72. "icon":"fas fa-plus",
  73. "vendor":"",
  74. "className": "text-info text-xl"
  75. },
  76. {
  77. type: "divider",
  78. },
  79. {
  80. "type":"icon",
  81. "icon":"far fa-question-circle",
  82. "vendor":""
  83. },
  84. {
  85. "type":"icon",
  86. "icon":"fas fa-question-circle",
  87. "vendor":""
  88. },
  89. {
  90. "type":"icon",
  91. "icon":"far fa-question-circle",
  92. "vendor":"",
  93. "className": "text-info text-xl"
  94. },
  95. {
  96. "type":"icon",
  97. "icon":"fas fa-question-circle",
  98. "vendor":"",
  99. "className": "text-info text-xl"
  100. }
  101. ]
  102. }

fontawesome v6 版本

v6 用 fa-regular / fa-solid 等表示前缀,可参考官网示例

  1. {
  2. "type":"page",
  3. "body":[
  4. {
  5. "type":"icon",
  6. "icon":"fa-regular fa-address-book",
  7. "vendor":""
  8. },
  9. {
  10. "type":"icon",
  11. "icon":"fa-solid fa-address-book",
  12. "vendor":""
  13. },
  14. {
  15. "type":"icon",
  16. "icon":"fa-regular fa-address-book",
  17. "vendor":"",
  18. "className": "text-info text-xl"
  19. },
  20. {
  21. "type":"icon",
  22. "icon":"fa-solid fa-address-book",
  23. "vendor":"",
  24. "className": "text-info text-xl"
  25. }
  26. ]
  27. }

属性表

属性名 类型 默认值 说明
type string icon 指定组件类型
className string 外层 CSS 类名
icon string icon 名称,支持 fontawesome v4 或 通过 registerIcon 注册的 icon、或使用 url
vendor string icon 类型,默认为fa, 表示 fontawesome v4。也支持 iconfont, 如果是 fontawesome v5 以上版本或者其他框架可以设置为空字符串