Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cursor position in FormattedInput #1257

Merged
merged 8 commits into from
Apr 14, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added regx for input is number
  • Loading branch information
priyang12 committed Mar 31, 2022
commit 4fc81ee0b84ceb0a1f75f62c7f919be12d6b6f64
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script lang="ts">
import Diff from 'fast-diff';
import type {diff} from 'fast-diff';
import TextInput from '@mathesar-component-library-dir/text-input/TextInput.svelte';
import { getOutcomeOfBeforeInputEvent } from '@mathesar-component-library-dir/common/utils';
import type { InputFormatter, ParseResult } from './InputFormatter';

type T = $$Generic;

export let formatter: InputFormatter<T>;
Expand Down Expand Up @@ -77,10 +77,10 @@
) {
if (element) {
element.value = intermediateDisplay;
const diff = Diff(userInput, intermediateDisplay, cursorPosition);
const diff = Diff(userInput, intermediateDisplay);
// Find New Cursor Position using diff and cursorPosition
let newCursorPosition = 0;
diff.forEach((part: diff[]) => {
diff.forEach((part: any[]) => {
if (part[0] === -1) {
newCursorPosition -= 1;
} else if (part[0] === 1) {
Expand All @@ -106,12 +106,14 @@
parseResult = formatter.parse(userInput);
parentValue = parseResult.value;
childText = parseResult.intermediateDisplay;

setCursorToPostion(
userInput,
cursorPosition,
parseResult.intermediateDisplay,
);
// get last character of userInput
// Regex for finding number
const regex1 = RegExp('[0-9]');
const lastCharacter = userInput[userInput.length - 1];
// set cursor position if last character is number
if (regex1.exec(lastCharacter)) {
setCursorToPostion(userInput, cursorPosition, childText);
}
} catch (error) {
onParseError({ userInput, error });
}
Expand Down