1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-01 08:31:59 +00:00

fix(web): validation of number input fields (#9789)

This commit is contained in:
Michel Heusschen 2024-05-27 10:19:08 +02:00 committed by GitHub
parent e3d39837d0
commit 298370b7be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 27 additions and 4 deletions

View file

@ -27,6 +27,6 @@
<div class="right"> <div class="right">
<Button {disabled} size="sm" color="gray" on:click={() => dispatch('reset', { default: false })}>Reset</Button> <Button {disabled} size="sm" color="gray" on:click={() => dispatch('reset', { default: false })}>Reset</Button>
<Button {disabled} size="sm" on:click={() => dispatch('save')}>Save</Button> <Button type="submit" {disabled} size="sm" on:click={() => dispatch('save')}>Save</Button>
</div> </div>
</div> </div>

View file

@ -27,4 +27,25 @@ describe('SettingInputField component', () => {
await user.click(document.body); await user.click(document.body);
expect(numberInput.value).toEqual('100'); expect(numberInput.value).toEqual('100');
}); });
it('allows emptying number inputs while editing', async () => {
const { getByRole } = render(SettingInputField, {
props: {
label: 'test-number-input',
inputType: SettingInputFieldType.NUMBER,
value: 5,
},
});
const user = userEvent.setup();
const numberInput = getByRole('spinbutton') as HTMLInputElement;
expect(numberInput.value).toEqual('5');
await user.click(numberInput);
await user.keyboard('{Backspace}');
expect(numberInput.value).toEqual('');
await user.click(document.body);
expect(numberInput.value).toEqual('0');
});
}); });

View file

@ -9,6 +9,7 @@
<script lang="ts"> <script lang="ts">
import { quintOut } from 'svelte/easing'; import { quintOut } from 'svelte/easing';
import type { FormEventHandler } from 'svelte/elements';
import { fly } from 'svelte/transition'; import { fly } from 'svelte/transition';
import PasswordField from '../password-field.svelte'; import PasswordField from '../password-field.svelte';
@ -25,7 +26,9 @@
export let isEdited = false; export let isEdited = false;
export let passwordAutocomplete: string = 'current-password'; export let passwordAutocomplete: string = 'current-password';
const validateInput = () => { const handleChange: FormEventHandler<HTMLInputElement> = (e) => {
value = e.currentTarget.value;
if (inputType === SettingInputFieldType.NUMBER) { if (inputType === SettingInputFieldType.NUMBER) {
let newValue = Number(value) || 0; let newValue = Number(value) || 0;
if (newValue < min) { if (newValue < min) {
@ -77,8 +80,7 @@
{step} {step}
{required} {required}
{value} {value}
on:input={(e) => (value = e.currentTarget.value)} on:change={handleChange}
on:blur={validateInput}
{disabled} {disabled}
{title} {title}
/> />