Home > OS >  How to pass semantic checkbox state (true/false) to e.target
How to pass semantic checkbox state (true/false) to e.target

Time:09-16

The aim: Pass the value of state.TNC and state.promos into the emailjs.sendForm. Submitted email should see 'true' if the box is checked, false if not.

I am lost for words as to what I need to google to find a resolution that involves semantic and emailjs...

Here is my code that I thought was going to do that for me.

value={this.state.promos}

value={this.state.TNC}

These are the states they should be returning as the values and being passed into the emailjs e.target

this.state = {
      TNC: false,
      promos: true,
    };

The code for the two checkboxes, (I have tried them with and without the value={this.state.TNC})

<Form.Group widths="equal">
          <Form.Field>
            <Checkbox
              label="I agree to the Terms and Conditions"
              required
              control={Checkbox}
              data="TNC"
              onChange={this.onToggle}
              value={this.state.TNC}
            />
          </Form.Field>
          <Form.Field>
            <Checkbox
              label="Send me occasional updates and promotions"
              defaultChecked
              control={Checkbox}
              data="promos"
              value={this.state.promos}
              onChange={this.onTogglePromos}
            />
          </Form.Field>
        </Form.Group>

Here is the full code, and you can find a semi-functional version of the form at https://test.ghostrez.net

import emailjs from "emailjs-com";
import { Component } from "react";
import { Button, Checkbox, Form, Input, TextArea } from "semantic-ui-react";

export default class ContactUs extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      TNC: false,
      promos: true,
    };
  }
  onToggle = () => {
    const TNC = !this.state.TNC;
    this.setState({ TNC });
  };
  onTogglePromos = () => {
    const promos = !this.state.promos;
    this.setState({ promos });
  };
  sendEmail = (e, props) => {
    e.preventDefault();
    if (this.state.TNC !== false) {
      return emailjs
        .sendForm("tester", "testiTemp", e.target, "user_EPYkKnHwiytfdolol565ljQTCbJkzO7YD")
        .then(
          (result) => {
            alert("Email sent successfully!");
            e.target.reset();
          },
          (error) => {
            alert("Email send failed... :( I had one job...");
          }
        );
    } else {
      return alert(
        "You need to accept the Terms and Conditions before proceeding."
      );
    }
  };

  render() {
    return (
      <Form onSubmit={this.sendEmail}>
        <Form.Group widths="equal">
          <Form.Field
            id="firstName"
            control={Input}
            label="First name"
            name="firstName"
            placeholder="First name"
            required
          />
          <Form.Field
            id="lastName"
            name="lastName"
            control={Input}
            label="Last name"
            placeholder="Last name"
          />
        </Form.Group>
        <Form.Group widths="equal">
          <Form.Field
            id="Email"
            control={Input}
            label="Email"
            name="email"
            placeholder="[email protected]"
            required
          />
          <Form.Field
            id="Phone"
            control={Input}
            label="Phone"
            name="phone"
            placeholder="0469 420 689"
            required
          />
        </Form.Group>
        <Form.Field
          id="Message"
          control={TextArea}
          label="Message"
          name="message"
          placeholder="Message"
          required
        />
        <Form.Group widths="equal">
          <Form.Field>
            <Checkbox
              label="I agree to the Terms and Conditions"
              required
              control={Checkbox}
              data="TNC"
              onChange={this.onToggle}
              value={this.state.TNC}
            />
          </Form.Field>
          <Form.Field>
            <Checkbox
              label="Send me occasional updates and promotions"
              defaultChecked
              control={Checkbox}
              data="promos"
              value={this.state.promos}
              onChange={this.onTogglePromos}
            />
          </Form.Field>
        </Form.Group>
        <Form.Field
          id="Submit"
          control={Button}
          type="submit"
          value="submit"
          positive
          content="Submit"
        />
      </Form>
    );
  }
}

Thank you for your help, I really appreciate it.

CodePudding user response:

In order to check, the property is not value but checked, try with this :

        <Checkbox
          label="I agree to the Terms and Conditions"
          required
          control={Checkbox}
          data="TNC"
          onChange={this.onToggle}
          checked={this.state.TNC}
        />
  • Related