テキストコントロールの入力フィールドにフローティングラベルを表示する機能について解説します。
フローティングラベルを表示する方法
フローティングラベルを表示するには、floatingLabelTextプロパティにフローティングラベルの表示テキストを設定します。floatingLabelDirectionプロパティを設定することで、入力時にフローティングラベルを指定位置に移動します。
floatingLabelDirectionプロパティの設定できる値は次のとおりで、既定値はFloatingLabelDirection.Outsideです。
FloatingLabelDirectionの値
説明
Outside
入力中にラベルが外側に表示する
ThroughBorder
入力中にラベルが上の線と重なって表示する
Inside
入力中にラベルが内側に表示する
また、floatingLabelTypeプロパティを設定すると、入力中のフローティングラベルの表示位置を設定できます。floatingLabelTypeプロパティの設定できる値は次のとおりで、既定値はFloatingLabelType.Autoです。
FloatingLabelTypeの値
説明
Auto
入力中にラベルがfloatingLabelDirectionに設定した位置に調整される
Always
ラベルが常に表示する
None
ラベルが常に表示しない
注意事項
watermarkDisplayNullTextプロパティと同時に設定すると、フローティングラベルがウォーターマークと重なる場合もあります。
import * as React from "react";
import * as ReactDom from "react-dom";
import { GcTextBox } from "@mescius/inputman.react";
import { InputMan } from "@mescius/inputman";
import '@mescius/inputman/CSS/gc.inputman-js.css';
import './styles.css';
InputMan.appearanceStyle = InputMan.AppearanceStyle.Modern;
const floatingLabelDirection = [
InputMan.FloatingLabelDirection.Outside,
InputMan.FloatingLabelDirection.ThroughBorder,
InputMan.FloatingLabelDirection.Inside,
];
const floatingLabelType = [
InputMan.FloatingLabelType.Auto,
InputMan.FloatingLabelType.Always,
InputMan.FloatingLabelType.None,
];
const App = () => {
const [direction, setDirection] = React.useState(InputMan.FloatingLabelDirection.ThroughBorder);
const [type, setType] = React.useState(InputMan.FloatingLabelType.Auto);
return (
<div>
<div>
<GcTextBox
floatingLabelText="名前"
floatingLabelDirection={direction}
floatingLabelType={type}
></GcTextBox>
</div>
<br />
<div>
フローティングラベルのスタイルを変更する<br />
<div class="custom">
<GcTextBox
floatingLabelText="名前"
floatingLabelDirection={direction}
floatingLabelType={type}
></GcTextBox>
</div>
</div>
<table class="sample">
<tr>
<th>入力中のラベルの表示位置</th>
<td>
<select onChange={e => setDirection(floatingLabelDirection[e.target.selectedIndex])}>
<option>外側</option>
<option selected>上線と重なる</option>
<option>内側</option>
</select>
</td>
</tr>
<tr>
<th>入力中のラベルの表示</th>
<td>
<select onChange={e => setType(floatingLabelType[e.target.selectedIndex])}>
<option selected>設定位置</option>
<option>常に表示</option>
<option>常に表示しない</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>
body {
padding-bottom: 10rem;
}
.gcim {
width: 180px;
}
.custom .gcim {
border-color: rgb(255, 204, 127);
}
.custom .gcim_focused {
border-color: rgb(255, 127, 127);
}
.custom .gcim-floating-label__container[label-state='floating'][label-direction='throughborder'] {
font-family: 'Serif';
font-size: medium;
color: rgb(255, 127, 127);
left: 30px;
}
.custom .gcim-floating-label__container[label-state='floating'][label-direction='throughborder'] {
color: rgb(255, 204, 127);
}
.custom .gcim_focused .gcim-floating-label__container[label-state='floating'][label-direction='throughborder'] {
color: rgb(255, 127, 127);
}
.custom .gcim-floating-label__container[label-state='display'] {
font-family: 'Serif';
font-size: medium;
color: rgb(255, 204, 127);
left: 30px;
}
body {
padding-bottom: 10rem;
}
[gcim-control-appearance="modern"] .gcim {
width: 180px;
}
[gcim-control-appearance="modern"] .custom .gcim {
border-color: rgb(255, 204, 127);
}
[gcim-control-appearance="modern"] .custom .gcim_focused {
border-color: rgb(255, 127, 127);
}
[gcim-control-appearance="modern"] .custom .gcim-floating-label__container[label-state='floating'][label-direction='throughborder'] {
font-family: 'Serif';
font-size: medium;
color: rgb(255, 127, 127);
left: 30px;
}
[gcim-control-appearance="modern"] .custom .gcim-floating-label__container[label-state='floating'][label-direction='throughborder'] {
color: rgb(255, 204, 127);
}
[gcim-control-appearance="modern"] .custom .gcim_focused .gcim-floating-label__container[label-state='floating'][label-direction='throughborder'] {
color: rgb(255, 127, 127);
}
[gcim-control-appearance="modern"] .custom .gcim-floating-label__container[label-state='display'] {
font-family: 'Serif';
font-size: medium;
color: rgb(255, 204, 127);
left: 30px;
}
(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);