mirror of
https://github.com/garraflavatra/rolens.git
synced 2025-01-19 05:27:57 +00:00
137 lines
3.5 KiB
Svelte
137 lines
3.5 KiB
Svelte
|
<script>
|
||
|
import { numericInputTypes } from '../utils';
|
||
|
import { input } from '../actions';
|
||
|
import Icon from './icon.svelte';
|
||
|
import { ObjectId } from 'bson';
|
||
|
import Datepicker from './datepicker.svelte';
|
||
|
|
||
|
export let column = {};
|
||
|
export let value = undefined;
|
||
|
export let valid = true;
|
||
|
|
||
|
const onValid = () => valid = true;
|
||
|
const onInvalid = () => valid = false;
|
||
|
let objectIdInput;
|
||
|
let showDatepicker;
|
||
|
$: type = column.inputType;
|
||
|
$: mandatory = column.mandatory;
|
||
|
|
||
|
$: if ((value === undefined) && mandatory) {
|
||
|
valid = false;
|
||
|
}
|
||
|
|
||
|
function markInputValid(input) {
|
||
|
input.setCustomValidity('');
|
||
|
input.reportValidity();
|
||
|
input.classList.remove('invalid');
|
||
|
valid = true;
|
||
|
}
|
||
|
|
||
|
function setObjectId(event) {
|
||
|
if (valid) {
|
||
|
value = new ObjectId(event.currentTarget?.value);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function generateObjectId() {
|
||
|
value = new ObjectId();
|
||
|
objectIdInput.value = value.toString();
|
||
|
markInputValid(objectIdInput);
|
||
|
objectIdInput.disabled = true;
|
||
|
}
|
||
|
|
||
|
function editObjectId() {
|
||
|
if (!objectIdInput) {
|
||
|
return;
|
||
|
}
|
||
|
objectIdInput.disabled = false;
|
||
|
objectIdInput.focus();
|
||
|
}
|
||
|
|
||
|
function selectChange() {
|
||
|
if ((value === undefined) && mandatory) {
|
||
|
valid = false;
|
||
|
}
|
||
|
else {
|
||
|
valid = true;
|
||
|
}
|
||
|
}
|
||
|
</script>
|
||
|
|
||
|
<div class="forminput {type}">
|
||
|
<div class="field">
|
||
|
{#if type === 'string'}
|
||
|
<input type="text" bind:value use:input={{ type, onValid, onInvalid, mandatory }} />
|
||
|
{:else if type === 'objectid'}
|
||
|
<input
|
||
|
type="text"
|
||
|
bind:this={objectIdInput}
|
||
|
on:input={setObjectId}
|
||
|
use:input={{ type, onValid, onInvalid, mandatory }}
|
||
|
/>
|
||
|
{:else if numericInputTypes.includes(type)}
|
||
|
<input type="number" bind:value use:input={{ type, onValid, onInvalid, mandatory }} />
|
||
|
{:else if type === 'bool'}
|
||
|
<select bind:value on:change={selectChange}>
|
||
|
<option value={undefined} disabled={mandatory}>Unset</option>
|
||
|
<option value={true}>Yes / true</option>
|
||
|
<option value={false}>No / false</option>
|
||
|
</select>
|
||
|
{:else if type === 'date'}
|
||
|
<input type="text" readonly value={value?.toString() || '...'} on:focus={() => showDatepicker = true} />
|
||
|
{/if}
|
||
|
</div>
|
||
|
|
||
|
<div class="actions">
|
||
|
{#if type === 'objectid'}
|
||
|
{#if objectIdInput?.disabled}
|
||
|
<button class="btn-sm" type="button" title="Edit object id" on:click={editObjectId}>
|
||
|
<Icon name="edit" />
|
||
|
</button>
|
||
|
{/if}
|
||
|
<button class="btn-sm" type="button" title="Generate random object id" on:click={generateObjectId}>
|
||
|
<Icon name="reload" />
|
||
|
</button>
|
||
|
{:else if type === 'date'}
|
||
|
<button class="btn-sm" type="button" title="Edit date" on:click={() => showDatepicker = true}>
|
||
|
<Icon name="edit" />
|
||
|
</button>
|
||
|
<button class="btn-sm" type="button" title="Set date to now" on:click={() => value = new Date()}>
|
||
|
<Icon name="o" />
|
||
|
</button>
|
||
|
{/if}
|
||
|
<button class="btn-sm" type="button" title="Reset field to default value" on:click={() => value = undefined}>
|
||
|
<Icon name="trash" />
|
||
|
</button>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
{#if type === 'date'}
|
||
|
<Datepicker bind:value bind:show={showDatepicker} />
|
||
|
{/if}
|
||
|
|
||
|
<style>
|
||
|
.forminput {
|
||
|
position: relative;
|
||
|
}
|
||
|
|
||
|
.forminput.date input {
|
||
|
cursor: pointer;
|
||
|
}
|
||
|
|
||
|
.actions {
|
||
|
display: none;
|
||
|
position: absolute;
|
||
|
right: 5px;
|
||
|
top: 6px;
|
||
|
background-color: #fff;
|
||
|
}
|
||
|
.actions button:last-child {
|
||
|
border-radius: 2px 6px 6px 2px;
|
||
|
}
|
||
|
|
||
|
.forminput:hover .actions {
|
||
|
display: flex;
|
||
|
}
|
||
|
</style>
|