Optimize decimal point number input

This commit is contained in:
MrKBear 2022-03-08 11:51:04 +08:00
parent 0e1aa51d4b
commit 8f5fff920f
2 changed files with 17 additions and 2 deletions

View File

@ -3,8 +3,10 @@ import { FontLevel, Theme } from "@Component/Theme/Theme";
import "./AttrInput.scss"; import "./AttrInput.scss";
import { Icon } from "@fluentui/react"; import { Icon } from "@fluentui/react";
import { Localization, AllI18nKeys } from "@Component/Localization/Localization"; import { Localization, AllI18nKeys } from "@Component/Localization/Localization";
import { ObjectID } from "@Model/Renderer";
interface IAttrInputProps { interface IAttrInputProps {
id?: ObjectID;
keyI18n: AllI18nKeys; keyI18n: AllI18nKeys;
infoI18n?: AllI18nKeys; infoI18n?: AllI18nKeys;
value?: number | string; value?: number | string;
@ -35,7 +37,7 @@ class AttrInput extends Component<IAttrInputProps> {
const praseNumber = (value as any) / 1; const praseNumber = (value as any) / 1;
// 数字校验 // 数字校验
if (isNaN(praseNumber)) { if (isNaN(praseNumber) || /\.0*$/.test(value)) {
return <Localization i18nKey="Input.Error.Not.Number" /> return <Localization i18nKey="Input.Error.Not.Number" />
} }
@ -127,7 +129,16 @@ class AttrInput extends Component<IAttrInputProps> {
} }
public shouldComponentUpdate(nextProps: IAttrInputProps) { public shouldComponentUpdate(nextProps: IAttrInputProps) {
// ID 都为空时更新
if (!nextProps.id && !this.props.id) {
this.updateValueFromProps(nextProps.value); this.updateValueFromProps(nextProps.value);
}
// ID 变换时更新 State 到最新的 Props
if (nextProps.id !== this.props.id) {
this.updateValueFromProps(nextProps.value);
}
return true; return true;
} }

View File

@ -21,6 +21,7 @@ class RangeDetails extends Component<IMixinStatusProps> {
private renderFrom(range: Range) { private renderFrom(range: Range) {
return <> return <>
<AttrInput <AttrInput
id={range.id}
keyI18n="Common.Attr.Key.Display.Name" keyI18n="Common.Attr.Key.Display.Name"
value={range.displayName} value={range.displayName}
valueChange={(e) => { valueChange={(e) => {
@ -28,6 +29,7 @@ class RangeDetails extends Component<IMixinStatusProps> {
}} }}
/> />
<AttrInput <AttrInput
id={range.id}
isNumber={true} isNumber={true}
step={.1} step={.1}
keyI18n="Common.Attr.Key.Position.X" keyI18n="Common.Attr.Key.Position.X"
@ -40,6 +42,7 @@ class RangeDetails extends Component<IMixinStatusProps> {
}} }}
/> />
<AttrInput <AttrInput
id={range.id}
isNumber={true} isNumber={true}
step={.1} step={.1}
keyI18n="Common.Attr.Key.Position.Y" keyI18n="Common.Attr.Key.Position.Y"
@ -52,6 +55,7 @@ class RangeDetails extends Component<IMixinStatusProps> {
}} }}
/> />
<AttrInput <AttrInput
id={range.id}
isNumber={true} isNumber={true}
step={.1} step={.1}
keyI18n="Common.Attr.Key.Position.Z" keyI18n="Common.Attr.Key.Position.Z"