メインコンテンツまでスキップ
バージョン: v2

Forms

Basic forms​

基本的な形式のコンポーネントの例を次に示します。

@Component({
tag: 'my-name',
styleUrl: 'my-name.css'
})
export class MyName {

@State() value: string;

handleSubmit(e) {
e.preventDefault()
console.log(this.value);
// send data to our backend
}

handleChange(event) {
this.value = event.target.value;
}

render() {
return (
<form onSubmit={(e) => this.handleSubmit(e)}>
<label>
Name:
<input type="text" value={this.value} onInput={(event) => this.handleChange(event)} />
</label>
<input type="submit" value="Submit" />
</form>
);
}
}

ここで何が起こっているのかを見てみましょう。 まず、入力の値を状態変数(この場合は this.value)にバインドします。 次に、 onInputにバインドしたhandleChangeメソッドを使用して、状態変数を入力の新しい値に設定します。 onInputは、ユーザーが入力に入力するすべてのキーストロークを起動します。

Advanced forms​

より高度な形式のコンポーネントの例を次に示します。

@Component({
tag: 'my-name',
styleUrl: 'my-name.css'
})
export class MyName {
selectedReceiverIds = [102, 103];
@State() value: string;
@State() selectValue: string;
@State() secondSelectValue: string;
@State() avOptions: any[] = [
{ 'id': 101, 'name': 'Mark' },
{ 'id': 102, 'name': 'Smith' }
];

handleSubmit(e) {
e.preventDefault();
console.log(this.value);
}

handleChange(event) {
this.value = event.target.value;

if (event.target.validity.typeMismatch) {
console.log('this element is not valid')
}
}

handleSelect(event) {
console.log(event.target.value);
this.selectValue = event.target.value;
}

handleSecondSelect(event) {
console.log(event.target.value);
this.secondSelectValue = event.target.value;
}

render() {
return (
<form onSubmit={(e) => this.handleSubmit(e)}>
<label>
Email:
<input type="email" value={this.value} onInput={(e) => this.handleChange(e)} />
</label>

<select onInput={(event) => this.handleSelect(event)}>
<option value="volvo" selected={this.selectValue === 'volvo'}>Volvo</option>
<option value="saab" selected={this.selectValue === 'saab'}>Saab</option>
<option value="mercedes" selected={this.selectValue === 'mercedes'}>Mercedes</option>
<option value="audi" selected={this.selectValue === 'audi'}>Audi</option>
</select>

<select onInput={(event) => this.handleSecondSelect(event)}>
{this.avOptions.map(recipient => (
<option value={recipient.id} selected={this.selectedReceiverIds.indexOf(recipient.id) !== -1}>{recipient.name}</option>
))}
</select>

<input type="submit" value="Submit" />
</form>
);
}
}

This form is a little more advanced in that it has two select inputs along with an email input. We also do validity checking of our email input in the handleChange method. We handle the select element in a very similar manner to how we handle text inputs.

For the validity checking, we are #usingtheplatform and are using the constraint validation api that is built right into the browser to check if the user is actually entering an email or not.