我想做一个icon选择器组件,能否把 packages/amis-ui/src/components/icons.tsx 中的 iconFactory的key导出?
概述
想要拿到通过 registerIcon 注册的所有 icon 的名字,因为涉及到基础库的修改,不清楚amis内部的编码规范,不敢随便提PR。
动机
我想做一个可视化的图标选择器,font awesome系列以及iconfont系列都有办法列举出来,但是amis-ui注册的icons目前没有办法拿到,如果能够导出一个方法,把iconFactory的key导出来:
export function getIconNames() {
return Object.keys(iconFactory)
}
这样就能做出一个完美的图标选择器啦!
替代选择
还有其他方式拿到已注册的图标名么?
之前弄过一个图标选择器效果不是很好

{
"type": "group",
"body": [
{
"type": "icon-picker",
"name": "icon",
"value": "${sub_form.icon}",
"label": "fontaweson 图标",
"labelField": "text"
},
{
"type": "input-sub-form",
"name": "sub_form",
"label": "图标库",
"btnLabel": "选择",
"form": {
"title": "选择图标",
"body": [
{
"type": "service",
"schemaApi": "get:../pages/help/icon_selector.json"
}
]
}
}
]
}
通过在子表单中嵌入一个锚点导航,links设置为icon的分类,links 内添加多个 Button-Group-Select 根据适配每个放18个图标,options 内添加icon 按钮

{
"type": "wrapper",
"body": [
{
"type": "tpl",
"tpl": "您选中的icon:"
},
{
"type": "icon",
"icon": "${icon}",
"vendor": "",
"className": "text-xl"
},
{
"type": "tpl",
"tpl": " ${icon}",
"className": "text-info text-xl"
},
{
"type": "divider"
},
{
"type": "anchor-nav",
"links": []
}
]
}
links:将生成的link.json放入links中
使用Python脚本从Font Awesome网站获取图标类别及其相应图标列表,并生成一个JSON文件。脚本使用BeautifulSoup库来解析HTML并提取必要的信息。生成的JSON文件可以作为图标选择器工具的数据源。
由chatgpt 给出的python 代码
运行环境 codespace-jupyter https://github.com/github/codespaces-jupyter
import requests
import json
from bs4 import BeautifulSoup
url = 'https://fontawesome.com/v4.7.0/icons/'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# Get the category information for each group of icons
categories = soup.select('h2.page-header')
category_names = [category.text for category in categories]
# Get the icons for each category
icon_groups = []
for category in categories:
icons = []
icon_container = category.find_next_sibling('div', class_='row')
for icon in icon_container.select('.fa-hover'):
class_name = icon.find('i')['class'][1]
icons.append({"icon": "fa %s" % class_name, "value": "fa %s" % class_name})
icon_groups.append(icons)
# Generate the anchor-nav options
anchor_nav_options = []
for i in range(len(category_names)):
# Add the category information
anchor_nav_option = {"title": category_names[i], "body": []}
# Split the list of icons into smaller lists of 18 icons each
icon_subgroups = [icon_groups[i][j:j+18] for j in range(0, len(icon_groups[i]), 18)]
# Add the Button-Group-Select options for each subgroup of icons
for group in icon_subgroups:
anchor_nav_option["body"].append({"type": "button-group-select", "name": "icon", "options": group})
# Add the anchor-nav option to the list
anchor_nav_options.append(anchor_nav_option)
# Format the list as a JSON string with indentations and line breaks
result = json.dumps(anchor_nav_options, indent=4)
# Open a file for writing
with open("links.json", "w") as f:
# Write the result to the file
f.write(result)
# Close the file
f.close()
选择提交后会卡顿,子表单好像也调整不了宽度
@ArrNNG 我的思路跟你的差不多,不过除了这些第三方的图标以外,amis会内置一些svg的图标,并且提供了注册接口,我想把这些也弄进来,但是如果要手工维护一个列表就有点麻烦了