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

radio improvements take 2 #116

Merged
merged 3 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions apps/web/styles/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@
@tailwind components;
@tailwind utilities;

sub,
sup {
font-size: 60%;
line-height: 0;
position: relative;
vertical-align: baseline;
}
@layer base {
label[for] {
@apply cursor-pointer;
}

sup {
top: 0;
vertical-align: super;
}
sub,
sup {
font-size: 60%;
line-height: 0;
position: relative;
vertical-align: baseline;
}

sup {
top: 0;
vertical-align: super;
}

sub {
bottom: 0;
vertical-align: sub;
sub {
bottom: 0;
vertical-align: sub;
}
}
25 changes: 24 additions & 1 deletion packages/remix-forms/src/createField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ const types: Record<FieldType, React.HTMLInputTypeAttribute> = {
date: 'date',
}

function getInputType(
type: FieldType,
radio: boolean,
): React.HTMLInputTypeAttribute {
if (radio) return 'radio'

return types[type]
}

type FieldBaseProps<Schema extends SomeZodObject> = Omit<
Partial<Field<z.infer<Schema>>>,
'name'
Expand Down Expand Up @@ -276,7 +285,7 @@ function createField<Schema extends SomeZodObject>({
: undefined

const style = hidden ? { display: 'none' } : undefined
const type = typeProp ?? types[fieldType]
const type = typeProp ?? getInputType(fieldType, radio)

const registerProps = register(String(name), {
setValueAs: (value) => coerceValue(value, shape),
Expand Down Expand Up @@ -406,6 +415,20 @@ function createField<Schema extends SomeZodObject>({
defaultChecked: Boolean(value),
...child.props,
})
} else if (child.type === RadioGroup) {
return React.cloneElement(child, {
...a11yProps,
...child.props,
})
} else if (child.type === Radio) {
return React.cloneElement(child, {
id: `${name}-${child.props.value}`,
type,
autoFocus,
...registerProps,
defaultChecked: value === child.props.value,
...child.props,
})
} else if (child.type === Errors) {
if (!child.props.children && !errors?.length) return null

Expand Down