マスクコントロールではフォーカス制御に関する様々な機能を搭載しています。
自動フォーカス移動
setExitOnLastCharメソッドでtrueを設定すると、入力された文字列が、setFormatPatternメソッドで設定した最大文字数に達したときに、自動的に次のコントロールへフォーカスを移動できます。
矢印キーによるフォーカス移動
setExitOnLeftRightKeyメソッドを使用すると、キャレットがコントロール内のテキストの最初または最後の文字上にある状態で、[→]、[←]、または[Ctrl]+[→]、[Ctrl]+[←]の各キーを押したときに、フォーカスを前後のコントロールに移動することができます。setExitOnLeftRightKeyメソッドに設定できる値は次のとおりで、既定値はExitOnLeftRightKey.Noneです。
ExitOnLeftRightKeyの値
説明
None
矢印キーを使ってフォーカスを移動することはできません。
Left
キャレットが入力中のテキストの左端にあるときに[←]キーまたは[Ctrl]+[←]キーを押すと、フォーカスは1つ前のコントロールに移動します。
Right
キャレットが入力中のテキストの右端にあるときに[→]キーまたは[Ctrl]+[→]キーを押すと、フォーカスは次のコントロールに移動します。
Both
「Left」と「Right」の両方の機能を使用できます。
フィールド間の移動
マスクコントロールの表示領域は、通常、リテラル文字列と入力フィールドの2種類に分かれています。
setTabActionメソッドをTabAction.Controlに設定すると、[Tab]キーまたは[Shift]+[Tab]キーによるフォーカス移動は、コントロール 間で行われます。TabAction.Fieldに設定すると、フォーカス移動は入力フィールド間で行われます。
また、マスクコントロールのナビゲーションや選択などの操作は、リテラル文字列と入力フィールドの位置関係による影響を受けます。
キャレットとフィールドの設定
setCursorPositionメソッドとsetHighlightTextメソッドを使用すると、コントロールがフォーカスを受け取ったときにキャレットを配置するフィールドまたは選択状態にするフィールドを指定できます。
HighlightTextの値
キャレットまたはフィールドの状態
None
キャレットは、setCursorPositionメソッドで指定したフィールドの最初の桁に位置します。
Field
setCursorPositionメソッドで指定したフィールドが選択状態になります。
All
setCursorPositionメソッドの設定に関わらず、コントロール内のすべての内容が選択状態になります。
Enterキーによるフォーカス移動
setExitOnEnterKeyメソッドを使用すると、[Shift]+[Enter]、[Enter]の各キーを押したときに、フォーカスを前後のコントロールに移動することができます。setExitOnEnterKeyメソッドに設定できる値は次のとおりで、既定値はExitKey.Noneです。
ExitKeyの値
説明
None
Enterキーを使ってフォーカスを移動することはできません。
ShiftEnter
[Shift]+[Enter]キーを押すと、フォーカスは1つ前のコントロールに移動します。
Enter
[Enter]キーを押すと、フォーカスは次のコントロールに移動します。
Both
「ShiftEnter」と「Enter」の両方の機能を使用できます。
import * as React from "react";
import * as ReactDom from "react-dom";
import { GcMask } from "@mescius/inputman.react";
import { InputMan } from "@mescius/inputman";
import '@mescius/inputman/CSS/gc.inputman-js.css';
const ExitOnLeftRightKeys = [
InputMan.ExitOnLeftRightKey.None,
InputMan.ExitOnLeftRightKey.Both,
InputMan.ExitOnLeftRightKey.Left,
InputMan.ExitOnLeftRightKey.Right
];
const TabActions = [
InputMan.TabAction.Control,
InputMan.TabAction.Field
];
const HighlightTexts = [
InputMan.HighlightText.None,
InputMan.HighlightText.Field,
InputMan.HighlightText.All
];
// Enterキーによるフォーカス移動
const ExitKeys = [
InputMan.ExitKey.None,
InputMan.ExitKey.Both,
InputMan.ExitKey.Enter,
InputMan.ExitKey.ShiftEnter
];
class App extends React.Component {
constructor(props, context) {
InputMan.appearanceStyle = InputMan.AppearanceStyle.Modern;
super(props, context);
this.state = {
exitOnLastChar: false,
exitOnLeftRightKey: InputMan.ExitOnLeftRightKey.None,
exitOnEnterKey: InputMan.ExitKey.None
};
}
render() {
return (
<div>
<GcMask
formatPattern={'〒\\D{3}-\\D{4}'}
exitOnLastChar={this.state.exitOnLastChar}
exitOnLeftRightKey={this.state.exitOnLeftRightKey}
tabAction={this.state.tabAction}
highlightText={this.state.highlightText}
exitOnEnterKey={this.state.exitOnEnterKey}>
</GcMask>
<GcMask
formatPattern={'〒\\D{3}-\\D{4}'}
exitOnLastChar={this.state.exitOnLastChar}
exitOnLeftRightKey={this.state.exitOnLeftRightKey}
tabAction={this.state.tabAction}
highlightText={this.state.highlightText}
exitOnEnterKey={this.state.exitOnEnterKey}>
</GcMask>
<GcMask
formatPattern={'〒\\D{3}-\\D{4}'}
exitOnLastChar={this.state.exitOnLastChar}
exitOnLeftRightKey={this.state.exitOnLeftRightKey}
tabAction={this.state.tabAction}
highlightText={this.state.highlightText}
exitOnEnterKey={this.state.exitOnEnterKey}>
</GcMask>
<table class="sample">
<tr>
<th>自動フォーカス移動</th>
<td><label><input type="checkbox" checked={this.state.exitOnLastChar} id="setExitOnLastChar" onChange={(e) => this.setState({ exitOnLastChar: e.target.checked })} />入力完了時に自動的に次のコントロールに移動する</label>
</td>
</tr>
<tr>
<th>矢印キーによるフォーカス移動</th>
<td>
<select id="setExitOnLeftRightKey" onChange={(e) => { this.setState({ exitOnLeftRightKey: ExitOnLeftRightKeys[e.target.selectedIndex] }) }}>
<option>なし</option>
<option>両方</option>
<option>左</option>
<option>右</option>
</select>
</td>
</tr>
<tr>
<th>フォーカスの移動先</th>
<td>
<select id="setTabAction" onChange={(e) => { this.setState({ tabAction: TabActions[e.target.selectedIndex] }) }}>
<option>コントロール間を移動</option>
<option>フィールド間を移動</option>
</select>
</td>
</tr>
<tr>
<th>フォーカス移動後の状態</th>
<td>
<select id="setHighlightText" onChange={(e) => { this.setState({ highlightText: HighlightTexts[e.target.selectedIndex] }) }}>
<option>キャレットをフィールドの先頭に移動</option>
<option>フィールドを選択</option>
<option>コントロールのすべての内容を選択</option>
</select>
</td>
</tr>
<tr>
<th>Enterキーによるフォーカス移動</th>
<td>
<select id="setExitOnEnterKey" onChange={(e) => { this.setState({ exitOnEnterKey: ExitKeys[e.target.selectedIndex] }) }}>
<option>なし</option>
<option>両方</option>
<option>Enter</option>
<option>Shift+Enter</option>
</select>
</td>
</tr>
</table>
</div>
)
}
}
ReactDom.render(<App />, document.getElementById("app"));
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>マスクコントロール - フォーカス移動</title>
<!-- SystemJS -->
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
window.onload = function() {
System.import('./src/app');
}
</script>
</head>
<body>
<div id="app"></div>
</body>
</html>
(function (global) {
System.config({
transpiler: 'plugin-babel',
babelOptions: {
es2015: true,
react: true
},
meta: {
'*.css': { loader: 'css' }
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
"@mescius/inputman": "npm:@mescius/inputman/index.js",
"@mescius/inputman.react": "npm:@mescius/inputman.react/GcInputMan.component.js",
"@mescius/inputman/CSS": "npm:@mescius/inputman/CSS",
'react': 'npm:react/umd/react.production.min.js',
'react-dom': 'npm:react-dom/umd/react-dom.production.min.js',
'css': 'npm:systemjs-plugin-css/css.js',
'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js',
'systemjs-babel-build':'npm:systemjs-plugin-babel/systemjs-babel-browser.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
src: {
defaultExtension: 'jsx'
},
"node_modules": {
defaultExtension: 'js'
},
}
});
})(this);