Skip to content

Commit

Permalink
feat: 🚧 starting to refactor rdf with shadcn-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
cif committed May 12, 2024
1 parent 41c3ffd commit 99a6588
Show file tree
Hide file tree
Showing 18 changed files with 742 additions and 81 deletions.
Binary file modified bun.lockb
Binary file not shown.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "@surface/switch",
"private": true,
"name": "@stabledata/switch",
"version": "0.0.0",
"type": "module",
"scripts": {
Expand All @@ -12,6 +11,8 @@
},
"dependencies": {
"@hono/node-server": "^1.9.1",
"@radix-ui/react-label": "^2.0.2",
"@radix-ui/react-select": "^2.0.0",
"@radix-ui/react-slot": "^1.0.2",
"@tanstack/react-router": "^1.28.5",
"@tanstack/react-router-server": "^1.28.5",
Expand All @@ -27,6 +28,7 @@
"pino": "^9.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.51.4",
"tailwind-merge": "^2.3.0",
"tailwindcss-animate": "^1.0.7",
"vite-tsconfig-paths": "^4.3.2",
Expand Down
43 changes: 0 additions & 43 deletions state/user.state.ts

This file was deleted.

2 changes: 1 addition & 1 deletion surface.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { StartClient } from "@tanstack/react-router-server";
import { createRouter } from "./surface.router";
import { hc } from "hono/client";
import { AppType } from "./surface.app";
import "./views/index.css";
import "./views/shad.css";
import "./views/index.css";

export const rpcClient = hc<AppType>(`/`, {
headers: {
Expand Down
2 changes: 0 additions & 2 deletions surface.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import {
createRouter as tanStackCreateRouter,
} from "@tanstack/react-router";
import { routeTree } from "./.routes.tree";
import { User } from "./handlers/auth.handlers";
import { inflateState } from "./state/registry";
import { rpcClient } from "./surface.client";

export type RouterContext = {
user?: User;
rpc?: typeof rpcClient;
};

Expand Down
47 changes: 47 additions & 0 deletions switch/components/controls/text.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { SwitchInputFieldProps } from "switch/types";
import { Label } from "switch/components/ui/label";
import { Input } from "switch/components/ui/input";
/**
*
* @props see {@link RDFTextFieldProps}
* @returns field with given options
*/
export const SwitchTextField = ({
name,
type,
label,
placeholder,
helperText,
errors,
options,
register,
disabled,
// hidden,
}: SwitchInputFieldProps) => {
const labelClasses = ["label", `label-${name}`];
const inputClasses = ["input", `input-${name}`];
const error = errors !== undefined ? errors[name] : undefined;
if (error) {
inputClasses.push("input-has-error");
labelClasses.push("label-has-error");
}

return (
<div>
<div className="grid w-full max-w-sm items-center gap-2">
<Label htmlFor={name}>{label}</Label>
<Input
type={type}
id={name}
placeholder={placeholder}
{...register(name, options)}
disabled={disabled as boolean}
/>
</div>
{/* <RDFErrorMessage error={error} /> */}
{JSON.stringify(error)}
{helperText}
{/* <RDFHelpText helper={helper} /> */}
</div>
);
};
205 changes: 205 additions & 0 deletions switch/components/form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
import { Form, useInputSwitches } from "../hooks/use-form";
import { SwitchTextField } from "./controls/text";
// import { RDFCheckbox } from "./RDFCheckbox";
// import { RDFSelect } from "./RDFSelect";
// import { RDFRadio } from "./RDFRadios";
// import { RDFSwitch } from "./RDFSwitch";
// import { RDFMedia } from "./RDFMedia";
// import { RDFList } from "./RDFList";
// import { RDFTable } from "./RDFTable";
// import { RDFDatePicker } from "./RDFDatePicker";

export type FormProps<T> = {
form: Form<T>;
submitButtonLabel?: string | React.ReactNode;
submitButtonLabelInFlight?: string | React.ReactNode;
isInFlight?: boolean;
};

export function SwitchForm<T extends object>({
form,
submitButtonLabel = "Send it",
submitButtonLabelInFlight = "Sending...",
isInFlight = false,
}: FormProps<T>) {
const {
register,
errors,
// control,
handleSubmit: rhfSubmitHandler,
handleSubmitWithFormData,
} = useInputSwitches<T>(form);

return (
<form onSubmit={rhfSubmitHandler(handleSubmitWithFormData)}>
{form.fields.map((field, index) => {
switch (field.type) {
// text field
case "text":
case "multiline":
case "number":
case "email":
case "password":
return (
<SwitchTextField
key={`${field.name}-${index}`}
type={field.type}
name={field.name}
label={field.label}
placeholder={field.placeholder}
helperText={field.helperText}
disabled={field.disabled}
hidden={field.hidden}
options={field.options}
register={register}
errors={errors}
/>
);
// text field
// case "media":
// return (
// <RDFMedia
// key={`${field.name}-${index}`}
// name={field.name}
// label={field.label}
// options={field.options}
// helper={field.helpText || field.HelpText}
// previewType={field.previewType}
// disabled={field.disabled}
// hidden={field.hidden}
// control={control}
// errors={errors}
// />
// );
// // checkbox
// case "checkbox":
// return (
// <RDFCheckbox
// key={`${field.name}-${index}`}
// name={field.name}
// label={field.label}
// options={field.options}
// disabled={field.disabled}
// hidden={field.hidden}
// helper={field.helpText || field.HelpText}
// control={control}
// register={register}
// errors={errors}
// />
// );
// // switch
// case "switch":
// return (
// <RDFSwitch
// key={`${field.name}-${index}`}
// name={field.name}
// label={field.label}
// options={field.options}
// helper={field.helpText || field.HelpText}
// disabled={field.disabled}
// hidden={field.hidden}
// defaultValue={field.default as boolean}
// control={control}
// register={register}
// errors={errors}
// />
// );
// // select
// case "select":
// return (
// <RDFSelect
// key={`${field.name}-${index}`}
// name={field.name}
// label={field.label}
// options={field.options}
// choices={field.choices as RDFChoiceOption[]}
// placeholder={field.placeholder}
// helper={field.helpText || field.HelpText}
// disabled={field.disabled}
// hidden={field.hidden}
// control={control}
// register={register}
// errors={errors}
// />
// );
// // radio
// case "radio":
// return (
// <RDFRadio
// key={`${field.name}-${index}`}
// name={field.name}
// label={field.label}
// options={field.options}
// choices={field.choices as RDFChoiceOption[]}
// placeholder={field.placeholder}
// helper={field.helpText || field.HelpText}
// disabled={field.disabled}
// hidden={field.hidden}
// control={control}
// register={register}
// errors={errors}
// />
// );
// // list
// case "list":
// return (
// <RDFList
// key={`${field.name}-${index}`}
// name={field.name}
// label={field.label}
// options={field.options}
// placeholder={field.placeholder}
// helper={field.helpText || field.HelpText}
// disabled={field.disabled}
// hidden={field.hidden}
// control={control}
// register={register}
// errors={errors}
// addItemText={field.addItemButtonText}
// />
// );

// // date picker:
// case "date":
// case "datetime":
// return (
// <RDFDatePicker
// key={`${field.name}-${index}`}
// name={field.name}
// label={field.label}
// options={field.options}
// helper={field.helpText || field.HelpText}
// disabled={field.disabled}
// hidden={field.hidden}
// control={control}
// register={register}
// errors={errors}
// showTimeSelect={field.type === "datetime"}
// />
// );
// // table
// case "table":
// return (
// <RDFTable
// key={`${field.name}-${index}`}
// name={field.name}
// label={field.label}
// options={field.options}
// helper={field.helpText || field.HelpText}
// disabled={field.disabled}
// hidden={field.hidden}
// control={control}
// register={register}
// errors={errors}
// addItemText={field.addItemButtonText}
// columns={field.columns}
// />
// );
}
})}
<button type="submit" className="submit" disabled={isInFlight}>
{isInFlight ? submitButtonLabelInFlight : submitButtonLabel}
</button>
</form>
);
}
Loading

0 comments on commit 99a6588

Please sign in to comment.