Skip to content

Commit

Permalink
feat(elements): Form and Field registration/submission (#2445)
Browse files Browse the repository at this point in the history
* feat(elements): Form and Field registration/submission

* build(repo): List installed packages

* build(repo): Test removing node_modules hard cache

* build(repo): Use setup-node .npm cache

* chore(repo): Finalize PR
  • Loading branch information
tmilewski authored Dec 22, 2023
1 parent 45da887 commit 4b85989
Show file tree
Hide file tree
Showing 14 changed files with 471 additions and 75 deletions.
2 changes: 2 additions & 0 deletions .changeset/quick-gorillas-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
21 changes: 3 additions & 18 deletions .github/actions/init/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ inputs:
description: 'Enable Playwright?'
required: false
default: 'false'
cache-suffix:
description: 'The cache suffix to use'
required: false
default: ''
turbo-summarize:
description: 'The token to use for Turbo task summaries'
required: false
Expand Down Expand Up @@ -97,23 +93,12 @@ runs:
- name: Setup NodeJS ${{ inputs.node-version }}
uses: actions/setup-node@v4
with:
cache: 'npm'
node-version: ${{ inputs.node-version }}
registry-url: ${{ inputs.registry-url }}

- name: Cache node_modules
uses: actions/cache@v3
id: npm-cache
with:
path: ./node_modules
key: ${{ runner.os }}-node-${{ inputs.node-version }}-${{ hashFiles('.github/.cache-version') }}-node-modules-${{ hashFiles('package-lock.json') }}${{ inputs.cache-suffix }}

- name: Install NPM Dependencies
if: steps.npm-cache.outputs.cache-hit != 'true'
run: npm ci --audit=false --fund=false
shell: bash

- name: Lint GitHub Actions Workflows
run: npx eslint .github
run: npm ci --audit=false --fund=false --prefer-offline
shell: bash

- name: Get Playwright Version
Expand All @@ -128,7 +113,7 @@ runs:
id: playwright-cache
with:
path: ~/.cache/ms-playwright
key: ${{ runner.os }}-node-${{ env.NODE_VERSION }}-${{ hashFiles('.github/.cache-version') }}-playwright-${{ steps.playwright-version.outputs.VERSION }}
key: ${{ runner.os }}-node-${{ env.NODE_VERSION }}-playwright-${{ steps.playwright-version.outputs.VERSION }}

- name: Install Playwright Browsers
if: inputs.playwright-enabled == 'true' && steps.playwright-cache.outputs.cache-hit != 'true'
Expand Down
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,19 @@ jobs:
turbo-team: ${{ vars.TURBO_TEAM }}
turbo-token: ${{ secrets.TURBO_TOKEN }}

- name: List node_modules
run: npm ls
shell: bash

- name: Require Changeset
timeout-minutes: ${{ fromJSON(vars.TIMEOUT_MINUTES_SHORT) }}
if: ${{ !(github.event_name == 'merge_group') }}
run: if [ "${{ github.event.pull_request.user.login }}" = "clerk-cookie" ]; then echo 'Skipping' && exit 0; else npx changeset status --since=origin/main; fi

- name: Lint GitHub Actions Workflows
run: npx eslint .github
shell: bash

- name: Check Formatting
timeout-minutes: ${{ fromJSON(vars.TIMEOUT_MINUTES_SHORT) }}
run: npm run format:check
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/preview.retheme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ jobs:
id: config
uses: ./.github/actions/init
with:
cache-suffix: "-retheme"
turbo-signature: ${{ secrets.TURBO_REMOTE_CACHE_SIGNATURE_KEY }}
turbo-team: ${{ vars.TURBO_TEAM }}
turbo-token: ${{ secrets.TURBO_TOKEN }}
Expand Down
134 changes: 132 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,18 +1,63 @@
import { SignIn, SignInFactorOne, SignInFactorTwo, SignInSSOCallback, SignInStart } from '@clerk/elements';
import {
Field,
Form,
Input,
Label,
SignIn,
SignInFactorOne,
SignInFactorTwo,
SignInSSOCallback,
SignInStart,
Submit,
} from '@clerk/elements';

import { Debug } from '@/components/debug';

export default function SignInPage() {
return (
<SignIn>
<SignInStart>
<p>Start child</p>
</SignInStart>
<SignInFactorOne>
<p>Factor one child</p>
</SignInFactorOne>
<SignInFactorTwo>
<p>Factor two child</p>
</SignInFactorTwo>
<SignInSSOCallback />
<div className='m-auto w-max text-sm'>
<SignInStart>
<h1 className='text-xl mb-6'>Start</h1>

<Form className='flex gap-6 flex-col '>
<Field
name='identifier'
className='flex gap-4 justify-between items-center '
>
<Label htmlFor='password'>Email</Label>
<Input
type='identifier'
className='bg-tertiary rounded-sm px-2 py-1 border border-foreground'
/>
</Field>

<Field
name='password'
className='flex gap-4 justify-between items-center'
>
<Label htmlFor='password'>Password</Label>
<Input
type='password'
className='bg-tertiary rounded-sm px-2 py-1 border border-foreground'
/>
</Field>

<Submit className='px-4 py-2 b-1 bg-blue-950 bg-opacity-20 hover:bg-opacity-10 active:bg-opacity-5 rounded-md dark:bg-opacity-100 dark:hover:bg-opacity-80 dark:active:bg-opacity-50 transition'>
Sign In
</Submit>
</Form>
</SignInStart>
<SignInFactorOne>
<p>Factor one child</p>
</SignInFactorOne>
<SignInFactorTwo>
<p>Factor two child</p>
</SignInFactorTwo>
<SignInSSOCallback />
</div>

<Debug />
</SignIn>
);
}
45 changes: 45 additions & 0 deletions packages/elements/examples/nextjs/components/debug.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use client';

import { SignedIn } from '@clerk/clerk-react';
import { useSignInFlow } from '@clerk/elements';
import { SignOutButton } from '@clerk/nextjs';

export function Button(props: React.ComponentProps<'button'>) {
return (
<button
className='px-4 py-2 b-1 bg-blue-950 bg-opacity-20 hover:bg-opacity-10 active:bg-opacity-5 rounded-md dark:bg-opacity-100 dark:hover:bg-opacity-80 dark:active:bg-opacity-50 transition'
type='button'
{...props}
/>
);
}

export function Debug() {
const ref = useSignInFlow();

return (
<div className='absolute text-xs flex p-4 gap-4 bottom-0 w-screen justify-center bg-secondary border-tertiary border-t'>
<Button
onClick={() => {
console.dir(ref.getSnapshot().context.fields);
}}
>
Log Fields
</Button>

<Button
onClick={() => {
console.dir(ref.getSnapshot().context);
}}
>
Log Context
</Button>

<SignedIn>
<SignOutButton>
<Button>Sign Out</Button>
</SignOutButton>
</SignedIn>
</div>
);
}
1 change: 1 addition & 0 deletions packages/elements/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"dependencies": {
"@clerk/nextjs": "^5.0.0-alpha-v5.12",
"@radix-ui/react-form": "^0.0.3",
"@radix-ui/react-slot": "^1.0.2",
"@xstate/react": "^4.0.1",
"clsx": "^2.0.0",
"react-children-utilities": "^2.9.0",
Expand Down
33 changes: 33 additions & 0 deletions packages/elements/src/common/form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { FormControlProps, FormProps } from '@radix-ui/react-form';
import { Control, Field, Form as RadixForm, Label, Submit } from '@radix-ui/react-form';
import { Slot } from '@radix-ui/react-slot';

import { useForm, useInput } from '../internals/machines/sign-in.context';

function Input({ asChild, ...rest }: FormControlProps) {
const { type, value } = rest;
const field = useInput({ type, value });

const Comp = asChild ? Slot : Control;

return (
<Comp
{...field.props} // TODO
{...rest}
/>
);
}

function Form({ asChild, ...rest }: FormProps) {
const form = useForm();

const Comp = asChild ? Slot : RadixForm;
return (
<Comp
{...form.props} // TODO
{...rest}
/>
);
}

export { Form, Input, Field, Label, Submit };
Loading

0 comments on commit 4b85989

Please sign in to comment.