Error messaging for invalid options in editor
This commit is contained in:
parent
9ac34940f6
commit
aaf491245b
|
@ -6,8 +6,7 @@
|
||||||
|
|
||||||
export let title
|
export let title
|
||||||
export let fillWidth
|
export let fillWidth
|
||||||
|
export let visible = false
|
||||||
let visible = false
|
|
||||||
|
|
||||||
export function show() {
|
export function show() {
|
||||||
if (visible) {
|
if (visible) {
|
||||||
|
|
|
@ -7,22 +7,57 @@
|
||||||
|
|
||||||
export let value
|
export let value
|
||||||
|
|
||||||
|
let drawerVisible = false
|
||||||
let drawer
|
let drawer
|
||||||
|
let valid = true
|
||||||
let tempValue = value || []
|
let tempValue = value || []
|
||||||
|
|
||||||
const saveFilter = async () => {
|
const saveFilter = async () => {
|
||||||
// Filter out incomplete options
|
// Filter out incomplete options, but if all are incomplete then show error
|
||||||
tempValue = tempValue.filter(option => option.value && option.label)
|
let filteredOptions = tempValue.filter(
|
||||||
dispatch("change", tempValue)
|
option => option.value && option.label
|
||||||
drawer.hide()
|
)
|
||||||
|
if (filteredOptions.length > 0 || tempValue.length === 0) {
|
||||||
|
tempValue = filteredOptions
|
||||||
|
valid = true
|
||||||
|
dispatch("change", tempValue)
|
||||||
|
drawer.hide()
|
||||||
|
} else {
|
||||||
|
valid = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//If the drawer is hidden or error corrected, reset the validation
|
||||||
|
$: {
|
||||||
|
if (
|
||||||
|
!drawerVisible ||
|
||||||
|
tempValue.some(
|
||||||
|
option => option.label?.length > 0 && option.value?.length > 0
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
valid = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<ActionButton on:click={drawer.show}>Define Options</ActionButton>
|
<ActionButton on:click={drawer.show}>Define Options</ActionButton>
|
||||||
<Drawer bind:this={drawer} title="Options">
|
<Drawer bind:this={drawer} bind:visible={drawerVisible} title="Options">
|
||||||
<svelte:fragment slot="description">
|
<svelte:fragment slot="description">
|
||||||
Define the options for this picker.
|
{#if !valid}
|
||||||
|
<span class="syntax-error"
|
||||||
|
>You must provide option labels and values.</span
|
||||||
|
>
|
||||||
|
{:else}
|
||||||
|
Define the options for this picker.
|
||||||
|
{/if}
|
||||||
</svelte:fragment>
|
</svelte:fragment>
|
||||||
<Button cta slot="buttons" on:click={saveFilter}>Save</Button>
|
<Button cta slot="buttons" on:click={saveFilter}>Save</Button>
|
||||||
<OptionsDrawer bind:options={tempValue} slot="body" />
|
<OptionsDrawer bind:options={tempValue} slot="body" />
|
||||||
</Drawer>
|
</Drawer>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.syntax-error {
|
||||||
|
padding-top: var(--spacing-m);
|
||||||
|
color: var(--red);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
Loading…
Reference in New Issue