Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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

              
                <div class="container"></div>
              
            
!

CSS

              
                .app-background {
  background-color: #bee3ff;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  font-size: 30px;
}

.main-container {
  background-color: #ec645b;
  width: 500px;
  height: min-content;
  border-radius: 5px;
  padding: 20px;
  box-shadow: 10px 10px 26px 0px rgba(0, 0, 0, 0.35);
}

.title {
  text-align: center;
}

.add-item-box {
  background: #fbc1bb;
  color: #ec645b;
  color: black;
  margin-top: 1em;
  margin-bottom: 1em;
  display: flex;
  align-items: center;
  border-radius: 10px;
  padding: 5px;
}

.add-item-input {
  box-sizing: border-box;
  border: none;
  background: transparent;
  color: #ec645b;
  color: black;
  /* width: 100%; */
  width: 450px;
  height: 30px;
}
.submitButton {
  text-align: right;
}
input::placeholder {
  color: #ec645b;
  color: grey;
}
input:focus {
  outline: 0;
}

.item-list {
  display: flex;
  flex-direction: column;
}

.item-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 0 20px 0;
  border-bottom: 1px #fbc1bb solid;
}

.item-name {
  cursor: pointer;
}

.item-name svg {
  margin-left: 5px;
  margin-right: 5px;
}

.item-action-buttons {
  /* width: 30%; */
  display: flex;
  justify-content: space-between;
}

.remove-button {
  color: white;
  background-color: red;
}

.total {
  float: right;
  padding: 10px;
}

.quantity {
  display: flex;
  align-items: center;
  border: 1px solid #ec645b;
  background: white;
  border-radius: 50px;
  font-size: 12px;
  color: #ec645b;
  min-width: 70px;
  justify-content: space-between;
}

button {
  background: transparent;
  border: none;
  margin: 3px;
  color: #ec645b;
}
button:focus {
  outline: 0;
}

button:hover {
  cursor: pointer;
}

.completed {
  text-decoration: line-through;
}

              
            
!

JS

              
                const { useState, useEffect } = React;

const App = () => {
  const [inputValue, setInputValue] = useState("");
  const [items, setItems] = useState<Item[]>([]);
  const [totalItemCount, setTotalItemCount] = useState(0);

  // id is not in the type. Is it Ok?
  type Item = {
    itemName: string;
    quantity: number;
    isSelected: boolean;
  };

  const calculateTotal = () => {
    const totalItemCount = items.reduce((total, item) => {
      return total + item.quantity;
    }, 0);

    setTotalItemCount(totalItemCount);
  };

  useEffect(() => {
    calculateTotal();
  });

  const handleAddButtonClick = (e: any) => {
    e.preventDefault();
    const newItem = {
      itemName: inputValue,
      quantity: 1,
      isSelected: false
    };

    const newItems = [...items, newItem];

    setItems(newItems);
    calculateTotal();

    setInputValue("");
  };

  //done切り替え
  // いまここ
  const toggleComplete = (index: number) => {
    //itemsを展開した配列、newItemsを作る
    const newItems = [...items];
    //引数にindexから、該当するitemのisSelectedを切り替える
    newItems[index].isSelected = !newItems[index].isSelected;

    setItems(newItems);
  };

  const handleChecked = (index: number, checked: boolean) => {
    const newItems = [...items];
    if (checked) {
      newItems[index].isSelected = true;
    } else {
      newItems[index].isSelected = false;
    }

    setItems(newItems);
  };

  const handleQuantityDecrease = (index: number) => {
    //itemsを展開した配列、newItemsを作る
    const newItems = [...items];
    //quantityに-1する
    newItems[index].quantity--;
    setItems(newItems);
    //総量更新
    calculateTotal();
  };

  const handleQuantityIncrease = (index: number) => {
    //itemsを展開した配列、newItemsを作る
    const newItems = [...items];
    //quantityに+1する
    newItems[index].quantity++;
    setItems(newItems);
    //総量更新
    calculateTotal();
  };

  return (
    <div className="app-background">
      <div className="main-container">
        <h4>買い物リスト 数量入り</h4>
        <div className="add-item-box">
          <form onSubmit={(e) => handleAddButtonClick(e)}>
            <input
              value={inputValue}
              onChange={(event) => setInputValue(event.target.value)}
              autoFocus
              className="add-item-input"
              placeholder="追加する品目を入力して下さい"
            />
            <input type="submit" value="+" className="submitButton" />
          </form>
        </div>
        <div className="item-list">
          {items.map((item, index) => (
            <div className="item-container">
              {/* toggleCompleteにindexを渡す事で、そのitemのisSelected切り替える。 */}
              <div className="item-name" onClick={() => toggleComplete(index)}>
                <input
                  type="checkbox"
                  checked={items[index].isSelected}
                  onChange={(e) => handleChecked(index, e.target.checked)}
                />
                {item.isSelected ? (
                  <span className="completed">{item.itemName}</span>
                ) : (
                  <span>{item.itemName}</span>
                )}
              </div>
              <div className="quantity">
                <button onClick={() => handleQuantityDecrease(index)}>-</button>
                <span> {item.quantity} </span>
                <button onClick={() => handleQuantityIncrease(index)}>+</button>
              </div>
            </div>
          ))}
        </div>
        <div className="total">Total:{totalItemCount}</div>
      </div>
    </div>
  );
};

ReactDOM.render(<App />, document.querySelector(".container"));

              
            
!
999px

Console