• Stars
    star
    102
  • Rank 335,584 (Top 7 %)
  • Language Svelte
  • Created about 5 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Input masking component for Svelte with simple API and rich customization

Mask input with simple API and rich customization.

If you need to create an input for:

  • credit card
  • phone number
  • date
  • birthday
  • numbers
  • Or other custom mask

This project could help you in all this situations!

Take a look at our demos: https://codesandbox.io/s/svelte-input-mask-demo-xurgr

How to use it:

Install it:

npm install --save svelte-input-mask

or if you're using yarn:

yarn add svelte-input-mask

Import MaskInput component:

import MaskInput from "svelte-input-mask/MaskInput.svelte";

Use it (for example for CreditCard):

<MaskInput alwaysShowMask maskChar="_" mask="0000-000000-00000" />

Add event listeners:

<script>
  import MaskInput from 'svelte-input-mask/MaskInput.svelte';

  let mask = '0000-0000-0000-0000';

  const handleChange = ({ detail }) => {
    console.log(detail.inputState.maskedValue); // stores the value of input

    if (detail.inputState.maskedValue.indexOf('34') === 0 || detail.inputState.maskedValue.indexOf('37') === 0) {
      mask = '0000-000000-00000';
      return;
    }

    mask = '0000-0000-0000-0000';
  };
</script>

<MaskInput alwaysShowMask maskChar="_" {mask} on:change={handleChange} />

Congrats! You made the first masked input :)

Checkout more usecases here: https://codesandbox.io/s/romantic-franklin-xurgr

Where to use?

Credit cards:

<MaskInput alwaysShowMask maskChar="_" mask="0000-000000-00000" />

Phones (you still can change prefixes, country code like in credit card example):

<MaskInput
  alwaysShowMask
  mask="+1 (000) 000 - 0000"
  size={20}
  showMask
  maskChar="_"
/>

Dates:

<script>
  import MaskInput from 'svelte-input-mask/MaskInput.svelte';

  let maskString = 'DD.MM.YYYY';
  let mask = '00.00.0000';

  const handleChange = ({ detail }) => {
    const value = detail.inputState.maskedValue;
    if (parseInt(value[6], 10) > 2) {
      maskString = 'DD.MM.YY';
      mask = '00.00.00';
    } else {
      maskString = 'DD.MM.YYYY';
      mask = '00.00.0000';
    }
  };
</script>

<MaskInput alwaysShowMask {maskString} {mask} on:change={handleChange}/>

Numbers:

<script>
  import NumberInput from 'svelte-input-mask/NumberInput.svelte';
</script>

<NumberInput />

Which props it has?

Mask input has next props:

Prop Default value Description
value - The value of the input. Will be processed to masked one. In this case you can control the value of the component
defaultValue - The default value of the input. Will be applied only during the first render
maskString - The mask string to show if there are no filled chars. It's length should be the same as mask. Example: 'DD.MM.YYYY'
maskChar '' In case you don't need a custom string you can define only a definite char for mask. Example: maskChar = '_' and mask = '0000-0000-0000-0000' will give: ____-____-____-____
mask - The mask of the input. Could be a credit card: '0000-0000-0000-0000', date: 00.00.0000 or whatever you want :) Doesn't work if reformat prop is setted
maskFormat regexp The regexp for custom formatting. You may use it if you want to define a specific mask. See example here: https://github.com/xnimorz/masked-input/blob/master/packages/input-core/src/index.ts#L16-L28
alwaysShowMask false Flag to show the mask
showMask false Show mask if there is any data in input
reformat - The function, which defines a custom formatting rules. In case if you can't describe the format only with mask (e.g. numbers). If you use this prop mask prop will be ignored

Svelte mask input pass all props that it doesn't handle right to input html element.

Quick start examples at local machine

git clone [email protected]:xnimorz/svelte-input-mask.git
cd svelte-input-mask/example
yarn install
yarn dev

Requirements:

Svelte should be installed in your project. Check the minimal Svelte version here: https://github.com/xnimorz/svelte-input-mask/blob/master/package.json#L42

More Repositories

1

use-debounce

A debounce hook for react
TypeScript
2,980
star
2

masked-input

Mask input with simple API and rich customization
TypeScript
52
star
3

react-providers

The React Component, which allows you to combine contexts and incapsulate to components using HOC, is built on a top of Context API
TypeScript
23
star
4

browser-extensions-talk-holyjs

JavaScript
21
star
5

on-hotkey

Native JavaScript library to subscribe on keyboard shortcuts, small and without any dependencies
TypeScript
19
star
6

selection-range-enhancer

A project, which enhances abilities of selectionRange. It allows to track changes, works with carets and so on
TypeScript
14
star
7

rprogress

React ajax loader progress bar with clear API
JavaScript
11
star
8

ex-habr

CSS
11
star
9

subscribe-event

The easiest way to subscribe and unsubscribe to browser / node events
JavaScript
8
star
10

simple-expert-system

Простейшая экспертная система
JavaScript
5
star
11

modern-frameworks-dom-invalidation-talk

Доклад о работе с DOM в современных фреймворках
HTML
5
star
12

use-handler

React-hook, that returns handler only once while component mounted. Your reference for a function won't be changed. It's useful for optimizations. Note: you can use useCallback hook, that is already included in react instead of custom useHandler hook. You can see example with useHandler and useCallback hooks here: https://codesandbox.io/s/vjko8kroyl
JavaScript
5
star
13

stackoverflow-example

JavaScript
4
star
14

textArea-selector

textArea-selector
JavaScript
4
star
15

icons-talks

Материалы по докладу <Icon />
JavaScript
4
star
16

ready

Promises/A+, ES6 promises
JavaScript
3
star
17

UIComponents

Библиотека интерфейсов
HTML
3
star
18

frontend-conf-materials

Материалы для frontend-conf — Middlewares are awesome
JavaScript
3
star
19

signal-middleware

Redux signal middleware. A place to store your business logic and async code
JavaScript
3
star
20

getItConf-web-site-performance

Дополнительные материалы по докладу "Сайт тормозит. Что делать?"
2
star
21

vanilla-masked-input

Masked inputs for using without React (Main project at https://github.com/xnimorz/masked-input)
JavaScript
2
star
22

e2

e2 (Emit-Event) is a custom event emitter.
JavaScript
1
star
23

BWar

Game BubblesWar
C++
1
star
24

FrontendConf-2019-materials

Материалы по докладу Hit Points вашего сервиса FrontendConf 2019
1
star
25

rplain

React container for only children rendering. Out of date
JavaScript
1
star