[RaisedButton] (all buttons) should default to <button> tag
Problem description
Semantically, button elements in a <form> should <button> elements, not <a> tags. I'm trying to take advantage of the onSubmit event on a form. By default, a button element in a form when pressed would trigger onSubmit. However, since Material UI buttons are rendered as <a> tags, this default browser behavior is broken.
Steps to reproduce
Put a <RaisedButton> in a <form> with an onSubmit handler and click it. The form is not submitted. Instead, the user is re-directed to https://github.com/callemall/material-u.
Versions
- Material-UI:
0.15.4 - React:
15.3.1 - Browser: all
Proposed Solution
I think we could solve this by only using an <a> tag if an href is specified, otherwise default to a <button>.
If you were not using Material UI components like RaisedButton, you can certainly take advantage of onSubmit event on a form. In the case of using Material UI components like RaisedButton or FlatButton, you cannot use onSubmit. To handle click events with MaterialUI buttons, you will have to use onTouchTap instead of onSubmit or onClick.
Have a look at this code below. This essentially uses RaisedButton component inside the form as you mentioned and works fine for me. Let me know if this approach worked for you.
Some react form component libraries and helpers rely on the built in onSubmit handler on the <form> element, so I can't work around it like you described. In a normal component, something like this would work:
function MyForm(props) {
return (
<form onSubmit={() => console.log('submit!')}>
<input type= "text" ref= {(value) => this.name = value}/>
<button type="submit">Submit</button>
</form>
)
}
If you swap out that button with a Material-UI button, the functionality breaks, because Material-UI buttons render an <a /> tag. I think it makes more sense that buttons should be <button> elements. Anchor tags are for navigation. Buttons are for forms and UI. I don't think any of the buttons in my app are used for navigation. They're all for manipulating the UI or submitting information.
I tried using onSubmit handler on the from element and it works just fine. This is my code. I have prevented the default response of a submit handler in the code below to test it to see the console.log statement but submit works too.
class Child extends Component{
render(){
return(
<MuiThemeProvider>
<div>
<form onSubmit={(e) => { e.preventDefault(); console.log('On submit')}}>
<input type= "text"/>
<RaisedButton type = "submit" className = "FormButton" label="Submit"></RaisedButton>
</form>
</div>
</MuiThemeProvider>
)
}
}
export default Child
Here is another Example where I am also using the value entered in the input box and using a defined function.
class Child extends Component{
constructor(props) {
super(props);
this.onFormSubmit = this.onFormSubmit.bind(this)
}
onFormSubmit(e) {
e.preventDefault();
console.log(this.name.value)
}
render(){
return(
<MuiThemeProvider>
<div>
<form onSubmit={this.onFormSubmit}>
<input type= "text" ref= {(value) => this.name = value}/>
<RaisedButton type = "submit" className = "FormButton" label="Submit"></RaisedButton>
</form>
</div>
</MuiThemeProvider>
)
}
}
export default Child
Looks like this has already been addressed since I opened this issue:
https://github.com/callemall/material-ui/blob/master/src/internal/EnhancedButton.js#L335-L339