Skip to content

Commit

Permalink
feat(form-field): add readonly prop
Browse files Browse the repository at this point in the history
  • Loading branch information
kikoruiz committed Jul 27, 2023
1 parent 084fb76 commit 8bdbdfb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
13 changes: 12 additions & 1 deletion packages/components/form-field/src/FormField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@ export interface FormFieldProps
*/
asChild?: boolean
disabled?: boolean
readOnly?: boolean
}

export const FormField = forwardRef<HTMLDivElement, FormFieldProps>(
(
{ className, disabled = false, name, state, isRequired = false, asChild = false, ...others },
{
className,
disabled = false,
readOnly = false,
name,
state,
isRequired = false,
asChild = false,
...others
},
ref
) => {
const id = useId()
Expand All @@ -30,6 +40,7 @@ export const FormField = forwardRef<HTMLDivElement, FormFieldProps>(
name={name}
isRequired={isRequired}
disabled={disabled}
readOnly={readOnly}
state={state}
>
<Component
Expand Down
4 changes: 4 additions & 0 deletions packages/components/form-field/src/FormFieldContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export interface FormFieldContextState {
* Disables the field and its associated input
*/
disabled?: boolean
/**
* Marks the field and its associated input as read only
*/
readOnly?: boolean
/**
* The validation state of the input.
*/
Expand Down
24 changes: 21 additions & 3 deletions packages/components/form-field/src/FormFieldControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import { FormFieldContext, FormFieldContextState } from './FormFieldContext'
type State = Partial<
Pick<
FormFieldContextState,
'id' | 'name' | 'description' | 'labelId' | 'disabled' | 'state' | 'isInvalid' | 'isRequired'
| 'id'
| 'name'
| 'description'
| 'labelId'
| 'disabled'
| 'readOnly'
| 'state'
| 'isInvalid'
| 'isRequired'
>
>

Expand All @@ -14,10 +22,20 @@ export interface FormFieldControlProps {
}

export const useFormFieldControl = () => {
const { id, name, description, disabled, state, labelId, isInvalid, isRequired } =
const { id, name, description, disabled, readOnly, state, labelId, isInvalid, isRequired } =
useContext(FormFieldContext) || {}

return { id, name, description, disabled, state, labelId, isInvalid, isRequired } as State
return {
id,
name,
description,
disabled,
readOnly,
state,
labelId,
isInvalid,
isRequired,
} as State
}

export const FormFieldControl = ({ children }: FormFieldControlProps) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/components/form-field/src/FormFieldLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export const FormFieldLabel = forwardRef<HTMLLabelElement, FormFieldLabelProps>(
) => {
const control = useFormField()

const { disabled, labelId, isRequired } = control
const { disabled, readOnly, labelId, isRequired } = control
const isInactive = disabled || readOnly
const htmlFor = asChild ? undefined : htmlForProp || control.id

return (
Expand All @@ -39,7 +40,7 @@ export const FormFieldLabel = forwardRef<HTMLLabelElement, FormFieldLabelProps>(
className={cx(
className,
'flex items-center gap-sm',
disabled ? 'text-on-surface/dim-3 pointer-events-none' : undefined
isInactive ? 'text-on-surface/dim-3 pointer-events-none' : undefined
)}
asChild={asChild}
{...others}
Expand Down
8 changes: 7 additions & 1 deletion packages/components/form-field/src/FormFieldProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import { ReactNode, useCallback, useMemo, useState } from 'react'
import { FormFieldContext, FormFieldContextState } from './FormFieldContext'

export interface FormFieldProviderProps
extends Pick<FormFieldContextState, 'id' | 'name' | 'disabled' | 'state' | 'isRequired'> {
extends Pick<
FormFieldContextState,
'id' | 'name' | 'disabled' | 'readOnly' | 'state' | 'isRequired'
> {
children: ReactNode
}

export const FormFieldProvider = ({
id,
name,
disabled = false,
readOnly = false,
state,
isRequired,
children,
Expand All @@ -36,6 +40,7 @@ export const FormFieldProvider = ({
labelId,
name,
disabled,
readOnly,
state,
isRequired,
isInvalid,
Expand All @@ -48,6 +53,7 @@ export const FormFieldProvider = ({
labelId,
name,
disabled,
readOnly,
description,
state,
isRequired,
Expand Down

0 comments on commit 8bdbdfb

Please sign in to comment.