Newlines, textfield sets the cursor position appropriately

This commit is contained in:
Pieter Vander Vennet 2020-09-26 01:10:10 +02:00
parent 984d88d01e
commit 223f6191cd
6 changed files with 10 additions and 16 deletions

View file

@ -6,13 +6,11 @@ export default class MultiLingualTextFields extends InputElement<any> {
private _fields: Map<string, TextField> = new Map<string, TextField>(); private _fields: Map<string, TextField> = new Map<string, TextField>();
private readonly _value: UIEventSource<any>; private readonly _value: UIEventSource<any>;
public readonly IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false); public readonly IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false);
constructor(languages: UIEventSource<string[]>, constructor(languages: UIEventSource<string[]>,
textArea: boolean = false, textArea: boolean = false,
value: UIEventSource<Map<string, UIEventSource<string>>> = undefined) { value: UIEventSource<Map<string, UIEventSource<string>>> = undefined) {
super(undefined); super(undefined);
this._value = value ?? new UIEventSource({}); this._value = value ?? new UIEventSource({});
this._value.addCallbackAndRun(latestData => { this._value.addCallbackAndRun(latestData => {
if (typeof (latestData) === "string") { if (typeof (latestData) === "string") {
console.warn("Refusing string for multilingual input", latestData); console.warn("Refusing string for multilingual input", latestData);

View file

@ -3,7 +3,6 @@ import TagInput from "./SingleTagInput";
import {MultiInput} from "./MultiInput"; import {MultiInput} from "./MultiInput";
export class MultiTagInput extends MultiInput<string> { export class MultiTagInput extends MultiInput<string> {
constructor(value: UIEventSource<string[]> = new UIEventSource<string[]>([])) { constructor(value: UIEventSource<string[]> = new UIEventSource<string[]>([])) {
super("Add a new tag", super("Add a new tag",

View file

@ -11,8 +11,7 @@ export class RadioButton<T> extends InputElement<T> {
private readonly value: UIEventSource<T>; private readonly value: UIEventSource<T>;
private readonly _elements: InputElement<T>[] private readonly _elements: InputElement<T>[]
private readonly _selectFirstAsDefault: boolean; private readonly _selectFirstAsDefault: boolean;
constructor(elements: InputElement<T>[], constructor(elements: InputElement<T>[],
selectFirstAsDefault = true) { selectFirstAsDefault = true) {
super(undefined); super(undefined);
@ -20,7 +19,6 @@ export class RadioButton<T> extends InputElement<T> {
this._selectFirstAsDefault = selectFirstAsDefault; this._selectFirstAsDefault = selectFirstAsDefault;
const self = this; const self = this;
this.value = this.value =
UIEventSource.flatten(this._selectedElementIndex.map( UIEventSource.flatten(this._selectedElementIndex.map(
(selectedIndex) => { (selectedIndex) => {

View file

@ -22,7 +22,6 @@ export default class SingleTagInput extends InputElement<string> {
constructor(value: UIEventSource<string> = undefined) { constructor(value: UIEventSource<string> = undefined) {
super(undefined); super(undefined);
this._value = value ?? new UIEventSource<string>(""); this._value = value ?? new UIEventSource<string>("");
this.helpMessage = new VariableUiElement(this._value.map(tagDef => { this.helpMessage = new VariableUiElement(this._value.map(tagDef => {
try { try {
FromJSON.Tag(tagDef, ""); FromJSON.Tag(tagDef, "");

View file

@ -10,7 +10,7 @@ export class TextField extends InputElement<string> {
public readonly IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false); public readonly IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false);
private readonly _isArea: boolean; private readonly _isArea: boolean;
private readonly _textAreaRows: number; private readonly _textAreaRows: number;
constructor(options?: { constructor(options?: {
placeholder?: string | UIElement, placeholder?: string | UIElement,
value?: UIEventSource<string>, value?: UIEventSource<string>,
@ -52,7 +52,6 @@ export class TextField extends InputElement<string> {
field.value = t; field.value = t;
}); });
this.dumbMode = false; this.dumbMode = false;
this.SetClass("deadbeef")
} }
GetValue(): UIEventSource<string> { GetValue(): UIEventSource<string> {
@ -72,18 +71,18 @@ export class TextField extends InputElement<string> {
`</form></span>`; `</form></span>`;
} }
Update() {
console.log("Updating TF")
super.Update();
}
InnerUpdate() { InnerUpdate() {
console.log("Inner Updating TF")
const field = document.getElementById("txt-" + this.id); const field = document.getElementById("txt-" + this.id);
const self = this; const self = this;
field.oninput = () => { field.oninput = () => {
// @ts-ignore
var endDistance = field.value.length - field.selectionEnd;
// @ts-ignore // @ts-ignore
self.value.setData(field.value); self.value.setData(field.value);
// Setting the value might cause the value to be set again. We keep the distance _to the end_ stable, as phone number formatting might cause the start to change
// See https://github.com/pietervdvn/MapComplete/issues/103
self.SetCursorPosition(field.value.length - endDistance);
}; };
if (this.value.data !== undefined && this.value.data !== null) { if (this.value.data !== undefined && this.value.data !== null) {
@ -105,7 +104,7 @@ export class TextField extends InputElement<string> {
} }
public SetCursorPosition(i: number) { public SetCursorPosition(i: number) {
const field = document.getElementById('text-' + this.id); const field = document.getElementById('txt-' + this.id);
if(field === undefined || field === null){ if(field === undefined || field === null){
return; return;
} }
@ -116,6 +115,7 @@ export class TextField extends InputElement<string> {
field.focus(); field.focus();
// @ts-ignore // @ts-ignore
field.setSelectionRange(i, i); field.setSelectionRange(i, i);
} }
IsValid(t: string): boolean { IsValid(t: string): boolean {

View file

@ -90,7 +90,7 @@ export default class ValidatedTextField {
return parsePhoneNumberFromString(str, country?.toUpperCase())?.isValid() ?? false return parsePhoneNumberFromString(str, country?.toUpperCase())?.isValid() ?? false
}, },
(str, country: any) => { (str, country: any) => {
console.log("country formatting", country) console.log("reformatting phone nuber",str, "for locale", country)
return parsePhoneNumberFromString(str, country?.toUpperCase()).formatInternational() return parsePhoneNumberFromString(str, country?.toUpperCase()).formatInternational()
} }
) )