convert to preact js component - "import React from 'react';
import { findDOMNode } from 'react-dom';
import invariant from 'invariant';
import warning from 'warning';
import { setInputSelection, getInputSelection } from './utils/selection';
import parseMask from './parseMask';
import processChange from './processChange';
import { isWindowsPhoneBrowser } from './utils/environment';
import {
formatValue,
getFilledLength,
isFilled,
isEmpty,
getRightEditablePosition,
getStringValue
} from './utils/string';
import { isFunction } from './utils/helpers';
import { defer, cancelDefer } from './utils/defer';
// only check for positive match because it will be false negative
// in case of autofill simulation in tests
//
// input.matches throws an exception if selector isn't supported
try {
if (input.matches(':-webkit-autofill')) {
return true;
}
} catch (e) {}
// if input isn't focused then change event must have been triggered
// either by autofill or event simulation in tests
if (!this.focused) {
return true;
}
// if cursor has moved to the end while previousSelection forbids it
// then it must be autofill
return previousSelection.end < previousValue.length
&&
selection.end === value.length;
}
onChange = (event) => {
const { beforePasteState } = this;
let { previousSelection } = this;
const { beforeMaskedValueChange } = this.props;
let value = this.getInputValue();
let previousValue = this.value;
let selection = this.getSelection();
code
// autofill replaces entire value, ignore old one
// https://github.com/sanniassin/react-input-mask/issues/113
if (this.isInputAutofilled(value, selection, previousValue, previousSelection)) {
previousValue = formatValue(this.maskOptions, '');
previousSelection = { start: 0, end: 0, length: 0 };
}
// set value and selection as if we haven't
// cleared input in onPaste handler
if (beforePasteState) {
previousSelection = beforePasteState.selection;
previousValue = beforePasteState.value;
selection = {
start: previousSelection.start + value.length,
end: previousSelection.start + value.length,
length: 0
};
value = previousValue.slice(0, previousSelection.start) + value + previousValue.slice(previousSelection.end);
this.beforePasteState = null;
}
const changedState = processChange(this.maskOptions, value, selection, previousValue, previousSelection);
const enteredString = changedState.enteredString;
let newSelection = changedState.selection;
let newValue = changedState.value;
if (isFunction(beforeMaskedValueChange)) {
const modifiedValue = beforeMaskedValueChange(
{ value: newValue, selection: newSelection },
{ value: previousValue, selection: previousSelection },
enteredString,
this.getBeforeMaskedValueChangeConfig()
);
newValue = modifiedValue.value;
newSelection = modifiedValue.selection;
}
this.setInputValue(newValue);
if (isFunction(this.props.onChange)) {
this.props.onChange(event);
}
if (this.isWindowsPhoneBrowser) {
this.setSelection(newSelection.start, newSelection.end, { deferred: true });
} else {
this.setSelection(newSelection.start, newSelection.end);
}
onMouseDown = (event) => {
// tiny unintentional mouse movements can break cursor
// position on focus, so we have to restore it in that case
//
// https://github.com/sanniassin/react-input-mask/issues/108
if (!this.focused && document.addEventListener) {
this.mouseDownX = event.clientX;
this.mouseDownY = event.clientY;
this.mouseDownTime = (new Date()).getTime();
onPaste = (event) => {
if (isFunction(this.props.onPaste)) {
this.props.onPaste(event);
}
code
// event.clipboardData might not work in Android browser
// cleaning input to get raw text inside onChange handler
if (!event.defaultPrevented) {
this.beforePasteState = {
value: this.getInputValue(),
selection: this.getSelection()
};
this.setInputValue('');
}
warning(
// parse mask to test against actual mask prop as this.maskOptions
// will be updated later in componentDidUpdate
!restProps.maxLength || !parseMask(mask, maskChar, formatChars).mask,
'react-input-mask: maxLength property shouldn\'t be passed to the masked input. It breaks masking and unnecessary because length is limited by the mask length.'
);
if (children) {
invariant(
isFunction(children),
'react-input-mask: children must be a function'
);
const controlledProps = ['onChange', 'onPaste', 'onMouseDown', 'onFocus', 'onBlur', 'value', 'disabled', 'readOnly'];
const childrenProps = { ...restProps };
controlledProps.forEach((propId) => delete childrenProps[propId]);
inputElement = children(childrenProps);
const conflictProps = controlledProps
.filter((propId) => inputElement.props[propId] != null && inputElement.props[propId] !== restProps[propId]);
invariant(
!conflictProps.length,
`react-input-mask: the following props should be passed to the react-input-mask's component and should not be altered in children's function: ${conflictProps.join(', ')}`
);
warning(
!inputRef,
'react-input-mask: inputRef is ignored when children is passed, attach ref to the children instead'
);
} else {
inputElement = <input ref={this.handleRef} {...restProps} />;
}
const changedProps = {
onFocus: this.onFocus,
onBlur: this.onBlur
};
if (this.maskOptions.mask) {
if (!restProps.disabled && !restProps.readOnly) {
changedProps.onChange = this.onChange;
changedProps.onPaste = this.onPaste;
changedProps.onMouseDown = this.onMouseDown;
}
if (restProps.value != null) {
changedProps.value = this.value;
}
}
inputElement = React.cloneElement(inputElement, changedProps);
return inputElement;
}
}
export default InputElement;"
import { h, Component } from 'preact';
import { findDOMNode } from 'preact-compat';
import invariant from 'invariant';
import warning from 'warning';
import { setInputSelection, getInputSelection } from './utils/selection';
import parseMask from './parseMask';
import processChange from './processChange';
import { isWindowsPhoneBrowser } from './utils/environment';
import {
formatValue,
getFilledLength,
isFilled,
isEmpty,
getRightEditablePosition,
getStringValue
} from './utils/string';
import { isFunction } from './utils/helpers';
import { defer, cancelDefer } from './utils/defer';
// only check for positive match because it will be false negative
// in case of autofill simulation in tests
//
// input.matches throws an exception if selector isn't supported
try {
if (input.matches(':-webkit-autofill')) {
return true;
}
} catch (e) {}
// if input isn't focused then change event must have been triggered
// either by autofill or event simulation in tests
if (!this.focused) {
return true;
}
// if cursor has moved to the end while previousSelection forbids it
// then it must be autofill
return previousSelection.end < previousValue.length
&&
selection.end === value.length;
}
onChange = (event) => {
const { beforePasteState } = this;
let { previousSelection } = this;
const { beforeMaskedValueChange } = this.props;
let value = this.getInputValue();
let previousValue = this.value;
let selection = this.getSelection();
code
// autofill replaces entire value, ignore old one
// https://github.com/sanniassin/react-input-mask/issues/113
if (this.isInputAutofilled(value, selection, previousValue, previousSelection)) {
previousValue = formatValue(this.maskOptions, '');
previousSelection = { start: 0, end: 0, length: 0 };
}
// set value and selection as if we haven't
// cleared input in onPaste handler
if (beforePasteState) {
previousSelection = beforePasteState.selection;
previousValue = beforePasteState.value;
selection = {
start: previousSelection.start + value.length,
end: previousSelection.start + value.length,
length: 0
};
value = previousValue.slice(0, previousSelection.start) + value + previousValue.slice(previousSelection.end);
this.beforePasteState = null;
}
const changedState = processChange(this.maskOptions, value, selection, previousValue, previousSelection);
const enteredString = changedState.enteredString;
let newSelection = changedState.selection;
let newValue = changedState.value;
if (isFunction(beforeMaskedValueChange)) {
const modifiedValue = beforeMaskedValueChange(
{ value: newValue, selection: newSelection },
{ value: previousValue, selection: previousSelection },
enteredString,
this.getBeforeMaskedValueChangeConfig()
);
newValue = modifiedValue.value;
newSelection = modifiedValue.selection;
}
this.setInputValue(newValue);
if (isFunction(this.props.onChange)) {
this.props.onChange(event);
}
if (this.isWindowsPhoneBrowser) {
this.setSelection(newSelection.start, newSelection.end, { deferred: true });
} else {
this.setSelection(newSelection.start, newSelection.end);
}
onMouseDown = (event) => {
// tiny unintentional mouse movements can break cursor
// position on focus, so we have to restore it in that case
//
// https://github.com/sanniassin/react-input-mask/issues/108
if (!this.focused && document.addEventListener) {
this.mouseDownX = event.clientX;
this.mouseDownY = event.clientY;
this.mouseDownTime = (new Date()).getTime();
onPaste = (event) => {
if (isFunction(this.props.onPaste)) {
this.props.onPaste(event);
}
code
// event.clipboardData might not work in Android browser
// cleaning input to get raw text inside onChange handler
if (!event.defaultPrevented) {
this.beforePasteState = {
value: this.getInputValue(),
selection: this.getSelection()
};
this.setInputValue('');
}
warning(
// parse mask to test against actual mask prop as this.maskOptions
// will be updated later in componentDidUpdate
!restProps.maxLength || !parseMask(mask, maskChar, formatChars).mask,
'react-input-mask: maxLength property shouldn\'t be passed to the masked input. It breaks masking and unnecessary because length is limited by the mask length.'
);
if (children) {
invariant(
isFunction(children),
'react-input-mask: children must be a function'
);
const controlledProps = ['onChange', 'onPaste', 'onMouseDown', 'onFocus', 'onBlur', 'value', 'disabled', 'readOnly'];
const childrenProps = { ...restProps };
controlledProps.forEach((propId) => delete childrenProps[propId]);
inputElement = children(childrenProps);
const conflictProps = controlledProps
.filter((propId) => inputElement.props[propId] != null && inputElement.props[propId] !== restProps[propId]);
invariant(
!conflictProps.length,
`react-input-mask: the following props should be passed to the react-input-mask's component and should not be altered in children's function: ${conflictProps.join(', ')}`
);
warning(
!inputRef,
'react-input-mask: inputRef is ignored when children is passed, attach ref to the children instead'
);
} else {
inputElement = <input ref={this.handleRef} {...restProps} />;
}
const changedProps = {
onFocus: this.onFocus,
onBlur: this.onBlur
};
if (this.maskOptions.mask) {
if (!restProps.disabled && !restProps.readOnly) {
changedProps.onChange = this.onChange;
changedProps.onPaste = this.onPaste;
changedProps.onMouseDown = this.onMouseDown;
}
if (restProps.value != null) {
changedProps.value = this.value;
}
}
inputElement = h(inputElement.type, {
...inputElement.props,
...changedProps
});
return inputElement;