Skip to content

Commit

Permalink
fix(Input Base): Fixed case where default value of 0 did not render t…
Browse files Browse the repository at this point in the history
…he default value in the input field (#1109)

Co-authored-by: Jerrod Heiser <[email protected]>
  • Loading branch information
silvalaura and jerrodheiser authored Jul 12, 2023
1 parent 15c4632 commit 7c8a9f5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/fix-inputBase.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-magma-dom': patch
---

fix(Input Base): Fixed case where default value of `0` did not render the default value in the input field.
20 changes: 20 additions & 0 deletions packages/react-magma-dom/src/components/Input/Input.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,26 @@ describe('Input', () => {
expect(getByLabelText(iconAriaLabel)).toBeDisabled();
});

it('should initially render with value of `defaultValue` if no `value` provided', () => {
const labelText = 'test label';
const defaultValue = 'defaultValue';
const { getByLabelText } = render(
<Input labelText={labelText} defaultValue={defaultValue} />
);

expect(getByLabelText(labelText)).toHaveAttribute('value', defaultValue);
});

it('should render with value "0" when `defaultValue` is 0', () => {
const labelText = 'test label';
const defaultValue = 0;
const { getByLabelText } = render(
<Input labelText={labelText} defaultValue={defaultValue} />
);

expect(getByLabelText(labelText)).toHaveAttribute('value', '0');
});

it('should render an input with a value passed through', () => {
const labelText = 'test label';
const value = 'Test Value';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export const Default = args => {
labelText="Input"
isClearable
/>
<Input
defaultValue={0}
labelText="Default value `0`"
isClearable
/>
<PasswordInput
errorMessage="danger will robinson."
labelText="PasswordInput"
Expand Down Expand Up @@ -141,6 +146,12 @@ export const Inverse = args => {
isClearable
isInverse
/>
<Input
defaultValue={0}
labelText="Default value `0`"
isClearable
isInverse
/>
<PasswordInput
errorMessage="danger will robinson."
labelText="PasswordInput"
Expand Down
9 changes: 7 additions & 2 deletions packages/react-magma-dom/src/components/InputBase/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,12 @@ export const InputBase = React.forwardRef<HTMLInputElement, InputBaseProps>(

const [value, setValue] = React.useState<
string | ReadonlyArray<string> | number
>(props.defaultValue || props.value || '');
>(
props.defaultValue !== undefined &&
props.defaultValue !== null
? props.defaultValue
: props.value || ''
);

const maxLengthNum = !hasCharacterCounter && maxLength ? maxLength : undefined;

Expand Down Expand Up @@ -654,7 +659,7 @@ export const InputBase = React.forwardRef<HTMLInputElement, InputBaseProps>(
</IconWrapper>
)}
</InputWrapper>
{isClearable && value && (
{isClearable && value !== '' && (
<IsClearableContainer
theme={theme}
iconPosition={iconPosition}
Expand Down

2 comments on commit 7c8a9f5

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.