-
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
47 changed files
with
2,900 additions
and
2,383 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
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
34 changes: 21 additions & 13 deletions
34
packages/app-builder/src/components/Form/FormCheckbox.tsx
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,24 +1,32 @@ | ||
import { unstable_useControl, useField } from '@conform-to/react'; | ||
import { Checkbox } from 'ui-design-system'; | ||
|
||
import { useFieldConfig } from './FormField'; | ||
import { useFieldName } from './FormField'; | ||
|
||
interface FormCheckboxProps | ||
extends Omit<React.ComponentProps<typeof Checkbox>, 'checked'> {} | ||
|
||
export function FormCheckbox({ | ||
children, | ||
onCheckedChange, | ||
...rest | ||
}: Omit<React.ComponentProps<typeof Checkbox>, 'checked'>) { | ||
const config = useFieldConfig(); | ||
}: FormCheckboxProps) { | ||
const name = useFieldName(); | ||
const [meta] = useField<boolean>(name); | ||
|
||
const control = unstable_useControl(meta); | ||
|
||
return ( | ||
<Checkbox | ||
id={config.id} | ||
name={config.name} | ||
defaultChecked={ | ||
typeof config.defaultValue === 'boolean' | ||
? config.defaultValue | ||
: config.defaultValue === 'on' | ||
} | ||
{...rest} | ||
/> | ||
// Radix UI don't expose the input element directly, so we need to query it | ||
<div ref={(element) => control.register(element?.querySelector('input'))}> | ||
<Checkbox | ||
id={meta.id} | ||
name={name} | ||
checked={control.value === 'on'} | ||
onCheckedChange={(state) => control.change(state.valueOf() ? 'on' : '')} | ||
onBlur={control.blur} | ||
{...rest} | ||
/> | ||
</div> | ||
); | ||
} |
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,31 +1,39 @@ | ||
import { useField } from '@conform-to/react'; | ||
import clsx from 'clsx'; | ||
import * as React from 'react'; | ||
|
||
import { useFieldConfig } from './FormField'; | ||
|
||
export const FormError = React.forwardRef< | ||
HTMLParagraphElement, | ||
Omit<React.HTMLAttributes<HTMLParagraphElement>, 'id'> | ||
>(function FormError({ className, children, ...props }, ref) { | ||
const { error, errorId } = useFieldConfig(); | ||
|
||
if (!error) { | ||
return null; | ||
} | ||
|
||
const body = children ?? error; | ||
|
||
return ( | ||
<p | ||
ref={ref} | ||
id={errorId} | ||
className={clsx( | ||
'text-s font-medium text-red-100 transition-opacity duration-200 ease-in-out', | ||
className, | ||
)} | ||
{...props} | ||
> | ||
{body} | ||
</p> | ||
); | ||
}); | ||
import { useFieldName } from './FormField'; | ||
|
||
interface FormErrorProps | ||
extends Omit<React.HTMLAttributes<HTMLParagraphElement>, 'id'> {} | ||
|
||
export const FormError = React.forwardRef<HTMLParagraphElement, FormErrorProps>( | ||
function FormError({ className, children, ...props }, ref) { | ||
const name = useFieldName(); | ||
const [meta] = useField(name); | ||
|
||
const { errors, errorId } = meta; | ||
|
||
const error = errors?.at(0); | ||
|
||
if (!error) { | ||
return null; | ||
} | ||
|
||
const body = children ?? error; | ||
|
||
return ( | ||
<p | ||
ref={ref} | ||
id={errorId} | ||
className={clsx( | ||
'text-s font-medium text-red-100 transition-opacity duration-200 ease-in-out', | ||
className, | ||
)} | ||
{...props} | ||
> | ||
{body} | ||
</p> | ||
); | ||
}, | ||
); |
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,20 +1,19 @@ | ||
import { createSimpleContext } from '@app-builder/utils/create-context'; | ||
import { type FieldConfig } from '@conform-to/react'; | ||
import { type FieldName } from '@conform-to/react'; | ||
import type * as React from 'react'; | ||
|
||
const FieldConfigContext = | ||
createSimpleContext<FieldConfig<void>>('FieldConfig'); | ||
export const useFieldConfig = FieldConfigContext.useValue; | ||
const FieldNameContext = createSimpleContext<string>('FieldName'); | ||
export const useFieldName = FieldNameContext.useValue; | ||
|
||
export function FormField<Schema>({ | ||
config, | ||
name, | ||
...props | ||
}: { | ||
config: FieldConfig<Schema>; | ||
name: FieldName<Schema>; | ||
} & React.ComponentPropsWithoutRef<'div'>) { | ||
return ( | ||
<FieldConfigContext.Provider value={config}> | ||
<FieldNameContext.Provider value={name}> | ||
<div {...props} /> | ||
</FieldConfigContext.Provider> | ||
</FieldNameContext.Provider> | ||
); | ||
} |
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,24 +1,43 @@ | ||
import { conform } from '@conform-to/react'; | ||
import { getInputProps, useField } from '@conform-to/react'; | ||
import * as React from 'react'; | ||
import { Input, type InputProps } from 'ui-design-system'; | ||
|
||
import { useFieldConfig } from './FormField'; | ||
import { extractInputOptions, type InputOptions } from './helpers'; | ||
import { useFieldName } from './FormField'; | ||
|
||
interface FormInputProps extends Omit<InputProps, 'borderColor' | 'type'> { | ||
type: | ||
| 'color' | ||
| 'date' | ||
| 'datetime-local' | ||
| 'email' | ||
| 'file' | ||
| 'hidden' | ||
| 'month' | ||
| 'number' | ||
| 'password' | ||
| 'range' | ||
| 'search' | ||
| 'tel' | ||
| 'text' | ||
| 'time' | ||
| 'url' | ||
| 'week'; | ||
} | ||
|
||
export const FormInput = React.forwardRef< | ||
React.ElementRef<typeof Input>, | ||
InputOptions & Omit<InputProps, 'borderColor'> | ||
>(function FormInput(props, ref) { | ||
const config = useFieldConfig(); | ||
|
||
const { options, ...rest } = extractInputOptions(props); | ||
FormInputProps | ||
>(function FormInput({ type, ...inputProps }, ref) { | ||
const name = useFieldName(); | ||
const [meta] = useField<string>(name); | ||
|
||
return ( | ||
<Input | ||
ref={ref} | ||
borderColor={config.error ? 'red-100' : 'grey-10'} | ||
{...rest} | ||
{...conform.input(config, options)} | ||
borderColor={meta.valid ? 'grey-10' : 'red-100'} | ||
{...inputProps} | ||
{...getInputProps(meta, { type })} | ||
key={meta.key} | ||
/> | ||
); | ||
}); |
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.