Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding format on type feature prop #677

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions example/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,26 @@ class App extends React.Component {
<h3>Custom Numeral: add support for custom languages </h3>
<NumericFormat customNumerals={['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']} />
</div>

<div className="example">
<h3>Format on type</h3>
<PatternFormat format="###.###.###-##" formatOnType />
</div>

<div className="example">
<h3>Format phone number on type</h3>
<PatternFormat format="+1 (###) ###-####" formatOnType />
</div>

<div className="example">
<h3>Format postal code on type</h3>
<PatternFormat format="##.###-###" formatOnType />
</div>

<div className="example">
<h3>Format date on type</h3>
<PatternFormat format="##/##/####" formatOnType />
</div>
</div>
);
}
Expand Down
32 changes: 28 additions & 4 deletions src/pattern_format.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,44 @@ import {
} from './utils';
import NumberFormatBase from './number_format_base';

function _getFormattedNumberArray(format: string, patternChar: string, numStr: string, formatOnType: boolean) {
const formatArray = format.split('');
if (!formatOnType) {
return formatArray;
}
let hashCount = 0;
// find last pattern index
const replacePatternIdx = formatArray.findIndex((char) => {
if (char === patternChar) {
hashCount += 1;
}
return hashCount === numStr.length
})

// filter format array until numStr length
return formatArray.filter((_, index) => {
return replacePatternIdx >= 0
? index <= replacePatternIdx
: true
});
}

export function format<BaseType = InputAttributes>(
numStr: string,
props: PatternFormatProps<BaseType>,
) {
const format = props.format as string;
const { allowEmptyFormatting, mask, patternChar = '#' } = props;
const { allowEmptyFormatting, mask, patternChar = '#', formatOnType } = props;

if (numStr === '' && !allowEmptyFormatting) return '';

let hashCount = 0;
const formattedNumberAry = format.split('');
const formattedNumberAry = _getFormattedNumberArray(format, patternChar, numStr, formatOnType);

let hashCount = 0

for (let i = 0, ln = format.length; i < ln; i++) {
if (format[i] === patternChar) {
formattedNumberAry[i] = numStr[hashCount] || getMaskAtIndex(mask, hashCount);
formattedNumberAry[i] = numStr[hashCount] || getMaskAtIndex(mask, hashCount, formatOnType);
hashCount += 1;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export type PatternFormatProps<BaseType = InputAttributes> = NumberFormatProps<
mask?: string | string[];
allowEmptyFormatting?: boolean;
patternChar?: string;
formatOnType?: boolean;
},
BaseType
>;
5 changes: 4 additions & 1 deletion src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,10 @@ export function getDefaultChangeMeta(value: string) {
};
}

export function getMaskAtIndex(mask: string | string[] = ' ', index: number) {
export function getMaskAtIndex(mask: string | string[] = ' ', index: number, formatOnType = false) {
if (formatOnType) {
return '';
}
if (typeof mask === 'string') {
return mask;
}
Expand Down