File size: 4,386 Bytes
dcdc07a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
  "id": "tag-input",
  "name": "Tag Input",
  "description": "Add and remove tags with keyboard input",
  "author": "gradio",
  "tags": [
    "input",
    "tags",
    "text"
  ],
  "category": "input",
  "html_template": "<div class=\"tag-input-container\">\n    <div class=\"tag-list\">\n        ${(value || []).map((tag, i) => `\n            <span class=\"tag-pill\">\n                ${tag}\n                <button class=\"tag-remove\" data-index=\"${i}\">&times;</button>\n            </span>\n        `).join('')}\n        <input type=\"text\" class=\"tag-text-input\" placeholder=\"${(value || []).length === 0 ? 'Type a tag and press Enter...' : 'Add more...'}\" />\n    </div>\n</div>",
  "css_template": ".tag-input-container { padding: 4px 0; }\n.tag-list { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; padding: 8px; border: 1px solid #e5e7eb; border-radius: 8px; min-height: 42px; }\n.tag-pill { display: inline-flex; align-items: center; gap: 4px; padding: 4px 10px; background: #fff7ed; color: #ea580c; border-radius: 16px; font-size: 13px; font-weight: 500; border: 1px solid #fed7aa; }\n.tag-remove { background: none; border: none; color: #ea580c; cursor: pointer; font-size: 16px; padding: 0 2px; line-height: 1; opacity: 0.6; }\n.tag-remove:hover { opacity: 1; }\n.tag-text-input { border: none; outline: none; flex: 1; min-width: 120px; font-size: 14px; padding: 4px; background: transparent; }",
  "js_on_load": "const input = element.querySelector('.tag-text-input');\ninput.addEventListener('keydown', (e) => {\n    if (e.key === 'Enter' && input.value.trim()) {\n        e.preventDefault();\n        const tags = [...(props.value || []), input.value.trim()];\n        props.value = tags;\n        input.value = '';\n    }\n    if (e.key === 'Backspace' && !input.value && props.value && props.value.length > 0) {\n        props.value = props.value.slice(0, -1);\n    }\n});\nelement.addEventListener('click', (e) => {\n    const btn = e.target.closest('.tag-remove');\n    if (!btn) return;\n    const idx = parseInt(btn.dataset.index);\n    const tags = [...(props.value || [])];\n    tags.splice(idx, 1);\n    props.value = tags;\n});",
  "default_props": {
    "value": [
      "python",
      "gradio",
      "html"
    ]
  },
  "python_code": "class TagInput(gr.HTML):\n    def __init__(self, value=None, **kwargs):\n        html_template = \"\"\"\n        <div class=\"tag-input-container\">\n            <div class=\"tag-list\">\n                ${(value || []).map((tag, i) => `\n                    <span class=\"tag-pill\">\n                        ${tag}\n                        <button class=\"tag-remove\" data-index=\"${i}\">&times;</button>\n                    </span>\n                `).join('')}\n                <input type=\"text\" class=\"tag-text-input\"\n                       placeholder=\"Type a tag and press Enter...\" />\n            </div>\n        </div>\n        \"\"\"\n        css_template = \"\"\"\n        .tag-list {\n            display: flex; flex-wrap: wrap; gap: 6px;\n            padding: 8px; border: 1px solid #e5e7eb;\n            border-radius: 8px;\n        }\n        .tag-pill {\n            padding: 4px 10px; background: #fff7ed;\n            color: #ea580c; border-radius: 16px;\n            font-size: 13px; border: 1px solid #fed7aa;\n        }\n        \"\"\"\n        js_on_load = \"\"\"\n        const input = element.querySelector('.tag-text-input');\n        input.addEventListener('keydown', (e) => {\n            if (e.key === 'Enter' && input.value.trim()) {\n                e.preventDefault();\n                props.value = [...(props.value || []), input.value.trim()];\n                input.value = '';\n            }\n        });\n        element.addEventListener('click', (e) => {\n            const btn = e.target.closest('.tag-remove');\n            if (!btn) return;\n            const idx = parseInt(btn.dataset.index);\n            const tags = [...(props.value || [])];\n            tags.splice(idx, 1);\n            props.value = tags;\n        });\n        \"\"\"\n        super().__init__(\n            value=value or [],\n            html_template=html_template,\n            css_template=css_template,\n            js_on_load=js_on_load, **kwargs\n        )\n\n    def api_info(self):\n        return {\"type\": \"array\", \"items\": {\"type\": \"string\"}}"
}