1.1.1 • Published 3 years ago

tformat.js v1.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

npm npm

tformat.js

Tformat.js is a lightweight library for number formatting (credit card and phone numbers, for example)

Examples can be found here

Table of contents

  1. Usage
  2. React
  3. Constructor options

Usage

Download tformat.js via npm

npm i tformat.js

or directly from the repo.

Then import it

<script src="tformat.min.js"></script>

To apply a tformat to input element with id inputField, create a new class by passing element id or HTMLInputElement

let tformat = new TemplateFormatter('inputField', { 
  template: '123 xxx xxx xxx',
  createHiddenInput: true,
  showPrefixOnFocus: true,
  prefixes: ["456 "]
});

Where character x in template is any number

Template can be with multiple prefixes. In example above, template contains two prefixes: 123 and 456

Tformat can also be initialized with a HTMLInputElement

let tformat = new TemplateFormatter(document.getElementById('inputField'), { 
    ...

React

You can also use tformat in React

import React, { useState } from 'react';
import { TFReact } from 'tformat.js';

const MyComponent = () => {
    const [formattedValue, setFormattedValue] = useState('');
    const [unformattedValue, setUnformattedValue] = useState('');

    return (
        <TFReact
            template="+1 xxx xxx"
            showPrefixOnFocus={true}
            onFormatted={(val, rawVal) => { setFormattedValue(val); setUnformattedValue(rawVal); }} />
    )
}

React component TFReact comes with npm package

Constructor options

Constructor has two parameters

new TemplateFormatter(input, props)

First parameter of constructor is an element that need to be formated. It can be an id of element as string or input itself as HTMLInputElement

Additional options goes in second parameter as one object. Possible object properties listed below

NameTypeMandatoryDefaultDescription
templatestringYesTemplate for value formatting
prefixesstring[]NoPossible template prefixes. If user enters first character from prefixes then this prefix will be used in input, otherwise template prefix will be used
showPrefixOnFocusbooleanNofalseIf true then template prefix will be shown after user clicks on input
createHiddenInputbooleanNofalseIf true then hidden clone of input will be created, that contains only numbers. It can be used in form submitting
hidePrefixOnBlurbooleanNotrueIf value input equals template prefix then value will be cleared
showFullTemplatebooleanNofalseIf true then full template will be shown. See examples
emptySpaceCharstringNo' 'Used as placeholder for x characters if showFullTemplate is true

Properties

template

string Template for number formatting. Template can be reassigned.

prefixes

string[]

All possible prefixes of template.

1.1.1

3 years ago

1.1.0

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.3

4 years ago

1.0.0

4 years ago