Typescript version

by Terrance Smith

HTML

<div id="react-app"></div>
<script src="https://cdn.jsdelivr.net/react/0.14.0-rc1/react.js"></script>
<script src="https://cdn.jsdelivr.net/react/0.14.0-rc1/react-dom.js"></script>

CSS

body {
  font-family: Tahoma, sans-serif;
  margin: 0;
}

.ContactView-title {
  font-size: 24px;
  padding: 0 24px;
}

.ContactView-list {
  list-style: none;
  margin: 0;
  padding: 0;
  border-top: 1px solid #f0f0f0;
}

.ContactItem {
  margin: 0;
  padding: 8px 24px;
  border-bottom: 1px solid #f0f0f0;
}

.ContactItem-name {
  font-size: 16px;
  font-weight: bold;
  margin: 0;
}

.ContactItem-email {
  font-size: 14px;
  margin-top: 4px;
  font-style: italic;
  color: #888;
}

.ContactItem-description {
  font-size: 14px;
  margin-top: 4px;
}

.ContactForm {
  padding: 8px 24px;
}

.ContactForm > input,
.ContactForm > textarea {
  display: block;
  width: 240px;
  padding: 4px 8px;
  margin-bottom: 8px;
  border-radius: 3px;
  border: 1px solid #888;
  font-size: 14px;
}

TypeScript

/*
Refactor your solution to the first exercise using what you’ve learned. In particular:

Create a ContactItem class which accepts three properties: name, email and description
Filter out contacts with no e-mail address
Store your data in an array instead of passing it directly to createElement
Use the following data:


When you’re done, check your work against this fiddle:
*/

class Contact {
  constructor(key, name, email, description) {
    this.key = key;
    this.name = name;
    this.email = email;
    this.description = description;
  }
  key: string;
  name: string;
  email: string;
  description: string;
}

var contacts: Array < Contact > = [
  new Contact(1, "James K Nelson", "[email protected]", "Front-end Unicorn"),
  new Contact(2, "Jim", "[email protected]", ""),
  new Contact(3, "Joe", "", "")
];

var ContactForm = React.createClass({
  propTypes: {
    contact: React.PropTypes.object.isRequired,
    onChange: React.PropTypes.func.isRequired
  },

  render: function() {
    return React.createElement('form', {
        className: 'ContactForm'
      },
      React.createElement('input', {
        type: 'text',
        id: 'name',
        placeholder: 'Name',
        value: this.props.contact.name,
      }),
      React.createElement('input', {
        type: 'email',
        id: 'email',
        placeholder: 'Email',
        value: this.props.contact.email,
      }),
      React.createElement('textarea', {
        id: 'description',
        name: 'description',
        placeholder: 'Description',
        value: this.props.contact.description,
      }),
      React.createElement('button', {
        'type': 'submit'
      }, 'Add Contact'),
    )
  }
});

var ContactItem = React.createClass({
  propTypes: {
    name: React.PropTypes.string.isRequired,
    email: React.PropTypes.string.isRequired,
    description: React.PropTypes.string
  },
  render: function() {
    return (
      React.createElement('li', {
          className: 'ContactItem'
    ...