将图标从fontaweslyicon上的功能(on Change文本输入)更改

发布于 2025-01-17 14:31:45 字数 1539 浏览 0 评论 0原文

我正在考虑根据之前的用户交互更改使用 fontawesome-react 包设置的图标。

用户在屏幕上有一个输入字段和按钮(其中包含一个图标)。如果输入为空,则

会显示不同的图标无用户输入:

+-----------------------+-----------+
| Search Placeholder... | (Shuffle) |
+-----------------------+-----------+

用户输入:

+-----------------------+-----------+
| Value                 | (Search)  |
+-----------------------+-----------+

内手动设置图标时, 我能够找到faSearch,但是当使用状态设置它时,我收到错误:

Could not find icon {prefix: 'fas', iconName: 'faSearch'}

这是我到目前为止的看法。如果有人能够提供帮助并能够解释我所缺少的内容,我会非常高兴。

class Search extends React.Component {

  state = {
    displayIcon: "faSearch",
  }
  
  onChange(event) {
    console.log(event.target.value)

    if(event.target.value === ''){
      this.setState({
        displayIcon: "faShuffle",
      })
    } else {
      this.setState({
        displayIcon: "faSearch",
      })
    }
  }

  render(){
    const { displayIcon } = this.state //destucture state

    console.log( );
    return (
      <>
      
        <InputGroup className="mb-3">
        <FormControl
          placeholder="search or feel lucky..."
          onChange={this.onChange}
        />

        <Button variant="primary" id="search" size="lg">
          <FontAwesomeIcon icon={displayIcon} />
        </Button>

        </InputGroup>
      </>
    )
  }

}

I'm thinking about changing the icon which is set using the fontawesome-react package depending on a previous user interaction.

On screen the user has a input field and button (which contains an icon). If the Input is empty a different icon is presented

No User Input:

+-----------------------+-----------+
| Search Placeholder... | (Shuffle) |
+-----------------------+-----------+

User Input:

+-----------------------+-----------+
| Value                 | (Search)  |
+-----------------------+-----------+

when setting the icon manually inside the <FontAwesomeIcon> I'm able to find faSearch, When however setting it using state I get the error:

Could not find icon {prefix: 'fas', iconName: 'faSearch'}

Here's my take so far. I'd be really happy if someone is able to assist and is able to explain what I'm missing.

class Search extends React.Component {

  state = {
    displayIcon: "faSearch",
  }
  
  onChange(event) {
    console.log(event.target.value)

    if(event.target.value === ''){
      this.setState({
        displayIcon: "faShuffle",
      })
    } else {
      this.setState({
        displayIcon: "faSearch",
      })
    }
  }

  render(){
    const { displayIcon } = this.state //destucture state

    console.log( );
    return (
      <>
      
        <InputGroup className="mb-3">
        <FormControl
          placeholder="search or feel lucky..."
          onChange={this.onChange}
        />

        <Button variant="primary" id="search" size="lg">
          <FontAwesomeIcon icon={displayIcon} />
        </Button>

        </InputGroup>
      </>
    )
  }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏有森光若流苏 2025-01-24 14:31:45

您也可以这样做并有条件地渲染它:

class Search extends React.Component {
  state = {
    value: '',
  };

  onChange(event) {
    this.setState({
      value: event.target.value,
    });
  }

  render() {
    const { value } = this.state; //destucture state

    return (    
        <InputGroup className='mb-3'>
          <FormControl placeholder='search or feel lucky...' onChange={this.onChange} />

          <Button variant='primary' id='search' size='lg'>
            {value ? <FontAwesomeIcon icon={faSearch} /> : <FontAwesomeIcon icon={faShuffle} />}
          </Button>
        </InputGroup>
    );
  }
}

you can also do that like this and render it conditionally:

class Search extends React.Component {
  state = {
    value: '',
  };

  onChange(event) {
    this.setState({
      value: event.target.value,
    });
  }

  render() {
    const { value } = this.state; //destucture state

    return (    
        <InputGroup className='mb-3'>
          <FormControl placeholder='search or feel lucky...' onChange={this.onChange} />

          <Button variant='primary' id='search' size='lg'>
            {value ? <FontAwesomeIcon icon={faSearch} /> : <FontAwesomeIcon icon={faShuffle} />}
          </Button>
        </InputGroup>
    );
  }
}
瞳孔里扚悲伤 2025-01-24 14:31:45

也许您可以尝试为 fa-search 传递一个字符串,而不是传递一个对象,如下所示:

onChange 中的实现是:

if(event.target.value === ''){
      this.setState(
        'fa fa-shuffle', // don't know this one check it on https://fontawesome.com/v5/icons/random?s=solid
      )
    } else {
      this.setState(
        'fas fas-search'
      )
    }

Maybe instead of passing an object you could try to pass a string for fa-search like so:

<FontAwesomeIcon icon='fas fas-search />

The implementation in onChange being:

if(event.target.value === ''){
      this.setState(
        'fa fa-shuffle', // don't know this one check it on https://fontawesome.com/v5/icons/random?s=solid
      )
    } else {
      this.setState(
        'fas fas-search'
      )
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文