Skip to content

Commit

Permalink
fix(input): readOnly and disabled within form field not being propagated
Browse files Browse the repository at this point in the history
  • Loading branch information
andresz1 committed Aug 28, 2023
1 parent 2b2fb3a commit 936dc4d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
52 changes: 52 additions & 0 deletions packages/components/input/src/Input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,30 @@ describe('Input', () => {
expect(screen.getByText(rightText)).toBeInTheDocument()
})

it('should be read only when group is read only', () => {
const placeholder = 'Smartphone'

render(
<InputGroup readOnly>
<Input placeholder={placeholder} />
</InputGroup>,
)

expect(screen.getByPlaceholderText(placeholder)).toHaveAttribute('readOnly')
})

it('should be disabled when group is disabled', () => {
const placeholder = 'Smartphone'

render(
<InputGroup disabled>
<Input placeholder={placeholder} />
</InputGroup>,
)

expect(screen.getByPlaceholderText(placeholder)).toBeDisabled()
})

it('should render icons within group', () => {
const leftLabel = 'Check'
const rightLabel = 'Euro'
Expand Down Expand Up @@ -354,4 +378,32 @@ describe('Input', () => {
expect(inputEl).toBeInvalid()
expect(inputEl).toHaveAccessibleDescription(errorText)
})

it('should be read only when field is read only', () => {
const label = 'Title'

render(
<FormField readOnly>
<FormField.Label>{label}</FormField.Label>

<Input />
</FormField>,
)

expect(screen.getByLabelText(label)).toHaveAttribute('readOnly')
})

it('should be disabled when field is disabled', () => {
const label = 'Title'

render(
<FormField disabled>
<FormField.Label>{label}</FormField.Label>

<Input />
</FormField>,
)

expect(screen.getByLabelText(label)).toBeDisabled()
})
})
6 changes: 3 additions & 3 deletions packages/components/input/src/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ const Root = forwardRef<HTMLInputElement, InputProps>(
onClear,
} = group
const Component = asChild ? Slot : 'input'
const state = field.state ?? group.state
const disabled = group.disabled ?? disabledProp
const readOnly = group.readOnly ?? readOnlyProp
const state = field.state || group.state
const disabled = field.disabled || group.disabled || disabledProp
const readOnly = field.readOnly || group.readOnly || readOnlyProp

const handleChange: ChangeEventHandler<HTMLInputElement> = event => {
if (onChange) {
Expand Down
12 changes: 6 additions & 6 deletions packages/components/input/src/InputGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ export const InputGroup = forwardRef<HTMLDivElement, PropsWithChildren<InputGrou

const current = useMemo(() => {
return {
state,
disabled,
readOnly,
state: stateProp,
disabled: !!disabledProp,
readOnly: !!readOnlyProp,
hasLeadingIcon,
hasTrailingIcon,
hasLeadingAddon,
Expand All @@ -113,9 +113,9 @@ export const InputGroup = forwardRef<HTMLDivElement, PropsWithChildren<InputGrou
onClear: handleClear,
}
}, [
state,
disabled,
readOnly,
stateProp,
disabledProp,
readOnlyProp,
hasLeadingIcon,
hasTrailingIcon,
hasLeadingAddon,
Expand Down

0 comments on commit 936dc4d

Please sign in to comment.