Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <section>
</section>

              
            
!

CSS

              
                header {
  background-color: #333;
  color: #fff;
  padding: 10px 20px;
}
section {
  padding: 20px;
}
button {
  transition: all .25s ease-out;
  border: solid 2px #ccc;
}
.list--spreadsheet {
  display: inline-block;
  padding: 10px;
  border: solid 1px #ccc;
  list-style: none;
}
.list--spreadsheet__item {
  padding: 5px;
  border-bottom: solid 1px #ccc;
}
.list--spreadsheet__item:last-of-type {
  border: none;
}
.list-item__button--delete {
  background-color: red;
  color: #fff;
  border-color: red;
  transition: all .25s ease-out;
  cursor: pointer;
}
.list-item__button--delete:hover {
  background-color: #fff;
  color: red;
}
.list--spreadsheet__item label {
  display: inline-block;
  width: 90px;
}
#spreadsheet {
  border-collapse: collapse';
}
#spreadsheet td {
  border: 1px solid #999;
  padding: 0;
  text-align: right;
  padding: 0px 10px;
}
#spreadsheet td input {
  border: none !important;
  width: 90px;
  font-size: 16px !important;
  padding: 10px;
  margin-right: 5px;
}
#spreadsheet span.total {
  display: block;
  padding-right: 10px;
  text-align: left;
}
#sum-of-rows {
  text-align: left;
}
#spreadsheet th {
  width: 100,
  text-align: center'
  background-color: #333;
  border: 'solid 1px #333;
  color: #fff;
}
#spreadsheet th:last-of-type {
  width: 20px;
}
#spreadsheet td:first-child, tfoot td {
  background-color: lightblue;
  color: #666;
  padding: 1px 3px;
  font-weight: bold;
  text-align: right;
}
.list--spreadsheet__item input {
  border: solid 1px #ccc;
}
#spreadsheet input:hover { 
  background-color: #eee
}
#spreadsheet input:focus { 
  background-color: #ccf;
},
#spreadsheet input:not(:focus) {
    text-align: right;
}
#newRowForm {
  flex: none;
  width: 220px;
}
#newRowForm label {
  display: inline-block;
  width: 80px;
  text-align: right;
  margin-right: 10px;
}
#newRowForm  input {
  width: 100px;
}
#addRow:hover {
  cursor: pointer;
  background-color: #333;
  color: #fff
}
.list-item__button--delete {
  background-color: red;
  color: #fff !important;
}
.list-item__button--delete:hover {
  background-color: #fff;
  color: red !important;
}
#hidden-input {
  height: 0px !important;
  width: 0px !important;
  padding: 0px !important;
  border: solid 1px transparent !important;
}
              
            
!

JS

              
                /** @jsx h */
const { h, Component } = composi



const fruitData = [
  {
    product: 'Apple',
    price: 1.50,
    quantity: 5
  },
  {
    product: 'Orange',
    price: 1.00,
    quantity: 7
  },
  {
    product: 'Banana',
    price: .90,
    quantity: 3
  }, {
    product: 'Peach',
    price: 1.10,
    quantity: 0
  }
]


function sumUp(rows) {
  let total = 0
  rows.forEach((row) => {
    total += row.price * row.quantity
  })
  return total
}


function TableHeader() {
  return (
    <tr>
      <th>Product</th>
      <th>Price</th>
      <th>Quantity</th>
      <th>Total</th>
      <th></th>
    </tr>
  )
}

function TableRow({row, idx}) {
  return (
    <tr>
      <td>{row.product}</td>
      <td>
        <input class='price' data-index={String(idx)} type='number' min='0' value={row.price.toFixed(2)}/>
      </td>
      <td>
        <input class='quantity' data-index={String(idx)} type='number' min='0' value={row.quantity}/>
      </td>
      <td>
        <span class='total'>${(row.price * row.quantity).toFixed(2)}</span>
      </td>
      <td>
        <button data-index={idx} class='list-item__button--delete'>X</button>
      </td>
    </tr>
  )
}

function TableFooter({rows}) {
  return (
    <tfoot>
      <tr>
        <td colspan='3'>Sum:</td>
        <td colspan='2'>
          <span class='total'>${sumUp(rows).toFixed(2)}</span>
        </td>
      </tr>
    </tfoot>
  )
}

function NewRowForm() {
  return (
    <div id='newRowForm'>
      <p>
        <label for="product">Product: </label>
        <input name='product' id='product' type="text" tabindex='1' />
      </p>
      <p>
        <label for="price">Price: </label>
        <input name='price' id='price' type="text" tabindex='2' />
      </p>
      <p>
        <label for="quantity">Quantity: </label>
        <input name='quantity' id='quantity' type="text" tabindex='3' />
      </p>
      <p>
        <button id='addRow'>Add Row</button>
      </p>
    </div>
  )
}

function SpreadSheet({rows}) {
  return (
    <li class='list--spreadsheet__item'>
      <table id='spreadsheet'>
        <TableHeader/>
        {
          rows.map((row, idx) => (
            <TableRow {...{row, idx}}/>
          ))
        }
        <TableFooter {...{rows}}/>
      </table>
    </li>
  )
}

function AddNewRow() {
  return (
    <li class='list--spreadsheet__item'>
      <NewRowForm/>
    </li>
  )
}

class SpreadSheetComponent extends Component{
  constructor(props) {
    super(props)
    this.container = 'section'
    this.state = fruitData
  }

  render(rows) {
    return (
      <ul class='list--spreadsheet'>
        <SpreadSheet {...{rows}}/>
        <AddNewRow/>
      </ul>
    )
  }

  addNewRow(e) {
    const productInput = this.element.querySelector('#product')
    const priceInput = this.element.querySelector('#price')
    const quantityInput = this.element.querySelector('#quantity')
    const product = productInput.value
    const price = Number(priceInput.value) || 0
    const quantity = Number(quantityInput.value) || 0
    productInput.value = ''
    priceInput.value = ''
    quantityInput.value = ''
    if (product) {
      this.setState(prevState => prevState.push({product, price, quantity}))
    } else {
      alert('Please provide a product name before trying to add a row.')
    }
  }

  deleteRow(e) {
    const index = e.target.dataset.index
    this.setState(prevState => prevState.splice(index, 1))
  }

  updateQuantity(e) {
    const index = e.target.dataset.index
    const value = Number(e.target.value)
    this.setState(prevState => prevState[index].quantity = value)
  }

  updatePrice(e) {
    const index = e.target.dataset.index
    const value = Number(e.target.value)
    this.setState(prevState => prevState[index].price = value)
  }

  handleEvent(e) {
    e.target.id === 'addRow' && this.addNewRow(e)
    e.target.className === 'list-item__button--delete' && this.deleteRow(e)
    e.target.className === 'quantity' && this.updateQuantity(e)
    e.target.className === 'price' && this.updatePrice(e)
  }

  componentDidMount() {
    this.element.addEventListener('click', this)
    this.element.addEventListener('input', this)
  }
}
const spreadsheet = new SpreadSheetComponent()
              
            
!
999px

Console