Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward use:action to components #1599

Open
theetrain opened this issue Dec 22, 2022 · 3 comments
Open

Forward use:action to components #1599

theetrain opened this issue Dec 22, 2022 · 3 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@theetrain
Copy link
Collaborator

theetrain commented Dec 22, 2022

Let's add a way to pass actions to components, especially Form and FluidForm.

Rejected Proposal 1: `use` prop

This is flawed since you cannot specify <form action={something} />

Proposed prop name is use. Example component:

<script>
  export let use
</script>

<div use:use>
  Hello
</div>

Example usage:

<script>
  import Component from './Component2.svelte'
	
  const action = () => console.log('action')
</script>

<Component use={action} />

Example: https://svelte.dev/repl/e24d07853ebe4773b1ab818ad60536a2?version=3.55.0

Proposal 2: formAttributes

Specify a formAttributes prop. If present, will render an outer <form> element.

  • Typed as HTMLFormAttributes | { useAction: () => {} }
  • If defined, renders a <form> element, wrapping the target component
  • If useAction is defined, it will be desconstructed from formAttributes and applied to the form element with use:useAction
  • For components with multiple interaction points, a more descriptive name can be used. For example, with Pagination we can have itemsPerPageFormAttributes for the 'items per page' dropdown (though an 'update' button may need to be rendered alongside <select> elements)
  • Supplementary documentation should be added explaining why these features exist and how to best use them
<Button formAttributes={{ action: '?/update', useAction: enhance }}>Update</Button>

Demo: https://svelte.dev/repl/a1f1eec629f141a9a3373dafa075dd8a?version=3.55.0

Related #1622

@metonym metonym added the enhancement New feature or request label Dec 22, 2022
@theetrain theetrain added this to the v1.0.0 milestone Jan 6, 2023
@theetrain
Copy link
Collaborator Author

Added proposal 2 above with a demo based on #1628 (comment)

Check out the demo; feedback is welcome.

@brunnerh
Copy link
Contributor

brunnerh commented Feb 4, 2023

If defined, renders a <form> element, wrapping the target component

That does not seem right. If you want a form you can just add one; other components should not be concerned with this.
If someone actually needs the feature of single-button forms a lot, that can just be implemented in user land.

If a property for attributes is added, that should in my opinion refer to the element at hand and could just be called attributes.

So if you want a button and a form with a Svelte action:

<Form action="?/kit-action" attributes={ useAction: enhance }>
    <Button type="submit">Submit</Button>
</Form>

@theetrain
Copy link
Collaborator Author

theetrain commented Feb 4, 2023

@brunnerh agreed; I reached a similar conclusion (not fully defined, though) in my <Button> proposal in #1622. Single-element components like <Button> can have the user wrap the <form> themselves, or spread attributes such as formaction if they like.

For complex components with multiple buttons, such as the sortable DataTable, the user should be able to provide an href to pass in the same route with searchParams, or formaction and related attributes to each individual sortable button; allowing a user to sort a table without JS or hydration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants