-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): coordinate input for asset location (#11291)
- Loading branch information
1 parent
8725656
commit 7d3db11
Showing
5 changed files
with
126 additions
and
8 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
web/src/lib/components/shared-components/__test__/number-range-input.spec.ts
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,50 @@ | ||
import NumberRangeInput from '$lib/components/shared-components/number-range-input.svelte'; | ||
import { act, render, type RenderResult } from '@testing-library/svelte'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
describe('NumberRangeInput component', () => { | ||
const user = userEvent.setup(); | ||
let sut: RenderResult<NumberRangeInput>; | ||
let input: HTMLInputElement; | ||
|
||
beforeEach(() => { | ||
sut = render(NumberRangeInput, { id: '', min: -90, max: 90, onInput: () => {} }); | ||
input = sut.getByRole('spinbutton') as HTMLInputElement; | ||
}); | ||
|
||
it('updates value', async () => { | ||
expect(input.value).toBe(''); | ||
await act(() => sut.component.$set({ value: 10 })); | ||
expect(input.value).toBe('10'); | ||
}); | ||
|
||
it('restricts minimum value', async () => { | ||
await user.type(input, '-91'); | ||
expect(input.value).toBe('-90'); | ||
}); | ||
|
||
it('restricts maximum value', async () => { | ||
await user.type(input, '09990'); | ||
expect(input.value).toBe('90'); | ||
}); | ||
|
||
it('allows entering negative numbers', async () => { | ||
await user.type(input, '-10'); | ||
expect(input.value).toBe('-10'); | ||
}); | ||
|
||
it('allows entering zero', async () => { | ||
await user.type(input, '0'); | ||
expect(input.value).toBe('0'); | ||
}); | ||
|
||
it('allows entering decimal numbers', async () => { | ||
await user.type(input, '-0.09001'); | ||
expect(input.value).toBe('-0.09001'); | ||
}); | ||
|
||
it('ignores text input', async () => { | ||
await user.type(input, 'test'); | ||
expect(input.value).toBe(''); | ||
}); | ||
}); |
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
27 changes: 27 additions & 0 deletions
27
web/src/lib/components/shared-components/coordinates-input.svelte
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,27 @@ | ||
<script lang="ts"> | ||
import NumberRangeInput from '$lib/components/shared-components/number-range-input.svelte'; | ||
import { generateId } from '$lib/utils/generate-id'; | ||
import { t } from 'svelte-i18n'; | ||
export let lat: number | null | undefined = undefined; | ||
export let lng: number | null | undefined = undefined; | ||
export let onUpdate: (lat: number, lng: number) => void; | ||
const id = generateId(); | ||
const onInput = () => { | ||
if (lat != null && lng != null) { | ||
onUpdate(lat, lng); | ||
} | ||
}; | ||
</script> | ||
|
||
<div> | ||
<label class="immich-form-label" for="latitude-input-{id}">{$t('latitude')}</label> | ||
<NumberRangeInput id="latitude-input-{id}" min={-90} max={90} {onInput} bind:value={lat} /> | ||
</div> | ||
|
||
<div> | ||
<label class="immich-form-label" for="longitude-input-{id}">{$t('longitude')}</label> | ||
<NumberRangeInput id="longitude-input-{id}" min={-180} max={180} {onInput} bind:value={lng} /> | ||
</div> |
28 changes: 28 additions & 0 deletions
28
web/src/lib/components/shared-components/number-range-input.svelte
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,28 @@ | ||
<script lang="ts"> | ||
import { clamp } from 'lodash-es'; | ||
export let id: string; | ||
export let min: number; | ||
export let max: number; | ||
export let step: number | string = 'any'; | ||
export let required = true; | ||
export let value: number | null = null; | ||
export let onInput: (value: number | null) => void; | ||
</script> | ||
|
||
<input | ||
type="number" | ||
class="immich-form-input w-full" | ||
{id} | ||
{min} | ||
{max} | ||
{step} | ||
{required} | ||
bind:value | ||
on:input={() => { | ||
if (value !== null && (value < min || value > max)) { | ||
value = clamp(value, min, max); | ||
} | ||
onInput(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