Skip to content

Commit

Permalink
fix(Currency): cleaned up context
Browse files Browse the repository at this point in the history
docs(currency): updated examples
  • Loading branch information
N00nDay committed Oct 23, 2022
1 parent a731ccb commit 4a1deb7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
17 changes: 7 additions & 10 deletions src/lib/components/currency/Currency.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<script lang="ts" context="module">
export const CURRENCY_CONTEXT_ID = 'currency-context-id';
</script>

<script lang="ts">
import { slide } from 'svelte/transition';
import { setContext } from 'svelte';
import Icon from '../icon';
import { error as errorIcon } from '../../icons';
import { writable, type Writable } from 'svelte/store';
export let name: string;
export let error: string | undefined = undefined;
Expand All @@ -17,6 +14,12 @@
export let disabled = false;
export let readonly = false;
let currentError: Writable<string | undefined> = writable(error);
$: currentError.set(error);
setContext('currency-name', name);
setContext('currency-error', currentError);
function onlyNumeric(e: KeyboardEvent) {
if (!e.key.match(/^[0-9]+$/)) e.preventDefault();
}
Expand All @@ -33,12 +36,6 @@
}
}
}
setContext(CURRENCY_CONTEXT_ID, {
currency: true,
name,
error
});
</script>

<div class={$$props.class} style={$$props.style}>
Expand Down
12 changes: 3 additions & 9 deletions src/lib/components/currency/Label.svelte
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
<script lang="ts">
import { getContext } from 'svelte';
import { twMerge } from 'tailwind-merge';
import { useContext } from '../../utils/useContext';
import { CURRENCY_CONTEXT_ID } from './Currency.svelte';
import { get_current_component } from 'svelte/internal';
import { forwardEventsBuilder, useActions, type ActionArray } from '../../actions';
export let use: ActionArray = [];
import { exclude } from '../../utils/exclude';
import type { Writable } from 'svelte/store';
const forwardEvents = forwardEventsBuilder(get_current_component());
useContext({
context_id: CURRENCY_CONTEXT_ID,
parent: 'Currency',
component: 'Currency.Label'
});
const { name, error }: { name: string; error: string } = getContext(CURRENCY_CONTEXT_ID);
const name: string = getContext('currency-name');
const error: Writable<string | undefined> = getContext('currency-error');
let defaultClass = 'block text-sm font-medium';
if (error) {
Expand Down
12 changes: 11 additions & 1 deletion src/routes/currency/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
import { PropsTable, SlotsTable, UpdatedComponent, CodeBlock } from '../../docs';
import { email, phone } from '../../docs/icons';
import { currency_usd } from '../../lib/icons';
let value: string | undefined;
let error: string | undefined = "You're doing it wrong!";
$: if (value && value.length > 0 && value !== '0.00') {
error = undefined;
} else {
error = "You're doing it wrong!";
}
$: console.log('value', value);
</script>

<Col class="col-24">
Expand All @@ -21,7 +31,7 @@
<Currency.Trailing slot="trailing" data={phone} />
</Currency>
<br />
<Currency name="currency-3" error="There has been an error">
<Currency name="currency-3" {error} bind:value>
<Currency.Label slot="label">Label</Currency.Label>
<Currency.Leading slot="leading" data={email} />
</Currency>
Expand Down

0 comments on commit 4a1deb7

Please sign in to comment.