score:5
Accepted answer
const { RaisedButton, MuiThemeProvider, getMuiTheme, TextField } = MaterialUI;
class Test extends React.Component {
state = {
value: 'good',
error: false,
successValidation: false
}
handleInputChange = (e, newValue) => {
// reset errors and validation as well
this.setState({
value: newValue,
error: false,
successValidation: false
});
}
isValid = () => {
const {value} = this.state;
return value != 'bad';
}
onSubmit = () => {
if(this.isValid()) {
this.setState({successValidation: true, error: false});
} else {
this.setState({error: true});
}
}
render = () => {
const {value, error, successValidation} = this.state;
const underlineStyle = successValidation ? { borderColor: 'green' } : null;
return (
<div style={{width: '50%', margin: '0 auto'}}>
<h3>The input "bad" will cause error</h3>
<h3>All other input will cause green underline on submit</h3>
<TextField
errorText={error ? 'Validation error!' : ''}
name="test"
value={value}
onChange={ this.handleInputChange }
underlineStyle={underlineStyle}
/>
<RaisedButton onClick={ this.onSubmit }>Submit</RaisedButton>
</div>
);
}
}
const App = () => (
<MuiThemeProvider muiTheme={getMuiTheme()}>
<Test />
</MuiThemeProvider>
);
ReactDOM.render(
<App />,
document.getElementById('container')
);
Source: stackoverflow.com
Related Query
- I use React js and Material-UI. How can i validate "correct" green color validation on button submit click?
- How can I use React Material UI's transition components to animate adding an item to a list?
- How can I create a color variant from a base color in React Material UI?
- What does forwardRef by a component in React dev tools mean and how can I use it?
- How can I use useEffect in React to run some code only when the component first mounts, and some other code whenever an event occurs (repeatedly)?
- How can I access to change styles of inner items of a component in react and material ui?
- How can I validate an Autocomplete multiple TextField using React Hook Form and Yup?
- how to use button as link in material-ui react with out underline and change color text?
- How can I validate on blur with Semantic UI React and React Hook Form?
- How can I create a counter as components to use multiple times and create one button to reset all the counter in React JS?
- How can I pass a value from the backend to a React frontend and use that value in the CSS for the frontend?
- How can I use React Testing Library to simulate clicking on Material UI RadioGroup option?
- How can you use CSS modules together with Typescript, React and Webpack?
- How can i store the value of a React input element and use it in some other elements
- How can I validate React input is not empty and valid email?
- How can I get the value of an input field and use it in URL in React
- I am learning react and my code is working fine but how can i declare currDate single time to use it globally to use in useState
- How can I use React context to get the router exactly and only where I need it?
- React - How can I let visitors use react dev tools but can not edit states and props
- How I can create a simple React component rendered as a <div> which takes a text value and a foreground color as input
- How can you pass props and use HOC with React context
- How can I import and use data from a JSON file in my React app?
- How can I get a Material UI React Textfield to not be overlayed by text, when it has position sticky and it has been scrolled up?
- How can we disable/hide the Add ,edit and delete icons in material table in react JS?
- How can I use scroll into view from different/other page with react and redux?
- How can I define a background image to fill the screen and remove edges on the left and right in React with Material UI?
- How can I use React Router to nest some routes? And I want to keep a header component
- React JS: How can I use the ID of a created doc in Firebase and place it in a modal box?
- How can I get the value of an element onClick in React and use it as condition
- How can I add a user input with an output and be able to display using React Javascript/ Material UI
More Query from same tag
- TypeScript/React. How to check returned value of internal function
- 'i18next-http-backend' module couldn't be load
- React - infinite loop using get request when using useState hook
- Problems with useState
- JSX Element does not have any construct or call signatures
- show loader while fetching initial data in react-redux component
- How to create Excel file with right-to-left sheet column order in React?
- React: Clean-Up function never called
- Unexpected error when querying for a field from Firebase
- Data models and business logic in isomorphic (React/Redux/Express/Mongo) app
- How to connect redux store using useSelector() when input fields already mapped using useState()
- Uncaught Error: Actions must be plain objects. Instead, the actual type was: 'undefined'
- Looping through simple object properties in react JSX
- Why must I delete the node_modules folder every time I pull from github
- Load an svg file without modifying webpack in react
- Cant use dot or bracket notation to access keys or data in json/javascript object saved to state after fetch
- Reactjs=> React App rendering blank page error
- Spread props based on conditional check
- Can React Native apps be tested in a browser?
- Error: Invariant failed: You should not use <Route> outside a <Router>
- Creating a text,value pair using dictionary in python or reactjs
- Efficiently adding click listener to an arbitrary number of elements
- Get the Value of react redux in the child at the Component that call on React JS
- How to Pass Id correctly to Rest API Endpoint from React
- Reusing React 'key'
- React: 'Redirect' is not exported from 'react-router-dom'
- Is it possible hide any input fields of a form during a cypress test?
- how to update a nested object which have made up by EntityAdapter redux?
- using debounce for search input in react
- Standardjs no-unused-vars while it's used