-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract anchor input component (#907)
* Extract anchor input component This extracts the anchor input component (with input filter) from the authenticate page, which allows reuse in both the login and anchor prompt pages. Also removes unnecessary anchor input CSS. * 🤖 Selenium screenshots auto-update Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: David Aerne <meodai@gmail.com>
- Loading branch information
1 parent
08ca6cf
commit 048541c
Showing
7 changed files
with
134 additions
and
142 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { html } from "lit"; | ||
import { createRef, ref } from "lit/directives/ref.js"; | ||
import { TemplateRef } from "../utils/templateRef"; | ||
|
||
/** A component for inputting an anchor number */ | ||
export const mkAnchorInput = ( | ||
inputId: string, | ||
userNumber?: bigint | ||
): TemplateRef<{ userNumberInput: HTMLInputElement }> => { | ||
const divRef = createRef(); | ||
const userNumberInput = createRef(); | ||
|
||
// How we react on unexpected (i.e. non-digit) input | ||
const onBadInput = () => { | ||
const div = divRef.value; | ||
if (div !== undefined && !div.classList.contains("flash-error")) { | ||
div.classList.add("flash-error"); | ||
setTimeout(() => div.classList.remove("flash-error"), 2000); | ||
} | ||
}; | ||
|
||
const template = html` <div | ||
${ref(divRef)} | ||
class="l-stack c-input c-input--vip c-input--anchor" | ||
> | ||
<input | ||
${ref(userNumberInput)} | ||
type="text" | ||
id="${inputId}" | ||
placeholder="Enter anchor" | ||
value="${userNumber !== undefined ? userNumber : ""}" | ||
style="width: 100%;" | ||
@input=${inputFilter(isDigits, onBadInput)} | ||
@keydown=${inputFilter(isDigits, onBadInput)} | ||
@keyup=${inputFilter(isDigits, onBadInput)} | ||
@mousedown=${inputFilter(isDigits, onBadInput)} | ||
@mouseup=${inputFilter(isDigits, onBadInput)} | ||
@select=${inputFilter(isDigits, onBadInput)} | ||
@contextmenu=${inputFilter(isDigits, onBadInput)} | ||
@drop=${inputFilter(isDigits, onBadInput)} | ||
@focusout=${inputFilter(isDigits, onBadInput)} | ||
/> | ||
<p | ||
id="invalidAnchorMessage" | ||
class="anchor-error-message is-hidden t-paragraph t-strong" | ||
> | ||
The Identity Anchor is not valid. Please try again. | ||
</p> | ||
</div>`; | ||
|
||
return { template, refs: { userNumberInput } }; | ||
}; | ||
|
||
const isDigits = (c: string) => /^\d*\.?\d*$/.test(c); | ||
|
||
/* Adds a filter to the input that only allows the given regex. | ||
* For more info see https://stackoverflow.com/questions/469357/html-text-input-allow-only-numeric-input | ||
*/ | ||
const inputFilter = (inputFilter: (c: string) => boolean, onBad: () => void) => | ||
function ( | ||
this: (HTMLInputElement | HTMLTextAreaElement) & { | ||
oldValue: string; | ||
oldSelectionStart: number | null; | ||
oldSelectionEnd: number | null; | ||
} | ||
) { | ||
if (inputFilter(this.value)) { | ||
this.oldValue = this.value; | ||
this.oldSelectionStart = this.selectionStart; | ||
this.oldSelectionEnd = this.selectionEnd; | ||
} else { | ||
onBad(); | ||
|
||
if (Object.prototype.hasOwnProperty.call(this, "oldValue")) { | ||
this.value = this.oldValue; | ||
if (this.oldSelectionStart !== null && this.oldSelectionEnd !== null) { | ||
this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd); | ||
} | ||
} else { | ||
this.value = ""; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters