Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnClick property on FontAwesomeIcon typescript issue #196

Open
e-osuna-g opened this issue Oct 22, 2018 · 35 comments
Open

OnClick property on FontAwesomeIcon typescript issue #196

e-osuna-g opened this issue Oct 22, 2018 · 35 comments

Comments

@e-osuna-g
Copy link

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

@robmadole
Copy link
Member

@cuauth can you provide a reproduction of this?

@e-osuna-g
Copy link
Author

e-osuna-g commented Oct 25, 2018

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

@robmadole
Copy link
Member

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

@e-osuna-g
Copy link
Author

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

@robmadole
Copy link
Member

@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?

@e-osuna-g
Copy link
Author

e-osuna-g commented Nov 8, 2018

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

@LaurensRietveld
Copy link

@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

@kontinuity
Copy link

kontinuity commented Jan 24, 2019

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'.

@gytisgreitai
Copy link

@robmadole any news on this?

@e-osuna-g
Copy link
Author

e-osuna-g commented Apr 19, 2019 via email

@Plasmoxy
Copy link

Plasmoxy commented Apr 22, 2019

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

@peetjvv
Copy link

peetjvv commented Apr 29, 2019

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.

@DiegoGorgazzi
Copy link

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.

@siffash
Copy link

siffash commented Jun 13, 2019

@cuauth any news on this?

@elramus
Copy link

elramus commented Jun 13, 2019

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.

@siffash
Copy link

siffash commented Jun 14, 2019

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?

@kolendomichal
Copy link

kolendomichal commented Jun 27, 2019

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

@fulin426
Copy link

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.

@siffash
Copy link

siffash commented Jul 18, 2019

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

@fulin426
Copy link

@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.

@e-osuna-g
Copy link
Author

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

@lwlewy
Copy link

lwlewy commented Aug 15, 2019

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

@JulienDemarque
Copy link

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

@JeremiahChurch
Copy link

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

@NimrodBZB
Copy link

NimrodBZB commented Nov 27, 2019

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

@robmadole
Copy link
Member

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 robmadole reopened this Dec 2, 2019
@robmadole
Copy link
Member

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

@ihaseebkhan
Copy link

I think this issue is fixed as onClick is directly working on FontAwesomeIcon tag.

@robmadole
Copy link
Member

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

@peetjvv
Copy link

peetjvv commented May 5, 2020

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

@kwent
Copy link

kwent commented Jul 28, 2020

Same here works with v0.1.11 ! Thank you

@achievermina
Copy link

I don't see onClick props in FontAwesomeIcon.

"@fortawesome/fontawesome-svg-core": "^1.2.35",
"@fortawesome/free-brands-svg-icons": "^5.15.3",
"@fortawesome/free-solid-svg-icons": "^5.15.3",
"@fortawesome/react-fontawesome": "^0.1.14",

@robmadole
Copy link
Member

@achievermina what do you mean that you don't see them? In you code editor?

@bartektartanus
Copy link

I can also confirm that it is working. Version 0.1.16

@peetjvv
Copy link

peetjvv commented Nov 18, 2021

yes, this issue can be closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests