react-fontawesome icon indicating copy to clipboard operation
react-fontawesome copied to clipboard

OnClick property on FontAwesomeIcon typescript issue

Open e-osuna-g opened this issue 7 years ago • 35 comments

adding Onclick property to FontAwesomeIcon gives this error in the compiler. example: <FaIcon onClick={this.showSettings} className={style.settingsIcon + ' ' +strIconClicked} icon="cog" /> TS2339: Property 'onClick' does not exist on type 'IntrinsicAttributes & Props'. i needed to wrap my <FaIcon in a span with the onClick, is there a way to add onClick and other Events to FontAwesomeIcon in typescript.

Thanks

e-osuna-g avatar Oct 22 '18 23:10 e-osuna-g

@cuauth can you provide a reproduction of this?

robmadole avatar Oct 25 '18 14:10 robmadole

Sure,

import React from "react"
import ReactDOM from "react-dom"
import { library } from '@fortawesome/fontawesome-svg-core';
import { faHome } from '@fortawesome/free-solid-svg-icons';
library.add( faHome);
import { FontAwesomeIcon as FaIcon } from '@fortawesome/react-fontawesome'
const onClick = function(){ console.log('Log')}
const Index = () => {
  return <FaIcon  onClick={onClick} icon="home"  />
};

ReactDOM.render(<Index />, document.getElementById("index")); 

this is the smallest reproduccion of code that i could make in my local environment maybe you will like it better in stackblitz https://stackblitz.com/edit/react-ts-npecft

e-osuna-g avatar Oct 25 '18 16:10 e-osuna-g

How about a cloneable GitHub repo? That Stackblitz doesn't run locally.

robmadole avatar Oct 25 '18 17:10 robmadole

first time i run one of this, i know there's a lot of unused tools in the package json, but i took it from another project. https://github.com/cuauth/FontAwesomeOnClick

e-osuna-g avatar Oct 25 '18 17:10 e-osuna-g

@cuauth I've release 0.1.4-1 that I think should fix this up. Can you give it a try in your real project?

robmadole avatar Nov 08 '18 17:11 robmadole

i'll hit it this weekend, and post updates, thanks

e-osuna-g avatar Nov 08 '18 17:11 e-osuna-g

@robmadole It works for me, but other valid attributes (like title) are not supported yet via these typings. How about extending from React.HTMLAttributes instead of React.DOMAttributes? I also got an import error at line 2 (import React from 'react'). Chaning this to import * as React from 'react' fixed it for me

LaurensRietveld avatar Dec 28 '18 13:12 LaurensRietveld

Can confirm this is not working on latest version.

import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
...
<FontAwesomeIcon icon={faStar} pull="right" onClick={() => message.success('Added to favorites!')} />

Results in:

TS2322: Type '{ icon: IconDefinition; pull: "right"; onClick: () => MessageType; }' is not assignable to type 'IntrinsicAttributes & Props'.
  Property 'onClick' does not exist on type 'IntrinsicAttributes & Props'.

kontinuity avatar Jan 24 '19 04:01 kontinuity

@robmadole any news on this?

gytisgreitai avatar Apr 19 '19 11:04 gytisgreitai

I haven't check it out but I will do it today if that's ok with you

On Fri, Apr 19, 2019, 4:17 AM Gytis Ramanauskas [email protected] wrote:

@robmadole https://github.com/robmadole any news on this?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/FortAwesome/react-fontawesome/issues/196#issuecomment-484856501, or mute the thread https://github.com/notifications/unsubscribe-auth/ABMWBJZQHCO7LNYWEV3OYYTPRGS35ANCNFSM4F6RTWFA .

e-osuna-g avatar Apr 19 '19 13:04 e-osuna-g

Same here, I had to wrap the FontAwesomeIcon with a span element to make it clickable.

Plasmoxy avatar Apr 22 '19 13:04 Plasmoxy

Thanks to @Plasmoxy for the tip, but it would be nice if we can get a working onClick prop. It's a little annoying to have to wrap all the icons in spans.

peetjvv avatar Apr 29 '19 09:04 peetjvv

I couldn't make onClick work no matter what I wrapped FontAwesomeIcon with. I was able to make it clickable ONLY when wrapping it with an anchor element, but kind of useless if onClick isn't working.

I ended up using unicode instead (I needed arrows) and fudged things via CSS.

DiegoGorgazzi avatar May 04 '19 20:05 DiegoGorgazzi

@cuauth any news on this?

siffash avatar Jun 13 '19 09:06 siffash

Yeah i get the same error here too, BUT -- are you sure you really want the onClick on the icon in the first place? Wrapping it in either a button or a link is the more accessibly correct way to do this, unless you're also adding aria stuff and tabindex to the icon too.

elramus avatar Jun 13 '19 13:06 elramus

Yes, ultimately I ended up using a wrapper tag, BUT since in general it IS possible to add onClick to SVGs why do we need to restrict developers in the first place?

siffash avatar Jun 14 '19 08:06 siffash

Issue is still relevant. I had to wrap my FontAwesomeIcon in <span> attribute

kolendomichal avatar Jun 27 '19 15:06 kolendomichal

Is wrapping the icon in a div or a span the only way to go around this issue? Without typescript I can put a onClick handler directly on the icon.

fulin426 avatar Jul 17 '19 17:07 fulin426

@fulin426 Please refer to the issue's name (it says "typescript issue").

siffash avatar Jul 18 '19 14:07 siffash

@siffash Maybe I wasn't clear with my issue, it's essentially the same as the OP. Typescript is not allowing me to attach a onClick handler directly on the Fontawesome component.

fulin426 avatar Jul 18 '19 19:07 fulin426

yeah thats the only workaround for typescript as far as i know.

e-osuna-g avatar Jul 18 '19 20:07 e-osuna-g

Unfortunately, wrapping doesn't seem to work on IE11 :(

lwlewy avatar Aug 15 '19 16:08 lwlewy

Maybe this issue should be re-opened. It's still throwing an error in Typescript.

JulienDemarque avatar Oct 08 '19 12:10 JulienDemarque

why was this issue closed? it's still an issue with current 5.11.2

JeremiahChurch avatar Oct 11 '19 20:10 JeremiahChurch

I think this issue should be re-opened, onClick is not working, tried wrapping it with span and it still not working for me

NimrodBZB avatar Nov 27 '19 13:11 NimrodBZB

PRs are welcome on this issue. We don't use Typescript so it's going to be hard for us to know if we have fixed this or not. What about this file needs to change?

robmadole avatar Dec 02 '19 17:12 robmadole

If anyone can provide a reproducible test case that we can run locally that would also help a lot.

robmadole avatar Dec 02 '19 17:12 robmadole

I think this issue is fixed as onClick is directly working on FontAwesomeIcon tag. <FontAwesomeIcon onClick={this.onClickHandler} icon={faShareAlt} />

ihaseebkhan avatar Jan 31 '20 20:01 ihaseebkhan

We just released 0.1.9 if someone can re-test this.

robmadole avatar Mar 05 '20 22:03 robmadole

I can confirm that it's working as expected for me now. I'm using v0.1.9

peetjvv avatar May 05 '20 13:05 peetjvv