Add checkbox component and add error handling to fancy form fields
This commit is contained in:
parent
03810e5b8a
commit
8939ecda03
|
@ -22,6 +22,7 @@
|
||||||
<style>
|
<style>
|
||||||
img {
|
img {
|
||||||
width: 22px;
|
width: 22px;
|
||||||
|
margin-right: 6px;
|
||||||
}
|
}
|
||||||
div {
|
div {
|
||||||
font-size: var(--font-size-l);
|
font-size: var(--font-size-l);
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
<script>
|
||||||
|
import { createEventDispatcher, getContext, onMount } from "svelte"
|
||||||
|
import FancyField from "./FancyField.svelte"
|
||||||
|
import Checkbox from "../Form/Core/Checkbox.svelte"
|
||||||
|
|
||||||
|
export let value
|
||||||
|
export let text
|
||||||
|
export let disabled = false
|
||||||
|
export let error = null
|
||||||
|
export let validate = null
|
||||||
|
|
||||||
|
const dispatch = createEventDispatcher()
|
||||||
|
const formContext = getContext("fancy-form")
|
||||||
|
const id = Math.random()
|
||||||
|
|
||||||
|
const onChange = () => {
|
||||||
|
const newValue = !value
|
||||||
|
dispatch("change", newValue)
|
||||||
|
value = newValue
|
||||||
|
if (validate) {
|
||||||
|
error = validate(newValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const API = {
|
||||||
|
validate: () => {
|
||||||
|
if (validate) {
|
||||||
|
error = validate(value)
|
||||||
|
}
|
||||||
|
return !error
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
if (formContext) {
|
||||||
|
formContext.registerField(id, API)
|
||||||
|
}
|
||||||
|
return () => {
|
||||||
|
if (formContext) {
|
||||||
|
formContext.unregisterField(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<FancyField {error} {disabled} clickable on:click={onChange}>
|
||||||
|
<span>
|
||||||
|
<Checkbox {disabled} {value} />
|
||||||
|
</span>
|
||||||
|
{#if text}
|
||||||
|
{text}
|
||||||
|
{/if}
|
||||||
|
<div>
|
||||||
|
<slot />
|
||||||
|
</div>
|
||||||
|
</FancyField>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
span {
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
</style>
|
|
@ -1,4 +1,6 @@
|
||||||
<script>
|
<script>
|
||||||
|
import Icon from "../Icon/Icon.svelte"
|
||||||
|
|
||||||
export let disabled = false
|
export let disabled = false
|
||||||
export let error = null
|
export let error = null
|
||||||
export let focused = false
|
export let focused = false
|
||||||
|
@ -13,7 +15,14 @@
|
||||||
class:clickable
|
class:clickable
|
||||||
>
|
>
|
||||||
<div class="content" on:click>
|
<div class="content" on:click>
|
||||||
<slot />
|
<div class="field">
|
||||||
|
<slot />
|
||||||
|
</div>
|
||||||
|
{#if error}
|
||||||
|
<div class="error-icon">
|
||||||
|
<Icon name="Alert" />
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
{#if error}
|
{#if error}
|
||||||
<div class="error-message">
|
<div class="error-message">
|
||||||
|
@ -53,14 +62,20 @@
|
||||||
pointer-events: none;
|
pointer-events: none;
|
||||||
}
|
}
|
||||||
.content {
|
.content {
|
||||||
|
position: relative;
|
||||||
height: 64px;
|
height: 64px;
|
||||||
padding: 0 16px;
|
padding: 0 16px;
|
||||||
|
}
|
||||||
|
.content,
|
||||||
|
.field {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: row;
|
flex-direction: row;
|
||||||
justify-content: flex-start;
|
justify-content: flex-start;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 16px;
|
gap: 10px;
|
||||||
position: relative;
|
}
|
||||||
|
.field {
|
||||||
|
flex: 1 1 auto;
|
||||||
}
|
}
|
||||||
.error-message {
|
.error-message {
|
||||||
background: var(--spectrum-global-color-red-400);
|
background: var(--spectrum-global-color-red-400);
|
||||||
|
@ -69,4 +84,10 @@
|
||||||
padding: 6px 16px;
|
padding: 6px 16px;
|
||||||
font-weight: 500;
|
font-weight: 500;
|
||||||
}
|
}
|
||||||
|
.error-icon {
|
||||||
|
flex: 0 0 auto;
|
||||||
|
}
|
||||||
|
.error-icon :global(.spectrum-Icon) {
|
||||||
|
fill: var(--spectrum-global-color-red-400);
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
<script>
|
<script>
|
||||||
import { createEventDispatcher, getContext, onMount } from "svelte"
|
import { createEventDispatcher, getContext, onMount } from "svelte"
|
||||||
import FancyField from "./FancyField.svelte"
|
import FancyField from "./FancyField.svelte"
|
||||||
|
import Icon from "../Icon/Icon.svelte"
|
||||||
|
|
||||||
export let label
|
export let label
|
||||||
export let value
|
export let value
|
||||||
|
@ -29,8 +30,6 @@
|
||||||
validate: () => {
|
validate: () => {
|
||||||
if (validate) {
|
if (validate) {
|
||||||
error = validate(value)
|
error = validate(value)
|
||||||
} else {
|
|
||||||
error = null
|
|
||||||
}
|
}
|
||||||
return !error
|
return !error
|
||||||
},
|
},
|
||||||
|
@ -69,25 +68,25 @@
|
||||||
.label {
|
.label {
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
font-weight: 500;
|
font-weight: 500;
|
||||||
transform: translateY(-12px);
|
transform: translateY(calc(-50% - 12px));
|
||||||
position: absolute;
|
position: absolute;
|
||||||
|
top: 50%;
|
||||||
color: var(--spectrum-global-color-gray-600);
|
color: var(--spectrum-global-color-gray-600);
|
||||||
transition: font-size 130ms ease-out, transform 130ms ease-out;
|
transition: font-size 130ms ease-out, transform 130ms ease-out;
|
||||||
}
|
}
|
||||||
.label.placeholder {
|
.label.placeholder {
|
||||||
font-size: 15px;
|
font-size: 15px;
|
||||||
transform: translateY(0);
|
transform: translateY(-50%);
|
||||||
}
|
}
|
||||||
input {
|
input {
|
||||||
z-index: 1;
|
width: 100%;
|
||||||
|
transition: transform 130ms ease-out;
|
||||||
|
transform: translateY(9px);
|
||||||
background: transparent;
|
background: transparent;
|
||||||
font-size: 15px;
|
font-size: 15px;
|
||||||
color: var(--spectrum-global-color-gray-900);
|
color: var(--spectrum-global-color-gray-900);
|
||||||
outline: none;
|
outline: none;
|
||||||
border: none;
|
border: none;
|
||||||
transition: transform 130ms ease-out;
|
|
||||||
width: 100%;
|
|
||||||
transform: translateY(9px);
|
|
||||||
}
|
}
|
||||||
input.placeholder {
|
input.placeholder {
|
||||||
transform: translateY(0);
|
transform: translateY(0);
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
export { default as FancyInput } from "./FancyInput.svelte"
|
export { default as FancyInput } from "./FancyInput.svelte"
|
||||||
|
export { default as FancyCheckbox } from "./FancyCheckbox.svelte"
|
||||||
export { default as FancyButton } from "./FancyButton.svelte"
|
export { default as FancyButton } from "./FancyButton.svelte"
|
||||||
export { default as FancyForm } from "./FancyForm.svelte"
|
export { default as FancyForm } from "./FancyForm.svelte"
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
<script>
|
<script>
|
||||||
import "@spectrum-css/link/dist/index-vars.css"
|
import "@spectrum-css/link/dist/index-vars.css"
|
||||||
|
import { createEventDispatcher } from "svelte"
|
||||||
|
|
||||||
export let href = "#"
|
export let href = "#"
|
||||||
export let size = "M"
|
export let size = "M"
|
||||||
|
@ -9,10 +10,12 @@
|
||||||
export let overBackground = false
|
export let overBackground = false
|
||||||
export let target
|
export let target
|
||||||
export let download
|
export let download
|
||||||
|
|
||||||
|
const dispatch = createEventDispatcher()
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<a
|
<a
|
||||||
on:click
|
on:click={e => e.stopPropagation() && dispatch(e)}
|
||||||
{href}
|
{href}
|
||||||
{target}
|
{target}
|
||||||
{download}
|
{download}
|
||||||
|
|
Loading…
Reference in New Issue