Click child component by clicking the parent component - reactjs

Is it possible to pass the click event from the parent to the child? So when Button is clicked FileUpload is clicked too. My working code is here.
https://codesandbox.io/s/uploadbutton-78h2d
<Button>
<Dialog>
<FileUpload/>
</Dialog>
</Button>

Simply have the onClick event listener on the Button instead of the FileUpload (or have it on both but make sure you're stopping propagation so it doesn't double-fire the event.

Related

Make the children element of a parent that has onClick immune

So I have a product wrapper that I want to be clicked and on click redirect the user to the specific page of that product. And inside that is the button that can add the product to the cart, but the problem occurs here, because the wrapper has onCLick, when I click the button, the parent onClick is called, So I don't know how to make the button immune to the onCLick of the div.
<div onClick={() => navigate(product.id)}>
<button onClick={() => add(product.id)>Add to Cart</button>
</div>
When the onClick event of a child is called, the event propagates by default and calls the onClick of the parent.
To avoid that, you should call event.stopPropagation() inside the child function.
<button onClick={(event)=>{event.stopPropagation(); /* rest of your code */}}>

primereact dialog not triggering onHide fn from footer actions for wrapper component

primereact dialog not triggering onHide fn from footer action for wrapper compoent
https://codesandbox.io/s/trusting-cdn-jkn5t?file=/src/demo/DialogDemo.js
isue: open console > open dialog > press no > console not print onhide (for esc and close btn working)
Method onHide is not triggered when clicking these buttons.
It is only triggered when closing dialog from icon.
I altered your code and used ref in order to achive this via props.onHide call.
Check my
codesandbox

How to stop event from bubbling when exiting react-bootstrap modal?

Say I want to stop the event from propagating upon exit from the modal, for example when clicking on the background or the exit button which will close the modal. But then it'll redirect me to the (parent)? onClick event upon exiting.
<Modal onHide={(e) => {
e.stopPropagation();
setShow(false);
...
}}>
<Modal/>
The above does not work. I also tried onExit which also doesn't work.
The workaround was to use a div element around the Modal to control the event.
<div
onClick ={(e) => e.preventPropagation()}
>
<Modal>
<Modal/>
</div>

Font-awesome icon within button not activating onClick in React app

I am happy I can use font-awesome in my projects. I want to put some bars as my open/close button for my menu. The icon itself is not clickable, but the small area between the icon and the border still activates the onClick. The console.log I put in my event handler shows that the icon does not pass the 'name' property needed to activate the state change. Does anyone know how to get around this?
I have tried wrapping it in span and i elements. The icon does show up, but is just not activating the onClick, probably because it is not passing the 'name' property.
My event handler:
menuClick(event) {
/*event.preventDefault();*/
const name = event.target.name;
console.log(name);
this.setState({[name]:!this.state[name]})
}
My button:
<button
name="menuOpen"
style={props.data.menuOpen ?
buttonStyle :
null}
onClick={props.menuClick}
className="menuOpenButton">
<FontAwesomeIcon name="menuOpen" icon="bars" size="3x" />
</button>
and the props are being passed to the child like this:
<Header
data={this.state}
menuClick={this.menuClick} />
Changing the event handler to look for the currentTarget fixed it.
const name = event.currentTarget.name

React Semantic-UI: Modal component with Form component? Is it possible?

So, I'm trying to use Semantic UI modal component with the form component.
My problem is that if I use these two together the UI becomes bad.
I created a sandbox about my current situation: https://codesandbox.io/s/2n1pj96ry
As you can see now the submit button does not attached to the form.
If I move the Form component directly inside the Modal component, like this:
<Modal...>
<Form>
...
</Form>
</Modal>
the submit will attached to the form, but the UI breakes down.
I tried to add different classes to these components (like ui modal to the Form component, but it doesnt worked well).
Do you have any suggetsion?
Thanks for you help!
You can use the as prop on the Modal to make it a form element.
<Modal
as={Form}
onSubmit={e => handleSubmit(e)}
open={true}
size="tiny">
Any button with the submit type in your modal will fire the onSubmit handler. I find this to be a nice way to opt-in to required fields and easy validation by the browser on form elements.
Be sure to pass the event to your submit handler and use the preventDefault method to avoid the browser from automatically trying to post your form.
Forked your sandbox and made a working example. The modal is changed to a <form> element, the Input has the required property and the browser will demand the element is valid before firing the onSubmit handler. The default form action is prevented, and you can handle as desired with whatever.

Resources