react-currency-input-field
Advanced tools
Comparing version 3.0.0-beta.1 to 3.0.0-beta.2
@@ -0,1 +1,13 @@ | ||
# [3.0.0-beta.2](https://github.com/cchanxzy/react-currency-input-field/compare/v3.0.0-beta.1...v3.0.0-beta.2) (2020-12-03) | ||
### Features | ||
* add intl locale config option ([e119352](https://github.com/cchanxzy/react-currency-input-field/commit/e119352212b1aaa8bafdb02dfd312de7a7302cfc)) | ||
### BREAKING CHANGES | ||
* Using Intl.NumberFormat to format value | ||
# [3.0.0-beta.1](https://github.com/cchanxzy/react-currency-input-field/compare/v2.7.0...v3.0.0-beta.1) (2020-11-19) | ||
@@ -2,0 +14,0 @@ |
import { Ref } from 'react'; | ||
declare type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U; | ||
export declare type Separator = ',' | '.'; | ||
export declare type IntlConfig = { | ||
locale: string; | ||
currency: string; | ||
}; | ||
export declare type CurrencyInputProps = Overwrite<React.InputHTMLAttributes<HTMLInputElement>, { | ||
@@ -36,3 +39,3 @@ /** | ||
/** | ||
* Default value | ||
* Default value if not passing in value | ||
*/ | ||
@@ -100,2 +103,10 @@ defaultValue?: number | string; | ||
/** | ||
* International locale config, examples: | ||
* { locale: 'ja-JP', currency: 'JPY' } | ||
* { locale: 'en-IN', currency: 'INR' } | ||
* | ||
* Any prefix, groupSeparator or decimalSeparator options passed in will override locale defaults | ||
*/ | ||
intlConfig?: IntlConfig; | ||
/** | ||
* Ref property | ||
@@ -102,0 +113,0 @@ */ |
@@ -1,2 +0,3 @@ | ||
declare type Props = { | ||
import { IntlConfig } from '../CurrencyInputProps'; | ||
declare type FormatValueOptions = { | ||
/** | ||
@@ -27,2 +28,6 @@ * Value to format | ||
/** | ||
* Intl locale currency config | ||
*/ | ||
intlConfig?: IntlConfig; | ||
/** | ||
* Prefix | ||
@@ -35,3 +40,3 @@ */ | ||
*/ | ||
export declare const formatValue: (props: Props) => string; | ||
export declare const formatValue: (options: FormatValueOptions) => string; | ||
export {}; |
export * from './cleanValue'; | ||
export * from './fixedDecimalValue'; | ||
export * from './formatValue'; | ||
export * from './getLocaleConfig'; | ||
export * from './isNumber'; | ||
export * from './padTrimValue'; |
@@ -1,2 +0,2 @@ | ||
module.exports=function(e){var r={};function t(a){if(r[a])return r[a].exports;var n=r[a]={i:a,l:!1,exports:{}};return e[a].call(n.exports,n,n.exports,t),n.l=!0,n.exports}return t.m=e,t.c=r,t.d=function(e,r,a){t.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:a})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,r){if(1&r&&(e=t(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var a=Object.create(null);if(t.r(a),Object.defineProperty(a,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var n in e)t.d(a,n,function(r){return e[r]}.bind(null,n));return a},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},t.p="",t(t.s=2)}([function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.escapeRegExp=void 0,r.escapeRegExp=function(e){return e.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&")}},function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.formatValue=void 0;var a=t(11);r.formatValue=function(e){var r=e.value,t=e.groupSeparator,n=void 0===t?",":t,i=e.decimalSeparator,o=void 0===i?".":i,u=e.turnOffSeparators,l=void 0!==u&&u,c=e.prefix;if(""===r||void 0===r)return"";var f=String(r);if("-"===f)return"-";var d=RegExp("^-\\d+").test(f),s=o&&f.includes(o),p=d?f.replace("-",""):f,v=s?p.split(o):[p],m=v[0],b=v[1];return""+(d?"-":"")+(c||"")+(l?m:a.addSeparators(m,n))+(s&&b?""+o+b:s?""+o:"")}},function(e,r,t){"use strict";var a=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(r,"__esModule",{value:!0});var n=a(t(3));r.default=n.default;var i=t(1);Object.defineProperty(r,"formatValue",{enumerable:!0,get:function(){return i.formatValue}})},function(e,r,t){"use strict";var a=this&&this.__assign||function(){return(a=Object.assign||function(e){for(var r,t=1,a=arguments.length;t<a;t++)for(var n in r=arguments[t])Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n]);return e}).apply(this,arguments)},n=this&&this.__createBinding||(Object.create?function(e,r,t,a){void 0===a&&(a=t),Object.defineProperty(e,a,{enumerable:!0,get:function(){return r[t]}})}:function(e,r,t,a){void 0===a&&(a=t),e[a]=r[t]}),i=this&&this.__setModuleDefault||(Object.create?function(e,r){Object.defineProperty(e,"default",{enumerable:!0,value:r})}:function(e,r){e.default=r}),o=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var r={};if(null!=e)for(var t in e)"default"!==t&&Object.hasOwnProperty.call(e,t)&&n(r,e,t);return i(r,e),r},u=this&&this.__rest||function(e,r){var t={};for(var a in e)Object.prototype.hasOwnProperty.call(e,a)&&r.indexOf(a)<0&&(t[a]=e[a]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(a=Object.getOwnPropertySymbols(e);n<a.length;n++)r.indexOf(a[n])<0&&Object.prototype.propertyIsEnumerable.call(e,a[n])&&(t[a[n]]=e[a[n]])}return t};Object.defineProperty(r,"__esModule",{value:!0}),r.CurrencyInput=void 0;var l=o(t(4)),c=t(5);r.CurrencyInput=l.forwardRef((function(e,r){var t=e.allowDecimals,n=void 0===t||t,i=e.allowNegativeValue,o=void 0===i||i,f=e.id,d=e.name,s=e.className,p=e.decimalsLimit,v=e.defaultValue,m=e.disabled,b=void 0!==m&&m,g=e.maxLength,h=e.value,y=e.onChange,_=e.onBlurValue,O=e.fixedDecimalLength,S=e.placeholder,x=e.precision,j=e.prefix,w=e.step,V=e.decimalSeparator,P=void 0===V?".":V,M=e.groupSeparator,E=void 0===M?",":M,R=e.turnOffSeparators,N=void 0!==R&&R,C=e.turnOffAbbreviations,D=void 0!==C&&C,A=u(e,["allowDecimals","allowNegativeValue","id","name","className","decimalsLimit","defaultValue","disabled","maxLength","value","onChange","onBlurValue","fixedDecimalLength","placeholder","precision","prefix","step","decimalSeparator","groupSeparator","turnOffSeparators","turnOffAbbreviations"]);if(P===E)throw new Error("decimalSeparator cannot be the same as groupSeparator");if(c.isNumber(P))throw new Error("decimalSeparator cannot be a number");if(c.isNumber(E))throw new Error("groupSeparator cannot be a number");var L={decimalSeparator:P,groupSeparator:E,turnOffSeparators:N,prefix:j},I={decimalSeparator:P,groupSeparator:E,allowDecimals:n,decimalsLimit:p||O||2,allowNegativeValue:o,turnOffAbbreviations:D,prefix:j},B=void 0!==v?c.formatValue(a({value:String(v)},L)):"",T=l.useState(B),k=T[0],$=T[1],U=l.useState(0),q=U[0],F=U[1],G=r||l.useRef(null),K=function(e,r){var t=c.cleanValue(a({value:e},I));if(!t)return y&&y(void 0,d),void $("");if(!(g&&t.replace(/-/g,"").length>g)){if("-"===t)return y&&y(void 0,d),void $(e);var n=c.formatValue(a({value:t},L));if(null!=r){var i=r+(n.length-e.length)||1;F(i)}$(n),y&&y(t,d)}};l.useEffect((function(){G&&"object"==typeof G&&G.current&&G.current.setSelectionRange(q,q)}),[q,G]);var z=void 0!==h?c.formatValue(a({value:String(h)},L)):void 0;return l.default.createElement("input",a({type:"text",inputMode:"decimal",id:f,name:d,className:s,onChange:function(e){var r=e.target,t=r.value,a=r.selectionStart;K(t,a)},onBlur:function(e){var r=e.target.value,t=c.cleanValue(a({value:r},I));if("-"===t||!t)return _&&_(void 0,d),void $("");var n=c.fixedDecimalValue(t,P,O),i=c.padTrimValue(n,P,x||O);y&&y(i,d),_&&_(i,d);var o=c.formatValue(a({value:i},L));$(o)},onFocus:function(){return k?k.length:0},onKeyDown:function(e){var r=e.key;if(w&&("ArrowUp"===r||"ArrowDown"===r)){var t=Number(void 0!==h?h:c.cleanValue(a({value:k},I)))||0,n=String("ArrowUp"===r?t+Number(w):t-Number(w));K(n)}},placeholder:S,disabled:b,value:void 0!==z&&"-"!==k?z:k,ref:G},A))})),r.CurrencyInput.displayName="CurrencyInput",r.default=r.CurrencyInput},function(e,r){e.exports=require("react")},function(e,r,t){"use strict";var a=this&&this.__createBinding||(Object.create?function(e,r,t,a){void 0===a&&(a=t),Object.defineProperty(e,a,{enumerable:!0,get:function(){return r[t]}})}:function(e,r,t,a){void 0===a&&(a=t),e[a]=r[t]}),n=this&&this.__exportStar||function(e,r){for(var t in e)"default"===t||r.hasOwnProperty(t)||a(r,e,t)};Object.defineProperty(r,"__esModule",{value:!0}),n(t(6),r),n(t(10),r),n(t(1),r),n(t(12),r),n(t(13),r)},function(e,r,t){"use strict";var a=this&&this.__spreadArrays||function(){for(var e=0,r=0,t=arguments.length;r<t;r++)e+=arguments[r].length;var a=Array(e),n=0;for(r=0;r<t;r++)for(var i=arguments[r],o=0,u=i.length;o<u;o++,n++)a[n]=i[o];return a};Object.defineProperty(r,"__esModule",{value:!0}),r.cleanValue=void 0;var n=t(7),i=t(8),o=t(9),u=t(0);r.cleanValue=function(e){var r=e.value,t=e.groupSeparator,l=void 0===t?",":t,c=e.decimalSeparator,f=void 0===c?".":c,d=e.allowDecimals,s=void 0===d||d,p=e.decimalsLimit,v=void 0===p?2:p,m=e.allowNegativeValue,b=void 0===m||m,g=e.turnOffAbbreviations,h=void 0!==g&&g,y=e.prefix,_=void 0===y?"":y,O=h?[]:["k","m","b"],S=r.includes("-"),x=RegExp("(\\d+)-?"+u.escapeRegExp(_)).exec(r)||[],j=x[0],w=x[1],V=_?r.replace(j,"").concat(w):r,P=i.removeSeparators(V,l),M=o.removeInvalidChars(P,a([l,f],O)),E=M;if(!h){if(O.some((function(e){return e===M.toLowerCase()})))return"";var R=n.parseAbbrValue(M,f);R&&(E=String(R))}var N=S&&b?"-":"";if(String(E).includes(f)){var C=M.split(f),D=C[0],A=C[1],L=v?A.slice(0,v):A;return""+N+D+(s?""+f+L:"")}return""+N+E}},function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.parseAbbrValue=r.abbrValue=void 0;var a=t(0);r.abbrValue=function(e,r,t){if(void 0===r&&(r="."),void 0===t&&(t=10),e>999){var a=(""+e).length,n=Math.pow,i=n(10,t);return a-=a%3,(Math.round(e*i/n(10,a))/i+" kMGTPE"[a/3]).replace(".",r)}return String(e)};var n={k:1e3,m:1e6,b:1e9};r.parseAbbrValue=function(e,r){void 0===r&&(r=".");var t=new RegExp("(\\d+("+a.escapeRegExp(r)+"\\d+)?)([kmb])$","i"),i=e.match(t);if(i){var o=i[1],u=i[3],l=n[u.toLowerCase()];if(o&&l)return Number(o.replace(r,"."))*l}}},function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.removeSeparators=void 0;var a=t(0);r.removeSeparators=function(e,r){void 0===r&&(r=",");var t=new RegExp(a.escapeRegExp(r),"g");return e.replace(t,"")}},function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.removeInvalidChars=void 0;var a=t(0);r.removeInvalidChars=function(e,r){var t=a.escapeRegExp(r.join("")),n=new RegExp("[^\\d"+t+"]","gi");return e.replace(n,"")}},function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.fixedDecimalValue=void 0,r.fixedDecimalValue=function(e,r,t){if(t&&e.length>1){if(e.includes(r)){var a=e.split(r),n=a[0];if((i=a[1]).length>t)return""+n+r+i.slice(0,t)}var i,o=e.length>t?new RegExp("(\\d+)(\\d{"+t+"})"):new RegExp("(\\d)(\\d+)"),u=e.match(o);if(u)return""+(n=u[1])+r+(i=u[2])}return e}},function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.addSeparators=void 0,r.addSeparators=function(e,r){return void 0===r&&(r=","),e.replace(/\B(?=(\d{3})+(?!\d))/g,r)}},function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.isNumber=void 0,r.isNumber=function(e){return RegExp(/\d/,"gi").test(e)}},function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.padTrimValue=void 0,r.padTrimValue=function(e,r,t){if(void 0===r&&(r="."),!t||""===e||void 0===e)return e;if(!e.match(/\d/g))return"";var a=e.split(r),n=a[0],i=a[1]||"";if(i.length<t)for(;i.length<t;)i+="0";else i=i.slice(0,t);return""+n+r+i}}]); | ||
module.exports=function(e){var r={};function t(a){if(r[a])return r[a].exports;var n=r[a]={i:a,l:!1,exports:{}};return e[a].call(n.exports,n,n.exports,t),n.l=!0,n.exports}return t.m=e,t.c=r,t.d=function(e,r,a){t.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:a})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,r){if(1&r&&(e=t(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var a=Object.create(null);if(t.r(a),Object.defineProperty(a,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var n in e)t.d(a,n,function(r){return e[r]}.bind(null,n));return a},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},t.p="",t(t.s=2)}([function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.escapeRegExp=void 0,r.escapeRegExp=function(e){return e.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&")}},function(e,r,t){"use strict";var a=this&&this.__spreadArrays||function(){for(var e=0,r=0,t=arguments.length;r<t;r++)e+=arguments[r].length;var a=Array(e),n=0;for(r=0;r<t;r++)for(var o=arguments[r],i=0,u=o.length;i<u;i++,n++)a[n]=o[i];return a};Object.defineProperty(r,"__esModule",{value:!0}),r.formatValue=void 0;var n=t(0);r.formatValue=function(e){var r=e.value,t=e.decimalSeparator,a=e.intlConfig,u=e.prefix,l=void 0===u?"":u;if(""===r||void 0===r)return"";if("-"===r)return"-";var c=new RegExp("^\\d?-"+(l?n.escapeRegExp(l)+"?":"")+"\\d").test(String(r)),f="."!==t?o(String(r),t,c):String(r),p=(a?new Intl.NumberFormat(a.locale,{style:"currency",currency:a.currency,minimumFractionDigits:0}):new Intl.NumberFormat).formatToParts(Number(f)),d=i(p,e);a||(d=c?d.replace(/^-/g,"-"+l):""+l+d);var s=f.slice(-1)===t?t:"",v=(f.match(RegExp("\\d+\\.(\\d+)"))||[])[1];return v&&t&&(d=d.includes(t)?d.replace(RegExp("(\\d+)("+n.escapeRegExp(t)+")(\\d+)","g"),"$1$2"+v):""+d+t+v),[d,s].join("")};var o=function(e,r,t){var a=e;return r&&"."!==r&&(a=a.replace(RegExp(n.escapeRegExp(r),"g"),"."),t&&"-"===r&&(a="-"+a.slice(1))),a},i=function(e,r){var t=r.prefix,n=r.groupSeparator,o=r.decimalSeparator,i=r.turnOffSeparators,u=void 0!==i&&i;return e.reduce((function(e,r){var i=r.type,l=r.value;return"currency"===i&&t?a(e,[t]):"group"===i?u?e:a(e,[void 0!==n?n:l]):"decimal"===i?u?e:a(e,[void 0!==o?o:l]):a(e,[l])}),[""]).join("")}},function(e,r,t){"use strict";var a=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(r,"__esModule",{value:!0});var n=a(t(3));r.default=n.default;var o=t(1);Object.defineProperty(r,"formatValue",{enumerable:!0,get:function(){return o.formatValue}})},function(e,r,t){"use strict";var a=this&&this.__assign||function(){return(a=Object.assign||function(e){for(var r,t=1,a=arguments.length;t<a;t++)for(var n in r=arguments[t])Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n]);return e}).apply(this,arguments)},n=this&&this.__createBinding||(Object.create?function(e,r,t,a){void 0===a&&(a=t),Object.defineProperty(e,a,{enumerable:!0,get:function(){return r[t]}})}:function(e,r,t,a){void 0===a&&(a=t),e[a]=r[t]}),o=this&&this.__setModuleDefault||(Object.create?function(e,r){Object.defineProperty(e,"default",{enumerable:!0,value:r})}:function(e,r){e.default=r}),i=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var r={};if(null!=e)for(var t in e)"default"!==t&&Object.hasOwnProperty.call(e,t)&&n(r,e,t);return o(r,e),r},u=this&&this.__rest||function(e,r){var t={};for(var a in e)Object.prototype.hasOwnProperty.call(e,a)&&r.indexOf(a)<0&&(t[a]=e[a]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(a=Object.getOwnPropertySymbols(e);n<a.length;n++)r.indexOf(a[n])<0&&Object.prototype.propertyIsEnumerable.call(e,a[n])&&(t[a[n]]=e[a[n]])}return t};Object.defineProperty(r,"__esModule",{value:!0}),r.CurrencyInput=void 0;var l=i(t(4)),c=t(5);r.CurrencyInput=l.forwardRef((function(e,r){var t=e.allowDecimals,n=void 0===t||t,o=e.allowNegativeValue,i=void 0===o||o,f=e.id,p=e.name,d=e.className,s=e.decimalsLimit,v=e.defaultValue,g=e.disabled,m=void 0!==g&&g,b=e.maxLength,y=e.value,h=e.onChange,S=e.onBlurValue,_=e.fixedDecimalLength,O=e.placeholder,x=e.precision,w=e.prefix,j=e.intlConfig,P=e.step,V=e.turnOffSeparators,E=void 0!==V&&V,M=e.turnOffAbbreviations,R=void 0!==M&&M,N=u(e,["allowDecimals","allowNegativeValue","id","name","className","decimalsLimit","defaultValue","disabled","maxLength","value","onChange","onBlurValue","fixedDecimalLength","placeholder","precision","prefix","intlConfig","step","turnOffSeparators","turnOffAbbreviations"]);if(N.decimalSeparator&&N.groupSeparator&&N.decimalSeparator===N.groupSeparator)throw new Error("decimalSeparator cannot be the same as groupSeparator");if(N.decimalSeparator&&c.isNumber(N.decimalSeparator))throw new Error("decimalSeparator cannot be a number");if(N.groupSeparator&&c.isNumber(N.groupSeparator))throw new Error("groupSeparator cannot be a number");var C=l.useMemo((function(){return c.getLocaleConfig(j)}),[j]),A=N.decimalSeparator||C.decimalSeparator||"",D=N.groupSeparator||C.groupSeparator||"",I={decimalSeparator:A,groupSeparator:D,turnOffSeparators:E,intlConfig:j,prefix:w},L={decimalSeparator:A,groupSeparator:D,allowDecimals:n,decimalsLimit:s||_||2,allowNegativeValue:i,turnOffAbbreviations:R,prefix:w},T=void 0!==v?c.formatValue(a({value:String(v)},I)):"",F=l.useState(T),k=F[0],B=F[1],$=l.useState(0),U=$[0],q=$[1],G=r||l.useRef(null),K=function(e,r){var t=c.cleanValue(a({value:e},L));if(!t)return h&&h(void 0,p),void B("");if(!(b&&t.replace(/-/g,"").length>b)){if("-"===t)return h&&h(void 0,p),void B(e);var n=c.formatValue(a({value:t},I));if(null!=r){var o=r+(n.length-e.length)||1;q(o)}B(n),h&&h(t,p)}};l.useEffect((function(){G&&"object"==typeof G&&G.current&&G.current.setSelectionRange(U,U)}),[U,G]);var z=void 0!==y?c.formatValue(a(a({},I),{value:String(y)})):void 0;return l.default.createElement("input",a({type:"text",inputMode:"decimal",id:f,name:p,className:d,onChange:function(e){var r=e.target,t=r.value,a=r.selectionStart;K(t,a)},onBlur:function(e){var r=e.target.value,t=c.cleanValue(a({value:r},L));if("-"===t||!t)return S&&S(void 0,p),void B("");var n=c.fixedDecimalValue(t,A,_),o=c.padTrimValue(n,A,x||_);h&&h(o,p),S&&S(o,p);var i=c.formatValue(a(a({},I),{value:o}));B(i)},onFocus:function(){return k?k.length:0},onKeyDown:function(e){var r=e.key;if(P&&("ArrowUp"===r||"ArrowDown"===r)){var t=Number(void 0!==y?y:c.cleanValue(a({value:k},L)))||0,n=String("ArrowUp"===r?t+Number(P):t-Number(P));K(n)}},placeholder:O,disabled:m,value:void 0!==z&&"-"!==k?z:k,ref:G},N))})),r.CurrencyInput.displayName="CurrencyInput",r.default=r.CurrencyInput},function(e,r){e.exports=require("react")},function(e,r,t){"use strict";var a=this&&this.__createBinding||(Object.create?function(e,r,t,a){void 0===a&&(a=t),Object.defineProperty(e,a,{enumerable:!0,get:function(){return r[t]}})}:function(e,r,t,a){void 0===a&&(a=t),e[a]=r[t]}),n=this&&this.__exportStar||function(e,r){for(var t in e)"default"===t||r.hasOwnProperty(t)||a(r,e,t)};Object.defineProperty(r,"__esModule",{value:!0}),n(t(6),r),n(t(10),r),n(t(1),r),n(t(11),r),n(t(12),r),n(t(13),r)},function(e,r,t){"use strict";var a=this&&this.__spreadArrays||function(){for(var e=0,r=0,t=arguments.length;r<t;r++)e+=arguments[r].length;var a=Array(e),n=0;for(r=0;r<t;r++)for(var o=arguments[r],i=0,u=o.length;i<u;i++,n++)a[n]=o[i];return a};Object.defineProperty(r,"__esModule",{value:!0}),r.cleanValue=void 0;var n=t(7),o=t(8),i=t(9),u=t(0);r.cleanValue=function(e){var r=e.value,t=e.groupSeparator,l=void 0===t?",":t,c=e.decimalSeparator,f=void 0===c?".":c,p=e.allowDecimals,d=void 0===p||p,s=e.decimalsLimit,v=void 0===s?2:s,g=e.allowNegativeValue,m=void 0===g||g,b=e.turnOffAbbreviations,y=void 0!==b&&b,h=e.prefix,S=void 0===h?"":h;if("-"===r)return r;var _=y?[]:["k","m","b"],O=new RegExp("^\\d?-"+(S?u.escapeRegExp(S)+"?":"")+"\\d").test(r),x=RegExp("(\\d+)-?"+u.escapeRegExp(S)).exec(r)||[],w=x[0],j=x[1],P=S?r.replace(w,"").concat(j):r,V=o.removeSeparators(P,l),E=i.removeInvalidChars(V,a([l,f],_)),M=E;if(!y){if(_.some((function(e){return e===E.toLowerCase()})))return"";var R=n.parseAbbrValue(E,f);R&&(M=String(R))}var N=O&&m?"-":"";if(String(M).includes(f)){var C=E.split(f),A=C[0],D=C[1],I=v?D.slice(0,v):D;return""+N+A+(d?""+f+I:"")}return""+N+M}},function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.parseAbbrValue=r.abbrValue=void 0;var a=t(0);r.abbrValue=function(e,r,t){if(void 0===r&&(r="."),void 0===t&&(t=10),e>999){var a=(""+e).length,n=Math.pow,o=n(10,t);return a-=a%3,(Math.round(e*o/n(10,a))/o+" kMGTPE"[a/3]).replace(".",r)}return String(e)};var n={k:1e3,m:1e6,b:1e9};r.parseAbbrValue=function(e,r){void 0===r&&(r=".");var t=new RegExp("(\\d+("+a.escapeRegExp(r)+"\\d+)?)([kmb])$","i"),o=e.match(t);if(o){var i=o[1],u=o[3],l=n[u.toLowerCase()];if(i&&l)return Number(i.replace(r,"."))*l}}},function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.removeSeparators=void 0;var a=t(0);r.removeSeparators=function(e,r){void 0===r&&(r=",");var t=new RegExp(a.escapeRegExp(r),"g");return e.replace(t,"")}},function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.removeInvalidChars=void 0;var a=t(0);r.removeInvalidChars=function(e,r){var t=a.escapeRegExp(r.join("")),n=new RegExp("[^\\d"+t+"]","gi");return e.replace(n,"")}},function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.fixedDecimalValue=void 0,r.fixedDecimalValue=function(e,r,t){if(t&&e.length>1){if(e.includes(r)){var a=e.split(r),n=a[0];if((o=a[1]).length>t)return""+n+r+o.slice(0,t)}var o,i=e.length>t?new RegExp("(\\d+)(\\d{"+t+"})"):new RegExp("(\\d)(\\d+)"),u=e.match(i);if(u)return""+(n=u[1])+r+(o=u[2])}return e}},function(e,r,t){"use strict";var a=this&&this.__assign||function(){return(a=Object.assign||function(e){for(var r,t=1,a=arguments.length;t<a;t++)for(var n in r=arguments[t])Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n]);return e}).apply(this,arguments)};Object.defineProperty(r,"__esModule",{value:!0}),r.getLocaleConfig=void 0;var n={currencySymbol:"",groupSeparator:"",decimalSeparator:""};r.getLocaleConfig=function(e){var r=e||{},t=r.locale,o=r.currency;return(t&&o?new Intl.NumberFormat(t,{currency:o,style:"currency"}):new Intl.NumberFormat).formatToParts(1000.1).reduce((function(e,r){return"currency"===r.type?a(a({},e),{currencySymbol:r.value}):"group"===r.type?a(a({},e),{groupSeparator:r.value}):"decimal"===r.type?a(a({},e),{decimalSeparator:r.value}):e}),n)}},function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.isNumber=void 0,r.isNumber=function(e){return RegExp(/\d/,"gi").test(e)}},function(e,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0}),r.padTrimValue=void 0,r.padTrimValue=function(e,r,t){if(void 0===r&&(r="."),!t||""===e||void 0===e)return e;if(!e.match(/\d/g))return"";var a=e.split(r),n=a[0],o=a[1]||"";if(o.length<t)for(;o.length<t;)o+="0";else o=o.slice(0,t);return""+n+r+o}}]); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "react-currency-input-field", | ||
"version": "3.0.0-beta.1", | ||
"version": "3.0.0-beta.2", | ||
"description": "React <input /> component for formatting currency and numbers.", | ||
@@ -15,3 +15,3 @@ "files": [ | ||
"test": "jest --coverage", | ||
"test:watch": "jest --coverage --watch", | ||
"test-ci": "cross-env NODE_ICU_DATA=node_modules/full-icu jest --coverage", | ||
"lint": "tsc && eslint 'src/**/*.{js,ts,tsx}' --max-warnings=0", | ||
@@ -39,3 +39,5 @@ "gh-predeploy": "NODE_ENV='production' webpack --config-name=dev --mode=production", | ||
"number", | ||
"input" | ||
"input", | ||
"intl", | ||
"locale" | ||
], | ||
@@ -57,2 +59,3 @@ "author": "cchanxzy", | ||
"codecov": "^3.7.2", | ||
"cross-env": "^7.0.3", | ||
"enzyme": "^3.11.0", | ||
@@ -64,2 +67,3 @@ "enzyme-adapter-react-16": "^1.15.3", | ||
"eslint-plugin-react": "^7.20.6", | ||
"full-icu": "^1.3.1", | ||
"gh-pages": "^3.1.0", | ||
@@ -66,0 +70,0 @@ "html-webpack-plugin": "^4.3.0", |
@@ -9,6 +9,7 @@ # React Currency Input Field Component | ||
- Prefix option eg. £ or \$ | ||
- Automatically inserts group separator | ||
- Accepts Intl locale config | ||
- Can allow/disallow decimals | ||
- Automatically inserts comma separator | ||
- Only allows valid numbers | ||
- Lightweight and simple | ||
- Written in TypeScript | ||
- Does not use any third party packages | ||
@@ -57,2 +58,4 @@ ## Examples | ||
This can be turned off by passing in `turnOffAbbreviations`. | ||
## Separators | ||
@@ -67,8 +70,3 @@ | ||
<CurrencyInput | ||
id="input-example" | ||
defaultValue="123.456,89" | ||
decimalSeparator="," | ||
groupSeparator="." | ||
/>; | ||
<CurrencyInput decimalSeparator="," groupSeparator="." />; | ||
``` | ||
@@ -80,2 +78,24 @@ | ||
## Intl Locale Config | ||
This component can also accept international locale config to format the currency to locale setting. | ||
Examples: | ||
```javascript | ||
import CurrencyInput from 'react-currency-input-field'; | ||
<CurrencyInput intlConfig={{ locale: 'en-US', currency: 'GBP' }} />; | ||
<CurrencyInput intlConfig={{ locale: 'ja-JP', currency: 'JPY' }} />; | ||
<CurrencyInput intlConfig={{ locale: 'en-IN', currency: 'INR' }} />; | ||
``` | ||
`locale` should be a [BCP 47 language tag](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation), such as "en-US" or "en-IN". | ||
`currency` should be a [ISO 4217 currency code](https://en.wikipedia.org/wiki/ISO_4217), such as "USD" for the US dollar, "EUR" for the euro, or "CNY" for the Chinese RMB. | ||
Any prefix, group separator and decimal separator options passed in will override the default locale settings. | ||
## Fixed Decimal Length | ||
@@ -96,24 +116,25 @@ | ||
| Name | Type | Default | Description | | ||
| -------------------- | ---------- | ------- | ------------------------------------------------------------------------ | | ||
| allowDecimals | `boolean` | `true` | Allow decimals | | ||
| allowNegativeValue | `boolean` | `true` | Allow user to enter negative value | | ||
| className | `string` | | Class names | | ||
| decimalsLimit | `number` | `2` | Limit length of decimals allowed | | ||
| defaultValue | `number` | | Default value | | ||
| value | `number` | | Programmatically set the value | | ||
| disabled | `boolean` | `false` | Disabled | | ||
| fixedDecimalLength | `number` | | Value will always have the specified length of decimals | | ||
| id | `string` | | Component id | | ||
| maxLength | `number` | | Maximum characters the user can enter | | ||
| onChange | `function` | | Handle change in value | | ||
| onBlurValue | `function` | | Handle value onBlur | | ||
| placeholder | `string` | | Placeholder if no value | | ||
| precision | `number` | | Specify decimal precision for padding/trimming | | ||
| prefix | `string` | | Include a prefix eg. £ or \$ | | ||
| step | `number` | | Incremental value change on arrow down and arrow up key press | | ||
| decimalSeparator | `string` | `.` | Separator between integer part and fractional part of value | | ||
| groupSeparator | `string` | `,` | Separator between thousand, million and billion | | ||
| turnOffAbbreviations | `boolean` | `false` | Disable abbreviations eg. 1k > 1,000, 2m > 2,000,000 | | ||
| turnOffSeparators | `boolean` | `false` | Disable auto adding the group separator between values, eg. 1000 > 1,000 | | ||
| Name | Type | Default | Description | | ||
| -------------------- | ---------- | -------------- | ------------------------------------------------------------------------ | | ||
| allowDecimals | `boolean` | `true` | Allow decimals | | ||
| allowNegativeValue | `boolean` | `true` | Allow user to enter negative value | | ||
| className | `string` | | Class names | | ||
| decimalsLimit | `number` | `2` | Limit length of decimals allowed | | ||
| defaultValue | `number` | | Default value | | ||
| value | `number` | | Programmatically set the value | | ||
| disabled | `boolean` | `false` | Disabled | | ||
| fixedDecimalLength | `number` | | Value will always have the specified length of decimals | | ||
| id | `string` | | Component id | | ||
| maxLength | `number` | | Maximum characters the user can enter | | ||
| onChange | `function` | | Handle change in value | | ||
| onBlurValue | `function` | | Handle value onBlur | | ||
| placeholder | `string` | | Placeholder if no value | | ||
| precision | `number` | | Specify decimal precision for padding/trimming | | ||
| prefix | `string` | | Include a prefix eg. £ or \$ | | ||
| step | `number` | | Incremental value change on arrow down and arrow up key press | | ||
| decimalSeparator | `string` | locale default | Separator between integer part and fractional part of value | | ||
| groupSeparator | `string` | locale default | Separator between thousand, million and billion | | ||
| intlConfig | `object` | | International locale config | | ||
| turnOffAbbreviations | `boolean` | `false` | Disable abbreviations eg. 1k > 1,000, 2m > 2,000,000 | | ||
| turnOffSeparators | `boolean` | `false` | Disable auto adding the group separator between values, eg. 1000 > 1,000 | | ||
@@ -120,0 +141,0 @@ ## Format values for display |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75933
262
155
36