convert to preact js functional component - “class InputElement extends React.Component…
- User
convert to preact js functional component - “class InputElement extends React.Component { focused = false mounted = false previousSelection = null selectionDeferId = null saveSelectionLoopDeferId = null
constructor(props) { super(props);
const { mask, maskChar, formatChars, alwaysShowMask, beforeMaskedValueChange } = props; let { defaultValue, value } = props;
this.maskOptions = parseMask(mask, maskChar, formatChars);
if (defaultValue == null) { defaultValue = ‘’; } if (value == null) { value = defaultValue; }
let newValue = getStringValue(value);
if (this.maskOptions.mask && (alwaysShowMask || newValue)) { newValue = formatValue(this.maskOptions, newValue);
if (isFunction(beforeMaskedValueChange)) { let oldValue = props.value; if (props.value == null) { oldValue = defaultValue; } oldValue = getStringValue(oldValue);
const modifiedValue = beforeMaskedValueChange( { value: newValue, selection: null }, { value: oldValue, selection: null }, null, this.getBeforeMaskedValueChangeConfig() );
newValue = modifiedValue.value; } }
this.value = newValue; }
componentDidMount() { this.mounted = true;
// workaround for react-test-renderer // https://github.com/sanniassin/react-input-mask/issues/147 if (!this.getInputDOMNode()) { return; }
this.isWindowsPhoneBrowser = isWindowsPhoneBrowser();
if (this.maskOptions.mask && this.getInputValue() !== this.value) { this.setInputValue(this.value); } } }”
- Assistant
import { useState, useEffect } from 'preact/hooks';
const InputElement = (props) => { const [focused, setFocused] = useState(false); const [mounted, setMounted] = useState(false); const [previousSelection, setPreviousSelection] = useState(null); const [selectionDeferId, setSelectionDeferId] = useState(null); const [saveSelectionLoopDeferId, setSaveSelectionLoopDeferId] = useState(null);
const { mask, maskChar, formatChars, alwaysShowMask, beforeMaskedValueChange } = props; let { defaultValue, value } = props;
const maskOptions = parseMask(mask, maskChar, formatChars);
if (defaultValue == null) { defaultValue = ''; } if (value == null) { value = defaultValue; }
let newValue = getStringValue(value);