テキストコントロールが提供する入力を制御する機能について解説します。
最大文字数の指定
setLengthAsByteメソッドを使えば、setMaxLengthメソッドでコントロールへの入力可能文字数を設定する際に、バイト単位と文字単位のどちらを使うかを指定できます。setLengthAsByteメソッドをtrueに設定すると、setMaxLengthメソッドはバイト単位で処理され、falseに設定すると文字単位で処理されます。
改行コードの取り扱い
setAcceptsCrlfメソッドを使用してクリップボードへ改行を含む文字列をコピー、または貼り付けた場合の改行コードの扱いを設定できます。setAcceptsCrlfメソッドは、以下の3つの動作から選択します。既定値はCrLfMode.NoControlです。
値
説明
NoControl
改行コードはそのままでコピー、貼り付けを行います。
Filter
コピー、切り取り、または貼り付け文字列内にあるすべての改行コードを削除します。
Cut
コピー、切り取り、および貼り付け文字列内にある最初の改行コード以降の文字列を切り取ります。
import { bootstrapApplication } from '@angular/platform-browser';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { InputManModule } from "@mescius/inputman.angular";
import '@mescius/inputman/CSS/gc.inputman-js.css';
import { Component, enableProdMode } from '@angular/core';
import * as GC from "@mescius/inputman";
@Component({
selector: 'app-component',
templateUrl: './src/app.component.html',
imports: [InputManModule, BrowserModule, FormsModule],
standalone: true
})
export class AppComponent {
ngOnInit(): void {
GC.InputMan.appearanceStyle = GC.InputMan.AppearanceStyle.Modern;
}
public maxLength: number = 5;
public lengthAsByte: boolean;
public acceptsCrlf: GC.InputMan.CrLfMode;
public crLfModes = [
GC.InputMan.CrLfMode.NoControl,
GC.InputMan.CrLfMode.Filter,
GC.InputMan.CrLfMode.Cut
];
}
enableProdMode();
bootstrapApplication(AppComponent).catch(err => console.error(err));
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>テキストコントロール - 入力の制御</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Polyfills -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/fesm2015/zone.min.js"></script>
<!-- SystemJS -->
<script src="node_modules/systemjs/dist/system.js"></script>
<script src="systemjs.config.js"></script>
<script>
// workaround to load 'rxjs/operators' from the rxjs bundle
window.onload = function() {
System.import('rxjs').then(function (m) {
System.import('@angular/compiler');
System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators));
System.import('./src/app.component');
});
}
</script>
</head>
<body>
<app-component></app-component>
</body>
</html>
<gc-text-box [maxLength]="maxLength" [lengthAsByte]="lengthAsByte" [acceptsCrlf]="acceptsCrlf"></gc-text-box>
<table class="sample">
<tr>
<th>最大文字数</th>
<td>
<input type="number" value="5" min="0" id="setMaxLength" (change)="maxLength=$event.target.value">
<label><input type="radio" name="setLengthAsByte" (change)="lengthAsByte=false" id="setLengthNotAsByte"
checked>文字単位</label>
<label><input type="radio" name="setLengthAsByte" (change)="lengthAsByte=true"
id="setLengthAsByte">バイト単位</label>
</td>
</tr>
<tr>
<th>改行コードの扱い</th>
<td>
<select id="setAcceptsCrlf" (change)="acceptsCrlf=crLfModes[$event.target.selectedIndex]">
<option>そのまま使用</option>
<option>すべての改行コードを削除</option>
<option>改行コード以降の文字列を切り取り</option>
</select>
</td>
</tr>
</table>
(function (global) {
System.config({
transpiler: 'ts',
typescriptOptions: {
tsconfig: true
},
meta: {
'typescript': {
"exports": "ts"
},
'*.css': { loader: 'css' }
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
'core-js': 'npm:core-js/client/shim.min.js',
'zone': 'npm:zone.js/fesm2015/zone.min.js',
//'reflect-metadata': 'npm:reflect-metadata/Reflect.js',
'rxjs': 'npm:rxjs/dist/bundles/rxjs.umd.min.js',
//'rxjs': 'npm:rxjs',
//'rxjs/operators': 'npm:rxjs/operators',
'@angular/core': 'npm:@angular/core/fesm2022',
'@angular/common': 'npm:@angular/common/fesm2022/common.mjs',
'@angular/compiler': 'npm:@angular/compiler/fesm2022/compiler.mjs',
'@angular/platform-browser': 'npm:@angular/platform-browser/fesm2022/platform-browser.mjs',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/fesm2022/platform-browser-dynamic.mjs',
'@angular/common/http': 'npm:@angular/common/fesm2022/http.mjs',
'@angular/router': 'npm:@angular/router/fesm2022/router.mjs',
'@angular/forms': 'npm:@angular/forms/fesm2022/forms.mjs',
'@mescius/inputman': 'npm:@mescius/inputman/index.js',
'@mescius/inputman.angular': 'npm:@mescius/inputman.angular/fesm2015/mescius-inputman.angular.mjs',
'@mescius/inputman.richtexteditor': 'npm:@mescius/inputman.richtexteditor/index.js',
'@mescius/inputman.richtexteditor.angular': 'npm:@mescius/inputman.richtexteditor.angular/fesm2015/mescius-inputman.richtexteditor.angular.mjs',
'@mescius/inputman/CSS': 'npm:@mescius/inputman/CSS',
'@mescius/inputman.richtexteditor/CSS': 'npm:@mescius/inputman.richtexteditor/CSS',
'@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js',
'@mescius/spread-sheets-resources-ja': 'npm:@mescius/spread-sheets-resources-ja/index.js',
'@mescius/spread-sheets/styles': 'npm:@mescius/spread-sheets/styles',
'@mescius/wijmo': 'npm:@mescius/wijmo/index.js',
'@mescius/wijmo.styles': 'npm:@mescius/wijmo.styles',
'@mescius/wijmo.cultures': 'npm:@mescius/wijmo.cultures',
'@mescius/wijmo.nav': 'npm:@mescius/wijmo.nav/index.js',
'@mescius/wijmo.grid': 'npm:@mescius/wijmo.grid/index.js',
'@mescius/wijmo.input': 'npm:@mescius/wijmo.input/index.js',
'bootstrap.css': 'npm:bootstrap/dist/css/bootstrap.min.css',
'jszip': 'npm:jszip/dist/jszip.min.js',
'typescript': 'npm:typescript/lib/typescript.js',
'ts': './plugin.js',
'tslib': 'npm:tslib/tslib.js',
'css': 'npm:systemjs-plugin-css/css.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
src: {
defaultExtension: 'ts',
meta: {
"*.component.ts": {
loader: "system.component-loader.js"
}
}
},
rxjs: {
defaultExtension: 'js'
},
"node_modules": {
defaultExtension: 'js'
},
"@mescius/inputman": {
defaultExtension: 'js'
},
"@mescius/inputman.richtexteditor": {
defaultExtension: 'js'
},
"node_modules/@angular": {
defaultExtension: 'mjs'
},
"@mescius/inputman.angular": {
defaultExtension: 'mjs'
},
'@angular/core': {
defaultExtension: 'mjs',
main: 'core.mjs'
},
"@mescius/inputman.richtexteditor.angular": {
defaultExtension: 'mjs'
},
}
});
})(this);