-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
329 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,93 @@ | ||
"use client"; | ||
|
||
import { useEffect } from "react"; | ||
import { createBuilder, createEntity } from "basebuilder"; | ||
import { useFormState, useFormStatus } from "react-dom"; | ||
import { z } from "zod"; | ||
|
||
import { | ||
createEntityComponent, | ||
Interpreter, | ||
useInterpreterStore, | ||
} from "@basebuilder/react"; | ||
|
||
import { submit } from "./submit"; | ||
|
||
const builder = createBuilder({ | ||
entities: [ | ||
createEntity({ | ||
name: "text", | ||
validate(value) { | ||
const res = z.string().safeParse(value); | ||
|
||
if (!res.success) { | ||
throw res.error.issues[0]?.message; | ||
} | ||
|
||
return res.data; | ||
}, | ||
}), | ||
], | ||
}); | ||
|
||
const textComponent = createEntityComponent( | ||
builder.entities[0], | ||
({ entity, setValue }) => { | ||
return ( | ||
<div> | ||
<input | ||
value={entity.value ?? ""} | ||
onChange={(e) => setValue(e.target.value)} | ||
name={entity.id} | ||
/> | ||
{JSON.stringify(entity.error)} | ||
</div> | ||
); | ||
}, | ||
); | ||
|
||
function SubmitButton() { | ||
const { pending } = useFormStatus(); | ||
|
||
return ( | ||
<button type="submit" aria-disabled={pending}> | ||
Add | ||
</button> | ||
); | ||
} | ||
|
||
const initialState = { | ||
errors: {}, | ||
values: {}, | ||
}; | ||
|
||
export default function Page() { | ||
return <div>Page</div>; | ||
const [state, formAction] = useFormState(submit, initialState); | ||
|
||
const interpreterStore = useInterpreterStore(builder, { | ||
entities: { | ||
"68b1a0dd-f3f8-4dde-a294-d61680d11223": { | ||
type: "text", | ||
attributes: {}, | ||
}, | ||
}, | ||
root: ["68b1a0dd-f3f8-4dde-a294-d61680d11223"], | ||
}); | ||
|
||
useEffect(() => { | ||
interpreterStore.setEntitiesErrors(state.errors); | ||
}, [interpreterStore, state]); | ||
|
||
return ( | ||
<form action={formAction}> | ||
<Interpreter | ||
interpreterStore={interpreterStore} | ||
components={{ | ||
text: textComponent, | ||
}} | ||
/> | ||
|
||
<SubmitButton /> | ||
</form> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"use server"; | ||
|
||
import { | ||
createBuilder, | ||
createEntity, | ||
validateEntitiesValues, | ||
} from "basebuilder"; | ||
import { z } from "zod"; | ||
|
||
const builder = createBuilder({ | ||
entities: [ | ||
createEntity({ | ||
name: "text", | ||
validate(value) { | ||
const res = z.string().min(5).safeParse(value); | ||
|
||
if (!res.success) { | ||
throw res.error.issues[0]?.message; | ||
} | ||
|
||
return res.data; | ||
}, | ||
}), | ||
], | ||
}); | ||
|
||
export async function submit(prevState: unknown, formData: FormData) { | ||
const values = Object.fromEntries(formData); | ||
|
||
const res = await validateEntitiesValues(values, builder, { | ||
entities: { | ||
"68b1a0dd-f3f8-4dde-a294-d61680d11223": { | ||
type: "text", | ||
attributes: {}, | ||
}, | ||
}, | ||
root: ["68b1a0dd-f3f8-4dde-a294-d61680d11223"], | ||
}); | ||
|
||
if (!res.success) { | ||
return { | ||
errors: res.entitiesErrors, | ||
values, | ||
}; | ||
} | ||
|
||
return { | ||
errors: {}, | ||
values: {}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.