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

feat(conform-dom,conform-react)!: imperative command api #70

Merged
merged 6 commits into from
Jan 8, 2023

Conversation

edmundhung
Copy link
Owner

@edmundhung edmundhung commented Jan 1, 2023

This simplifies the useFieldList() to only returns the array with an extra list command builder that could be used anywhere.

import { list, requestCommand } from '@conform-to/react';

function Example(config) {
    const ref = useRef<HTMLFieldSetElement>(null);
    // useFieldList no longer returns the command
    const list = useFieldList(ref, { name: 'tasks' });
    // requestCommand is a helper to execute a command in an imperative way
    const reorder = (from, to) => requestCommand(ref.current?.form, list.reorder('tasks', { from, to }));

    return (
        <fieldset ref={ref}>
            {/* As the command is no longer scoped, you need to provide the name as well */}
            <button {...list.append('tasks')}>Add</button>
        </fieldset>
    )
}

close #68

@codesandbox
Copy link

codesandbox bot commented Jan 1, 2023

CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jan 1, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 554ff59
Status: ✅  Deploy successful!
Preview URL: https://571861e8.conform.pages.dev
Branch Preview URL: https://request-command.conform.pages.dev

View logs

@edmundhung edmundhung changed the base branch from main to dev January 2, 2023 22:25
@edmundhung edmundhung force-pushed the request-command branch 2 times, most recently from 8a3c88b to 934db9e Compare January 4, 2023 21:33
@edmundhung edmundhung changed the title feat(conform-dom,conform-react): imperative command api feat(conform-dom,conform-react)!: imperative command api Jan 7, 2023
@brandonpittman
Copy link
Contributor

Is the reorder function being used in the example above?

@edmundhung
Copy link
Owner Author

Is the reorder function being used in the example above?

No. Can't think of a simple example to demonstrate it. Maybe something that requires dynamic index like drag and drop.

@edmundhung edmundhung merged commit 233eb0d into dev Jan 8, 2023
@edmundhung edmundhung deleted the request-command branch January 8, 2023 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

imperative commands for useFieldList
2 participants