Skip to content

Commit 4b3731d

Browse files
committed
let RadioFieldDyn handle its own radio state
the div wrapping the radios was messing up the way RadioFieldDyn passes checked state to its child radios because they're expected to be direct children
1 parent c1dd1b2 commit 4b3731d

File tree

4 files changed

+19
-29
lines changed

4 files changed

+19
-29
lines changed

app/components/form/fields/RadioField.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import { FieldLabel } from '~/ui/lib/FieldLabel'
1919
import { Radio, type RadioProps } from '~/ui/lib/Radio'
2020
import { RadioGroup, type RadioGroupProps } from '~/ui/lib/RadioGroup'
2121
import { TextInputHint } from '~/ui/lib/TextInput'
22+
import { isOneOf } from '~/util/children'
23+
import { invariant } from '~/util/invariant'
2224
import { capitalize } from '~/util/str'
2325

2426
export type RadioFieldProps<
@@ -126,6 +128,10 @@ export function RadioFieldDyn<
126128
}: RadioFieldDynProps<TFieldValues, TName>) {
127129
const id = useId()
128130
const { field } = useController({ name, control })
131+
invariant(
132+
isOneOf(children, [Radio, RadioCard]),
133+
'Children of RadioFieldDyn must be Radio or RadioCard'
134+
)
129135
return (
130136
<div>
131137
<div className="mb-2">

app/forms/access-util.tsx

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Copyright Oxide Computer Company
77
*/
88
import type { Control } from 'react-hook-form'
9+
import * as R from 'remeda'
910

1011
import {
1112
allRoles,
@@ -25,12 +26,12 @@ import { capitalize } from '~/util/str'
2526

2627
type AddUserValues = {
2728
identityId: string
28-
roleName: RoleKey | ''
29+
roleName: RoleKey
2930
}
3031

3132
export const defaultValues: AddUserValues = {
3233
identityId: '',
33-
roleName: '',
34+
roleName: 'viewer',
3435
}
3536

3637
// Role descriptions for project-level roles
@@ -84,7 +85,6 @@ type RoleRadioFieldProps = {
8485

8586
export function RoleRadioField({ control, scope }: RoleRadioFieldProps) {
8687
const roleDescriptions = scope === 'Silo' ? siloRoleDescriptions : projectRoleDescriptions
87-
const currentRole = control._formValues.roleName || ''
8888
return (
8989
<>
9090
<RadioFieldDyn
@@ -95,25 +95,17 @@ export function RoleRadioField({ control, scope }: RoleRadioFieldProps) {
9595
column
9696
className="mt-2"
9797
>
98-
{allRoles.map((role) => (
99-
<div className="mt-1" key={role}>
100-
<Radio
101-
name="roleName"
102-
value={role}
103-
defaultChecked={currentRole === role}
104-
alignTop
105-
>
106-
{/* negative top margin to control spacing with radio button and label */}
107-
<div className="-mt-0.5 ml-1">
108-
<div className="text-sans-md text-raise">
109-
{capitalize(role).replace('_', ' ')}
110-
</div>
111-
<div className="text-sans-sm text-secondary mt-0.5">
112-
{roleDescriptions[role]}
113-
</div>
98+
{R.reverse(allRoles).map((role) => (
99+
<Radio name="roleName" key={role} value={role} alignTop>
100+
<div className="ml-1">
101+
<div className="text-sans-md text-raise">
102+
{capitalize(role).replace('_', ' ')}
114103
</div>
115-
</Radio>
116-
</div>
104+
<div className="text-sans-sm text-secondary mt-0.5">
105+
{roleDescriptions[role]}
106+
</div>
107+
</div>
108+
</Radio>
117109
))}
118110
</RadioFieldDyn>
119111
{scope === 'Project' && (

app/forms/project-access.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ export function ProjectAccessAddUserSideModal({ onDismiss, policy }: AddRoleModa
5151
form={form}
5252
formType="create"
5353
onSubmit={({ identityId, roleName }) => {
54-
// can't happen because roleName is validated not to be '', but TS
55-
// wants to be sure
56-
if (roleName === '') return
57-
5854
// actor is guaranteed to be in the list because it came from there
5955
const identityType = actors.find((a) => a.id === identityId)!.identityType
6056

app/forms/silo-access.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ export function SiloAccessAddUserSideModal({ onDismiss, policy }: AddRoleModalPr
4646
title="Add user or group"
4747
onDismiss={onDismiss}
4848
onSubmit={({ identityId, roleName }) => {
49-
// can't happen because roleName is validated not to be '', but TS
50-
// wants to be sure
51-
if (roleName === '') return
52-
5349
// TODO: DRY logic
5450
// actor is guaranteed to be in the list because it came from there
5551
const identityType = actors.find((a) => a.id === identityId)!.identityType

0 commit comments

Comments
 (0)