如何添加Bootstrap标签输入

huangapple go评论55阅读模式
英文:

How to Add Bootstrap Tags Input

问题

这是你的代码,你想要在表单的第三个 div 内放置标签输入:

// This is the place where i want the tags input

如果你需要进一步的帮助来实现标签输入,请提出具体问题。

英文:

Iam cuurently working on a notes application and i want the user to provide tags for their
note but i am not sure how i can do it , please help;
and also one of the css is effecting all my bootstrap css, And it would be great i dont have to use Jquery
Pls Help

This is my code;

i want the tag input in the 3rd div inside the form

import React from "react";
import { useContext } from "react";
import { useState } from "react";
import { useEffect } from "react";
import noteContext from "../context/notes/noteContext";
// import $ from 'jquery'
import "../addNote.css";
import Tags from "./Tags";
import { Helmet } from "react-helmet";

const AddNote = () => {
  useEffect(() => {
    const script = document.createElement("Script");

    script.src = "../script.js";

    document.body.appendChild(script);

    return () => {
      document.body.removeChild(script);
    };
  });

  const context = useContext(noteContext);
  const { addNote } = context;

  const [note, setNote] = useState({
    title: "",
    description: "",
    msg: "",
    tag: "",
  });

  const handleClick = (e) => {
    e.preventDefault();
    addNote(note.title, note.description, note.msg, note.tag);
  };

  const onChange = (e) => {
    setNote({
      ...note,
      [e.target.name]: e.target.value === "" ? "----" : e.target.value,
    });
  };

  return (
    <>
      <div className="container my-3">
        <h2>Add a Note</h2>
        <form id="form-id" className="my-3">
          <div className="mb-3">
            <label htmlFor="title" className="form-label">
              Title
            </label>
            <input
              type="text"
              className="form-control"
              id="title"
              name="title"
              aria-describedby="emailHelp"
              onChange={onChange}
            />
          </div>
          <div className="mb-3">
            <label htmlFor="description" className="form-label">
              Description
            </label>
            <input
              type="text"
              className="form-control"
              id="description"
              name="description"
              onChange={onChange}
            />
          </div>
          <div className="mb-3">
            <label htmlFor="msg" className="form-label">
              Note Message
            </label>
            <input
              type="text"
              className="form-control"
              id="msg"
              name="msg"
              onChange={onChange}
            />
          </div>
          <div className="mb-3">
            **// This is the place where i want the tags input**
          </div>
          <div className="mb-3 form-check">
            <input
              type="checkbox"
              className="form-check-input"
              id="exampleCheck1"
            />
            <label className="form-check-label" htmlFor="exampleCheck1">
              Check me out
            </label>
          </div>
          <button
            type="submit"
            className="btn btn-primary"
            onClick={handleClick}
          >
            Add Note
          </button>
        </form>
      </div>
      <script src="../script.js"></script>
    </>
  );
};

export default AddNote;

答案1

得分: 0

为了让用户在您的应用中为笔记提供标签而不使用jQuery,您可以利用React的状态管理来处理标签输入和存储。

在您的笔记创建表单/组件中,添加一个输入字段,让用户输入标签。您可以使用HTML输入元素或React UI库组件以获得更好的样式和功能。

在您的组件中创建一个状态变量来存储用户输入的标签。您可以使用React的useState钩子来管理状态。将状态初始化为空数组。

import React, { useState } from 'react';
import 'bootstrap/dist/css/bootstrap.min.css'; // Bootstrap样式

function AddNote() {
  const [tags, setTags] = useState([]);

  // 组件的其余代码

  return (
    <form>
      {/* 其他表单字段 */}
      <input type="text" value={tags} onChange={handleTagsChange} />
      {/* 其他表单元素 */}
    </form>
  );
}

实现handleTagsChange函数以在输入字段值更改时更新标签状态。

function handleTagsChange(event) {
  const newTags = event.target.value.split(','); // 假设标签是逗号分隔的
  setTags(newTags);
}

要显示输入的标签,您可以遍历tags数组并将各个标签呈现为单独的元素。您可以根据应用程序的设计进行样式设置。

return (
  <div>
    {/* 其他笔记内容 */}
    <div>
      {tags.map((tag, index) => (
        <span key={index} className="badge bg-primary">{tag}</span>
      ))}
    </div>
  </div>
);

标签状态变量输出示例:

如何添加Bootstrap标签输入

英文:

To allow users to provide tags for their notes in your application without using jQuery, you can utilize React's state management to handle the tag input and storage.

In your note creation form/component, add an input field where users can enter tags. You can use an HTML input element or a React UI library component for better styling and functionality.

Create a state variable in your component to store the tags entered by the user. You can use the useState hook from React to manage the state. Initialize the state with an empty array.

import React, { useState } from &#39;react&#39;;
import &#39;bootstrap/dist/css/bootstrap.min.css&#39;; // Bootstrap styles   

function AddNote() {
  const [tags, setTags] = useState([]);

  // Rest of the component code

  return (
    &lt;form&gt;
      {/* Other form fields */}
      &lt;input type=&quot;text&quot; value={tags} onChange={handleTagsChange} /&gt;
      {/* Other form elements */}
    &lt;/form&gt;
  );
}

Implement the handleTagsChange function to update the tags state whenever the input field value changes.

function handleTagsChange(event) {
  const newTags = event.target.value.split(&#39;,&#39;); // Assuming tags are comma-separated
  setTags(newTags);
}

To display the entered tags, you can map over the tags array and render individual tags as separate elements. You can style them as per your application's design.

return (
  &lt;div&gt;
    {/* Other note content */}
    &lt;div&gt;
      {tags.map((tag, index) =&gt; (
        &lt;span key={index} className=&quot;badge bg-primary&quot;&gt;{tag}&lt;/span&gt;
      ))}
    &lt;/div&gt;
  &lt;/div&gt;
);

tags state variable output example:

如何添加Bootstrap标签输入

huangapple
  • 本文由 发表于 2023年5月17日 22:53:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定