简体   繁体   English

有什么方法可以通过Semantic-UI获得动态图标?

[英]Is there any way to get a dynamic icon with Semantic-UI?

I have this Icon from Semantic-UI with React 我有来自带有React的Semantic-UI的图标

<Icon name={`idea ${words}`} /> where words can be any string of words based on user input. <Icon name={`idea ${words}`} /> ,其中单词可以是基于用户输入的任何字符串。 Is there a way to get this to not error out? 有没有办法使它不会出错? I'm getting errors like these right now 我现在遇到这样的错误

Warning: Failed prop type: Invalid prop `name` of value `user github` supplied 
to `Icon`.

Instead of `user github`, did you mean:
  - user
  - users
  - github

where it looks like it only wants one word but I want it to be able to take multiple. 看起来它只想要一个字,但我希望它可以取多个字。

Is there a way for when the Icon loads to display the trophy icon, but when people input a word that matches an Icon in Semantic-UI's library it switches to that one? 有没有一种方法可以在图标加载时显示奖杯图标,但是当人们在语义UI的库中输入与图标匹配的单词时,它会切换到那个图标? And when they delete that word(s) it should fall back to the trophy icon. 并且当他们删除该单词时,它应该退回到奖杯图标。

Thanks! 谢谢!

The error is due to the test by propTypes . 该错误归因于propTypes的测试。 It should not happen in production as React disables it there automatically, but it shows up in development to warn you about passing wrong prop. 它不会在生产中发生,因为React会在此处自动禁用它,但它会在开发中显示,以警告您传递错误的道具。

If you want to make sure the input is a valid prop, a workaround is checking words to make sure it exists in the provided icons by semantic-ui. 如果要确保输入是有效的道具,则一种变通方法是检查words以确保语义UI在提供的图标中存在该输入。

You can get a list of icons by importing it like this: import {ALL_ICONS_IN_ALL_CONTEXTS} from 'semantic-ui-react/src/lib/SUI'; 您可以像这样import {ALL_ICONS_IN_ALL_CONTEXTS} from 'semantic-ui-react/src/lib/SUI';图标来获取图标列表: import {ALL_ICONS_IN_ALL_CONTEXTS} from 'semantic-ui-react/src/lib/SUI';

ALL_ICONS_IN_ALL_CONTEXTS is an array of icon names, so just check and see if the passed name is in that array. ALL_ICONS_IN_ALL_CONTEXTS是图标名称的数组,因此只需检查并查看传递的名称是否在该数组中。

This might have not been the best way to do it, but this is what I did. 这可能不是最好的方法,但这就是我所做的。 First I added an ID to each Icon I wanted to be dynamic. 首先,我向每个要动态化的图标添加了一个ID。

<Icon ... id={`...-${index}`} color="teal" name={`${DEFAULT_ICON}`} />

next, I subscribed the input field to an onChange event and did this. 接下来,我将输入字段订阅了onChange事件并执行了此操作。

const val = e.target.value; (Value of the Input Field)
let icon = `${DEFAULT_ICON}`; (Whatever you want the fallback icon to be
let words = val.split(' ');

for (let i = 0; i < words.length; i++) {
  let word = words[i];
  if (ALL_ICONS_IN_ALL_CONTEXTS.indexOf(word.toLowerCase()) > -1) {
    icon = word;
  }
}
$(`#...-${index}`)[0].className = `teal icon ${icon}`;

Keep in mind this solution requires these two imports. 请记住,此解决方案需要这两个导入。

import { ALL_ICONS_IN_ALL_CONTEXTS } from 'semantic-ui-react/dist/commonjs/lib/SUI';
import $ from 'jquery';

This solution also takes the last valid word, so if the input field contains "idea wizard", it will use the wizard icon. 此解决方案还使用最后一个有效字,因此,如果输入字段包含“ idea向导”,它将使用向导图标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM