英文:
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>
);
标签状态变量输出示例:
英文:
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 'react';
import 'bootstrap/dist/css/bootstrap.min.css'; // Bootstrap styles
function AddNote() {
const [tags, setTags] = useState([]);
// Rest of the component code
return (
<form>
{/* Other form fields */}
<input type="text" value={tags} onChange={handleTagsChange} />
{/* Other form elements */}
</form>
);
}
Implement the handleTagsChange
function to update the tags state whenever the input field value changes.
function handleTagsChange(event) {
const newTags = event.target.value.split(','); // 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 (
<div>
{/* Other note content */}
<div>
{tags.map((tag, index) => (
<span key={index} className="badge bg-primary">{tag}</span>
))}
</div>
</div>
);
tags state variable output example:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论