正则表达式 InputNumber 格式化程序 ant-design [英] regex InputNumber formatter ant-design
问题描述
伙计们,我真的不了解正则表达式,我正在使用 ant-design Input Number 组件来制作货币过滤器.
Guys I really not understand about regex, I'm using ant-design Input Number component to make a currency filter.
目前是这样的:
<InputNumber
style={{ width: 175 }}
formatter={value => `R$ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g,
".")}
parser={value => value.replace(/[A-Z]|[a-z]|[$ ]|\.+/g, "")}
onChange={(value) => setSelectedKeys(value ? value : [])}
/>
目前的格式,比如一千五百是这样的:1.500我也需要它接受负数(-1.500)和逗号,例如 1.500,25
The current format, for exemple thousand five hundred is like this : 1.500 I need it to accept as well, negatives numbers (-1.500) and comma for the cents like 1.500,25
你们能帮帮我吗?我尝试了一些解决方案,但无法按我的需要运行
Can you guys help me ? I've try some solutions but can't make it work as I need
推荐答案
伙计们,经过大量搜索,我找到了一个解决方案,不可能是更好的,但目前正在发挥作用..
Guys after searching a lot,I found one solution, can't be the better one, but at moment is doing its job..
我安装了 MaskedInput 和 text-mask-addons 依赖项.
I installed MaskedInput and text-mask-addons dependencies.
import MaskedInput from "react-text-mask";
import createNumberMask from "text-mask-addons/dist/createNumberMask";
const defaultMaskOptions = {
prefix: "R$",
suffix: "",
includeThousandsSeparator: true,
thousandsSeparatorSymbol: ".",
allowDecimal: true,
decimalSymbol: ",",
decimalLimit: 2,
integerLimit: 7,
allowNegative: true,
allowLeadingZeroes: false,
};
const currencyMask = createNumberMask(defaultMaskOptions);
const NumberFilter = (
<Space style={{ marginRight: 20 }}>
<MaskedInput
mask={currencyMask}
render={(ref, props) => (
<Input
placeholder="Valor inicial"
ref={(input) => ref(input && input.input)}
{...props}
value={selectedKeys[0]}
onChange={(event) => {
props.onChange(event);
let betweenInitial = [...selectedKeys];
betweenInitial[0] = event.target.value;
setSelectedKeys(betweenInitial);
}}
/>
)}
/>
<RiArrowLeftRightLine />
<MaskedInput
mask={currencyMask}
render={(ref, props) => (
<Input
placeholder="Valor final"
ref={(input) => ref(input && input.input)}
{...props}
value={selectedKeys[1]}
onChange={(event) => {
props.onChange(event);
let betweenFinal = [...selectedKeys];
betweenFinal[1] = event.target.value;
setSelectedKeys(betweenFinal);
}}
/>
)}
/>
</Space>
);
这篇关于正则表达式 InputNumber 格式化程序 ant-design的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!