タグ入力
入力したテキストを値に変え、各値を入力枠内のチップとして表示するフィールド。
このページの内容
タグ入力は、入力したテキストを値に変え、各値を入力枠内のチップとして表示します。input-group-wrap と tags クラスを指定した入力グループで構成します。入力枠とその状態、枠と同心の角を持つチップは入力グループが描画し、チップの後ろにあるフィールドが次の値を受け付けます。tags のビヘイビアが、テキストをチップに変換し、チップ間でフォーカスを移動します。
タグ入力
<div class="field max-w(420) mx-auto"> <label for="labels">Labels</label> <div class="tags input-group input-group-wrap"> <span class="chip" data-value="design"> design<input type="hidden" name="labels" value="design"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove design"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </span> <span class="chip" data-value="research"> research<input type="hidden" name="labels" value="research"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove research"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </span> <span class="chip" data-value="accessibility"> accessibility<input type="hidden" name="labels" value="accessibility"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove accessibility"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </span> <input class="input-group-field" id="labels" placeholder="Add a label" autocomplete="off" aria-describedby="labels-hint"> </div> <p class="field-hint" id="labels-hint">Press Enter or type a comma after each label.</p></div><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import XIcon from '@master/icons-react/native/16/soft/x'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root} className="field max-w(420) mx-auto"> <label htmlFor="labels">Labels</label> <div className="tags input-group input-group-wrap"> <span className="chip" data-value="design"> design<input type="hidden" name="labels" value="design" /><button type="button" className="chip-remove" tabIndex={-1} aria-label="Remove design"> <XIcon className="icon" /> </button> </span> <span className="chip" data-value="research"> research<input type="hidden" name="labels" value="research" /><button type="button" className="chip-remove" tabIndex={-1} aria-label="Remove research"> <XIcon className="icon" /> </button> </span> <span className="chip" data-value="accessibility"> accessibility<input type="hidden" name="labels" value="accessibility" /><button type="button" className="chip-remove" tabIndex={-1} aria-label="Remove accessibility"> <XIcon className="icon" /> </button> </span> <input className="input-group-field" id="labels" placeholder="Add a label" autoComplete="off" aria-describedby="labels-hint" /> </div> <p className="field-hint" id="labels-hint">Press Enter or type a comma after each label.</p> </div> )}<script> import { attach } from '@master/ui/behaviors' import XIcon from '@master/icons-svelte/native/16/soft/x'</script><div class="field max-w(420) mx-auto" {@attach attach}> <label for="labels">Labels</label> <div class="tags input-group input-group-wrap"> <span class="chip" data-value="design"> design<input type="hidden" name="labels" value="design"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove design"> <XIcon class="icon" /> </button> </span> <span class="chip" data-value="research"> research<input type="hidden" name="labels" value="research"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove research"> <XIcon class="icon" /> </button> </span> <span class="chip" data-value="accessibility"> accessibility<input type="hidden" name="labels" value="accessibility"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove accessibility"> <XIcon class="icon" /> </button> </span> <input class="input-group-field" id="labels" placeholder="Add a label" autocomplete="off" aria-describedby="labels-hint"> </div> <p class="field-hint" id="labels-hint">Press Enter or type a comma after each label.</p></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import XIcon from '@master/icons-vue/native/16/soft/x'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root" class="field max-w(420) mx-auto"> <label for="labels">Labels</label> <div class="tags input-group input-group-wrap"> <span class="chip" data-value="design"> design<input type="hidden" name="labels" value="design"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove design"> <XIcon class="icon" /> </button> </span> <span class="chip" data-value="research"> research<input type="hidden" name="labels" value="research"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove research"> <XIcon class="icon" /> </button> </span> <span class="chip" data-value="accessibility"> accessibility<input type="hidden" name="labels" value="accessibility"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove accessibility"> <XIcon class="icon" /> </button> </span> <input class="input-group-field" id="labels" placeholder="Add a label" autocomplete="off" aria-describedby="labels-hint"> </div> <p class="field-hint" id="labels-hint">Press Enter or type a comma after each label.</p> </div></template>#入力グループに加わる機能
- 値の追加 新しいチップは、いきなり表示されるのではなく、キャレットのあった位置から quick の再生時間で 80% から短く拡大して現れます。動きを抑える設定では、フェードインだけになります。
- 単語 1 つ分の幅 フィールドは少なくとも
--tags-field-min(既定は 6rem)の幅を保ち、それより狭くなる場合は下の行に単独で移ります。そのため、行末の細い隙間にキャレットが押し込まれることはありません。 - キー操作の対象になっている値 チップ全体にフォーカスリングが表示されます。その位置で Backspace を押すと値全体が削除されるためです。削除ボタンの×印は押下状態の色調になります。このリングはチップ自体のもので、
chip-removeを持つチップであれば、どこに置かれていても表示されます。 - 受け付けられなかった値 フィールドに
aria-invalid="true"が設定され、テキストが変わるまで、入力グループが危険を示す色の縁を表示します。
@starting-style に対応していないブラウザーでは、新しい値はすぐに表示されます。
#ビヘイビア
tags は、ルート要素(tags クラスを持つ入力グループ)を受け取り、ビヘイビアを解除する関数を返します。
import { tags } from '@master/ui/behaviors/tags.js'const detach = tags(document.querySelector('.tags'), { max: 10 })- 追加 Enter を押すか、区切り文字(既定はカンマ)を入力すると、フィールドのテキストが値になります。フィールドの前にチップが書き込まれ、フィールドは空になります。区切り文字、改行、タブを含むテキストを貼り付けると、それらで区切られた部分がそれぞれ値になり、最後の区切りより後ろのテキストはフィールドに残ります。
- 拒否 空の値、すでにある値、追加すると値の個数が
maxを超える値、validateが拒否した値は追加されません。入力したテキストが失われないようにフィールドはテキストを保持し、テキストが変わるまでaria-invalid="true"を示します。 - 削除 チップの削除ボタンをクリックするとそのチップが削除され、フォーカスはフィールドに戻ります。
- フォーム 既存のチップが
name付きの hidden input を持っている場合、またはnameを指定した場合、各チップは値を持つ hidden input を含みます。そのため、フォームはすべての値をその名前で送信します。フォームをリセットすると、フィールドに最初にあった値に戻ります。 - イベント 値を追加または削除するたびに、ルート要素が
inputとchangeを発行します。返される関数のvalueは、値を順番どおりに並べたものです。valueに代入すると値が置き換わりますが、イベントは発行されません。
| オプション | 既定値 | |
|---|---|---|
delimiters | [','] | Enter と同様に値を追加するキー。[] にすると Enter だけになります |
max | なし | フィールドに入れられる値の最大数 |
validate | なし | 値を追加してよいかどうかを判定する関数 |
name | 既存のチップが持つ hidden input の名前 | 新しいチップが持つ hidden input の名前 |
removeLabel | Remove <value> | チップの削除ボタンのアクセシブルネーム。ページの言語で指定します |
draw | なし | チップの書き込みを肩代わりします。独自のリストからチップを描画するレンダラー向けです |
チップは data-value、なければテキストを値とします。ビヘイビアを適用しない場合、フィールドは通常のテキスト入力で、チップは固定されます。その場合も、チップが持つ hidden input はフォームで送信されます。
#入力候補
<datalist> を使うと、専用のスクリプトなしでブラウザー標準の入力候補をフィールドに表示できます。候補を選んでから Enter を押すと追加されます。値をリストから選ぶ必要がある場合は、代わりに複数の値を選べるコンボボックスを使います。
入力候補
<div class="field max-w(420) mx-auto"> <label for="recipients">Recipients</label> <div class="tags input-group input-group-wrap"> <span class="chip" data-value="ada@example.com"> ada@example.com<input type="hidden" name="to" value="ada@example.com"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove ada@example.com"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </span> <input class="input-group-field" id="recipients" type="email" list="recipient-suggestions" placeholder="Add a recipient" autocomplete="off"> </div> <datalist id="recipient-suggestions"> <option value="grace@example.com"></option> <option value="linus@example.com"></option> <option value="margaret@example.com"></option> </datalist></div><script type="module"> import { attach } from '@master/ui/behaviors' attach()</script>import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import XIcon from '@master/icons-react/native/16/soft/x'export function Example() { const root = useRef<HTMLDivElement>(null) useEffect(() => attach(root.current!), []) return ( <div ref={root} className="field max-w(420) mx-auto"> <label htmlFor="recipients">Recipients</label> <div className="tags input-group input-group-wrap"> <span className="chip" data-value="ada@example.com"> ada@example.com<input type="hidden" name="to" value="ada@example.com" /><button type="button" className="chip-remove" tabIndex={-1} aria-label="Remove ada@example.com"> <XIcon className="icon" /> </button> </span> <input className="input-group-field" id="recipients" type="email" list="recipient-suggestions" placeholder="Add a recipient" autoComplete="off" /> </div> <datalist id="recipient-suggestions"> <option value="grace@example.com"></option> <option value="linus@example.com"></option> <option value="margaret@example.com"></option> </datalist> </div> )}<script> import { attach } from '@master/ui/behaviors' import XIcon from '@master/icons-svelte/native/16/soft/x'</script><div class="field max-w(420) mx-auto" {@attach attach}> <label for="recipients">Recipients</label> <div class="tags input-group input-group-wrap"> <span class="chip" data-value="ada@example.com"> ada@example.com<input type="hidden" name="to" value="ada@example.com"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove ada@example.com"> <XIcon class="icon" /> </button> </span> <input class="input-group-field" id="recipients" type="email" list="recipient-suggestions" placeholder="Add a recipient" autocomplete="off"> </div> <datalist id="recipient-suggestions"> <option value="grace@example.com"></option> <option value="linus@example.com"></option> <option value="margaret@example.com"></option> </datalist></div><script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import XIcon from '@master/icons-vue/native/16/soft/x'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template> <div ref="root" class="field max-w(420) mx-auto"> <label for="recipients">Recipients</label> <div class="tags input-group input-group-wrap"> <span class="chip" data-value="ada@example.com"> ada@example.com<input type="hidden" name="to" value="ada@example.com"><button type="button" class="chip-remove" tabindex="-1" aria-label="Remove ada@example.com"> <XIcon class="icon" /> </button> </span> <input class="input-group-field" id="recipients" type="email" list="recipient-suggestions" placeholder="Add a recipient" autocomplete="off"> </div> <datalist id="recipient-suggestions"> <option value="grace@example.com"></option> <option value="linus@example.com"></option> <option value="margaret@example.com"></option> </datalist> </div></template>#キーボード操作
Tab キーで止まるのはフィールドだけです。チップへはフィールドから移動し、チップの削除ボタンは tabindex="-1" で Tab キーの移動順序から外します。入力メソッドで文字が未確定の間は、Enter を入力メソッドが処理します。
| キー | 操作 |
|---|---|
| Enter, Comma | フィールドのテキストを値に変えます。空のフィールドでの Enter はフォームの処理に任せます。 |
| Backspace | 空のフィールドでは最後の値に移動します。値の上では、その値を削除して前の値に移動します。 |
| Delete | 値の上では、その値を削除して次の値に移動します。 |
| ArrowLeft, ArrowRight | フィールドの先頭からは最後の値に移動します。値の間では前または次の値に移動し、最後の値を越えるとフィールドに戻ります。右から左に並ぶ場合は方向が逆になります。 |
| Home, End | 値の上では、最初の値に移動するか、フィールドに戻ります。 |
| Escape | 値の上では、フィールドに戻ります。 |
#アクセシビリティ
<label>またはaria-labelでフィールドにラベルを付けます。カンマや Enter で値を追加できることはわかりにくいため、フィールドの下のヒントで値の追加方法を説明します。- 各削除ボタンには、削除する値を含む名前をページの言語で付けます。ビヘイビアが書き込むチップには、
removeLabelがこの名前を付けます。 - 受け付けられなかった値はフィールドにテキストのまま残り、フィールドは無効な入力として示されます。ほかのフィールドと同様に、その理由をフィールドのヒントまたはエラーで説明します。
#React、Svelte、Vue
tags コンポーネントは、値からチップを描画し、ビヘイビアを適用します。入力したテキストはビヘイビアが値に変換します。npx master-ui add tags でプロジェクトにコピーできます。コンポーネントの構成と使い方は React、Svelte、Vue を参照してください。
#クラス
| クラス | 説明 |
|---|---|
tags | input-group-wrap を指定した入力グループに適用します。チップが値を表し、フィールドが次の値を受け付けます。フィールドは単語 1 つ分の幅を確保し、それより狭くなると次の行に単独で移ります。 |
input-group、input-group-wrap、input-group-field、および値ごとに chip-remove を持つ chip と組み合わせて使います。
#カスタムプロパティ
| プロパティ | 既定値 | 説明 |
|---|---|---|
--tags-field-min | 6rem | フィールドが次の行に単独で移る前に許容される最小の幅 |
#関連コンポーネント
- 入力フィールド背景面より一段くぼんだテキスト入力フィールド。ラベル、ヒント、エラー、グループに対応します。
- チップフィルターや削除可能な値を表す、小さなカプセル形の要素。
- コンボボックス入力に一致する候補をリストボックスに表示するテキストフィールド。文字を入力できるセレクトです。