Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0125· OpenAIAi2 WildChat-4.8M (AI2)

convert to preact js functional component -"class InputElement extends React.Component {…

  1. 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);
    }
    

    }

    componentDidUpdate() { const { previousSelection } = this; const { beforeMaskedValueChange, alwaysShowMask, mask, maskChar, formatChars } = this.props; const previousMaskOptions = this.maskOptions; const showEmpty = alwaysShowMask || this.isFocused(); const hasValue = this.props.value != null; let newValue = hasValue ? getStringValue(this.props.value) : this.value; let cursorPosition = previousSelection ? previousSelection.start : null;

    this.maskOptions = parseMask(mask, maskChar, formatChars);
    
    if (!this.maskOptions.mask) {
      if (previousMaskOptions.mask) {
        this.stopSaveSelectionLoop();
    
        // render depends on this.maskOptions and this.value,
        // call forceUpdate to keep it in sync
        this.forceUpdate();
      }
      return;
    } else if (!previousMaskOptions.mask && this.isFocused()) {
      this.runSaveSelectionLoop();
    }
    
    const isMaskChanged = this.maskOptions.mask && this.maskOptions.mask !== previousMaskOptions.mask;
    
    if (!previousMaskOptions.mask && !hasValue) {
      newValue = this.getInputValue();
    }
    
    if (isMaskChanged || (this.maskOptions.mask && (newValue || showEmpty))) {
      newValue = formatValue(this.maskOptions, newValue);
    }
    
    if (isMaskChanged) {
      const filledLength = getFilledLength(this.maskOptions, newValue);
      if (cursorPosition === null || filledLength < cursorPosition) {
        if (isFilled(this.maskOptions, newValue)) {
          cursorPosition = filledLength;
        } else {
          cursorPosition = getRightEditablePosition(this.maskOptions, filledLength);
        }
      }
    }
    
    if (this.maskOptions.mask && isEmpty(this.maskOptions, newValue) && !showEmpty && (!hasValue || !this.props.value)) {
      newValue = '';
    }
    
    let newSelection = { start: cursorPosition, end: cursorPosition };
    
    if (isFunction(beforeMaskedValueChange)) {
      const modifiedValue = beforeMaskedValueChange(
        { value: newValue, selection: newSelection },
        { value: this.value, selection: this.previousSelection },
        null,
        this.getBeforeMaskedValueChangeConfig()
      );
      newValue = modifiedValue.value;
      newSelection = modifiedValue.selection;
    }
    
    this.value = newValue;
    const isValueChanged = this.getInputValue() !== this.value;
    
    // render depends on this.maskOptions and this.value,
    // call forceUpdate to keep it in sync
    if (isValueChanged) {
      this.setInputValue(this.value);
      this.forceUpdate();
    } else if (isMaskChanged) {
      this.forceUpdate();
    }
    
    let isSelectionChanged = false;
    if (newSelection.start != null && newSelection.end != null) {
      isSelectionChanged = !previousSelection
                           ||
                           previousSelection.start !== newSelection.start
                           ||
                           previousSelection.end !== newSelection.end;
    }
    
    if (isSelectionChanged || isValueChanged) {
      this.setSelection(newSelection.start, newSelection.end);
    }
    

    }"

  2. Assistant
    import { useEffect, useState } from 'preact/hooks';
    
    const InputElement = (props) => {
      let focused = false;
      let mounted = false;
      let previousSelection = null;
      let selectionDeferId = null;
      let saveSelectionLoopDeferId = null;
      let value = '';
    
      const [maskOptions, setMaskOptions] = useState(null);
      const [isWindowsPhoneBrowser, setIsWindowsPhoneBrowser] = useState(false);
    
      useEffect(() => {
        const { mask, maskChar, formatChars, alwaysShowMask, beforeMaskedValueChange } = props;
        let { defaultValue } = props;
    
        setMaskOptions(parseMask(mask, maskChar, formatChars));
    
        if (defaultValue == null) {
          defaultValue = '';
        }
    
        const newValue = getStringValue(props.value || defaultValue);
    
        if (maskOptions.mask && (alwaysShowMask || newValue)) {
          const formattedValue = formatValue(maskOptions, newValue);
    
          if (isFunction(beforeMaskedValueChange)) {
            const oldValue = props.value || defaultValue;
            const modifiedValue = beforeMaskedValueChange(
              { value: formattedValue, selection: null },
              { value: getStringValue(oldValue), selection: null },
              null,
              getBeforeMaskedValueChangeConfig()
            );
    
            value = modifiedValue.value;
          } else {
            value = formattedValue;
          }
        } else {
          value = newValue;
        }
    
        mounted = true;
        setIsWindowsPhoneBrowser(isWindowsPhoneBrowser());
    
        if (maskOptions.mask && getInputValue() !== value) {
          setInputValue(value);
        }
      }, []);
    
      useEffect(() => {
        const { beforeMaskedValueChange, alwaysShowMask, mask, maskChar, formatChars } = props;
        const previousMaskOptions = maskOptions;
        const showEmpty = alwaysShowMask || isFocused();
        const hasValue = props.value != null;
        let newValue = hasValue ? getStringValue(props.value) : value;
        let cursorPosition = previousSelection ? previousSelection.start : null;
    
        setMaskOptions(parseMask(mask, maskChar, formatChars));
    
        if (!maskOptions.mask) {
          if (previousMaskOptions.mask) {
            stopSaveSelectionLoop();
          }
          
          forceUpdate();
          return;
        } else if (!previousMaskOptions.mask && isFocused()) {
          runSaveSelectionLoop();
        }
    
        const isMaskChanged = maskOptions.mask && maskOptions.mask !== previousMaskOptions.mask;
    
        if (!previousMaskOptions.mask && !hasValue) {
          newValue = getInputValue();
        }
    
        if (isMaskChanged || (maskOptions.mask && (newValue || showEmpty))) {
          newValue = formatValue(maskOptions, newValue);
        }
    
        if (isMaskChanged) {
          const filledLength = getFilledLength(maskOptions, newValue);
          if (cursorPosition === null || filledLength < cursorPosition) {
            if (isFilled(maskOptions, newValue)) {
              cursorPosition = filledLength;
            } else {
              cursorPosition = getRightEditablePosition(maskOptions, filledLength);
            }
          }
        }
    
        if (maskOptions.mask && isEmpty(maskOptions, newValue) && !showEmpty && (!hasValue || !props.value)) {
          newValue = '';
        }
    
        let newSelection = { start: cursorPosition, end: cursorPosition };
    
        if (isFunction(beforeMaskedValueChange)) {
          const modifiedValue = beforeMaskedValueChange(
            { value: newValue, selection: newSelection },
            { value: value, selection: previousSelection },
            null,
            getBeforeMaskedValueChangeConfig()
          );
          newValue = modifiedValue.value;
          newSelection = modifiedValue.selection;
        }
    
        value = newValue;
    
        if (getInputValue() !== value) {
          setInputValue(value);
          forceUpdate();
        } else if (isMaskChanged) {
          forceUpdate();
        }
    
        let isSelectionChanged = false;
        if (newSelection.start != null && newSelection.end != null) {
          isSelectionChanged = !previousSelection ||
            previousSelection.start !== newSelection.start ||
            previousSelection.end !== newSelection.end;
        }
    
        if (isSelectionChanged) {
          setSelection(newSelection.start, newSelection.end);
        }
      });
    
      return null;
    };