Question about composability #165
Replies: 1 comment
-
hey @directormac! i'm thinking through these same questions right now. could you share a bit more about your context/use-case?
you can resolve that ts error by simplifying how you pass the RE composition patterns: i've come to take a need-driven approach. in my experience, it can be difficult to predict what an abstraction will need to do, or whether a coupling will be help or hindrance. i try to build with 'primitives' as long as possible. once it begins to get burdensome, i find that i usually have enough data points to abstract away repeated work. i'm new to svelte, but i've built composed forms on top of a few react libraries, and these days i lean towards "less is more". composition can be really useful, but there's always a trade-off to the convenience of the coupling. fields get reused a ton, and are more likely to have complex styling or logical requirements, so there's a lot of value-add to composing them. in my experience, binding form state to ui is a bit more hit/miss. one-off formswith <script lang="ts">
import { verifyEmailValidator } from '$dtos/client';
export let data: PageData;
const verifyEmailForm = superForm(data.verifyEmailForm, {
id: 'verify-email',
dataType: 'json',
validators: verifyEmailValidator,
});
</script>
<form
use:verifyEmailForm.enhance
novalidate
method="POST"
>
<Card.Content class="space-y-4">
<!-- composed using formsnap + bits-ui -->
<FormCodeInput
form={verifyEmailForm}
name="code"
length={6}
label={locale.verificationCode}
/>
<!-- composed using formsnap + bits-ui -->
<FormButton class="w-full mt-2">
{locale.register}
</FormButton>
</Card.Content>
</form> reused formsi have come across a few cases where i've composed a specific form that needs* to be instantiated multiple times. since *note: i'm not convinced this is the right level of abstraction. differences between implementations are largely stylistic, so there's probably a better way to express that within the context of a single component. i'm still doing a lot of refactoring and simplification as i refine what was initially a fairly brute-force approach <script lang="ts">
const form = superForm($page.data.signInForm, {
id: 'sign-in',
dataType: 'json',
validators: signInValidator,
});
</script>
<form
use:form.enhance
novalidate
action="/api/sign-in"
method="POST"
class={className}
>
<slot {form} />
</form>
<!-- consumer -->
<SignInForm let:form>
<Input {form} />
<Input {form} />
</SignInForm> |
Beta Was this translation helpful? Give feedback.
-
Hello everyone just would like to ask about composability and how would you implement a reuable form and components for reference this example by superforms
Superform example SvelteLab
Repository
I have comeup with this for formsnap usecase, don't mind the dispatches its my way of hacking something from the parent.
Here i expose superForm in the slot as a let declaration, as its needed by formfield as
SuperForm<T,ResponseMessage>
in a basic input field i have this
And using it with this, my problem now is binding it as svelte does not allow us to bind values from the
let
declarationHelp would be appreciated. i'm just trying to hack things around on this im fine redeclaring my forms in a single file but given that snapshot is only supported on page level, i need to redeclare those on pages and pass them down the drain elegantly.
Beta Was this translation helpful? Give feedback.
All reactions