首页 > 解决方案 > React, Graphql 选择表单

问题描述

我是 graphql react 的新手,我有这个选择输入,但我无法接收数据。请有人帮助我。谢谢。

import React, { Component } from "react";
import { graphql } from "react-apollo";

import gql from "graphql-tag";

class EmployeeCreate extends Component {
  constructor(props) {
    super(props);
    this.state = { firstName: "", lastName: "", type: "", status: "" };
  }

  onSubmit(event) {
    event.preventDefault();
    console.log(this.props);
    this.props
      .mutate({
        variables: {
          firstName: this.state.firstName,
          lastName: this.state.lastName,
          type: this.state.type,
          status: this.state.status,
          groupId: this.props.groupId
        }
      })
      .then(() =>
        this.setState({ lastName: "", firstName: "", type: "", status: "" })
      );
  }

  render() {
    return (
      <div className="ui six wide column">
        <div className="ui sticky">
          <ul className="list-group">
            <li className="list-group-item d-flex justify-content-between align-items-center active">
              EMPLOYEE FORM
            </li>
            <li className="list-group-item d-flex justify-content-between align-items-center">
              <form onSubmit={this.onSubmit.bind(this)}>
                <div className="form-row">
                  <div className="form-group col-md-6">
                    <label>First Name</label>
                    <input
                      type="text"
                      className="form-control"
                      placeholder="First Name"
                      onChange={event =>
                        this.setState({ firstName: event.target.value })
                      }
                      value={this.state.firstName}
                    />
                  </div>
                  <div className="form-group col-md-6">
                    <label>Last Name</label>
                    <input
                      type="text"
                      className="form-control"
                      placeholder="Last Name"
                      onChange={event =>
                        this.setState({ lastName: event.target.value })
                      }
                      value={this.state.lastName}
                    />
                  </div>
                </div>
                <div className="form-row">
                  <div className="form-group col-md-6">
                    <label>Type</label>
                    <select
                      className="form-control"
                      onChange={event =>
                        this.setState({ type: event.target.value })
                      }
                      value={this.state.type}
                    >
                      <option value="'REGULAR'">Regular</option>
                      <option value='EXTRA'>Extra</option>
                      <option value='OTHERS'>Others</option>
                    </select>
                  </div>
                  <div className="form-group col-md-6">
                    <label>Status</label>
                    <select
                      className="form-control"
                      onChange={event =>
                        this.setState({ status: event.target.value })
                      }
                      value={this.state.value}
                    >
                      <option>Active</option>
                      <option>Inactive</option>
                    </select>
                  </div>
                </div>
                <hr />
                <button type="submit" className="btn btn-primary">
                  Submit
                </button>
              </form>
            </li>
          </ul>
        </div>
      </div>
    );
  }
}

const mutation = gql`
  mutation AddEmployeeToGroup(
    $firstName: String
    $lastName: String
    $type: String
    $status: String
    $groupId: ID
  ) {
    addEmployeeToGroup(
      firstName: $firstName
      lastName: $lastName
      type: $type
      status: $status
      groupId: $groupId
    ) {
      id
      employees {
        id
        firstName
        lastName
        type
        status
      }
    }
  }
`;
export default graphql(mutation)(EmployeeCreate);

这是我的 EmployeeCreate javascript 文件。我可以收到我的名字和姓氏正在工作,只有选择不起作用。这是我的 graphql 网络结果。

11:{id:“5bda6fd576cca2030667f2b2”,名字:“aaa”,姓氏:“aaa”,类型:“”,状态:“”,...}

标签: reactjsgraphqlgraphql-js

解决方案


有人帮助了我,但他删除了他的评论。我刚刚用他的回答解决了我的问题。谢谢。

const options = {
 REGULAR: "Regular",
 EXTRA: "Extra",
 OTHERS: "Others"
};

this.state = { firstName: "", lastName: "", type: "REGULAR", status: "" };

                <select
                  className="form-control"
                  onChange={event =>
                    this.setState({ type: event.target.value })
                  }
                  value={this.state.type}
                >
                  {Object.keys(options).map(option => {
                    return (
                      <option
                        key={option}
                        value={option}

                      >
                        {option}
                      </option>
                    );
                  })}
                </select>

推荐阅读