0

I have the following code where a text input box accepts an url and if it is valid it should create a display area and provide a nice preview of the url.

But I am not sure how to associate the submit from the button with the submit from the form.

import React, {useState} from 'react';
import './App.css';
import Microlink from '@microlink/react';
import { String } from 'core-js';
import Card from '@material-ui/core/Card';
import TextField from '@material-ui/core/TextField';
import Button from "@material-ui/core/Button";

function validURL(str) {
  var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
    '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
    '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
    '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
    '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
    '(\\#[-a-z\\d_]*)?$','i'); // fragment locator
  return !!pattern.test(str);
}

function App (){
  const [myurl, setMyurl] = useState("")
  const [display, setDisplay] = useState(true)

  const handleChange = (event) => {
    const url = event.target.value    
    var str = validURL(url)
    console.log(str)
    str ? setMyurl(String(url)) : setDisplay(false)
  }

  return (    
    <form>
      <TextField
        id="outlined-name"
        label="Name"        
        margin="normal"
        variant="outlined"
        onSubmit={event => handleChange(event)}        
      />
      <Button
        type="submit"
        variant="contained"                          
      >
       Submit
      </Button>

      {display ? 
      <Card>
      <Microlink url={myurl}/>;        
    </Card> : null}


    </form>

  )
}

export default App;

1 Answer 1

2

You need to put the onSubmit inside the form tag like this. Also need to have a controlled form. Meaning, you need an onChange, I wrote it for you.

 return (    
    <form onSubmit={event => handleChange(event)} >
      <TextField
        id="outlined-name"
        label="Name"        
        margin="normal"
        variant="outlined"
        value={myurl}
        onChange={e => setMyUrl(e.target.value)}

      />
      <Button
        type="submit"
        variant="contained"                          
      >
       Submit
      </Button>

      {display ? 
      <Card>
      <Microlink url={myurl}/>;        
    </Card> : null}


    </form>

  )
}

export default App;
Sign up to request clarification or add additional context in comments.

4 Comments

That's not enough. They would also need at minimum a change handler on the TextField as well as value to ensure value of the input gets updated.
yeah i didn't look at it close enough I made update
So, submit in form is able to automatically figure that Button submit is married to them and it doesn't need any explicit mention anywhere ?
yeah because on the button you have type="submit"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.