Skip to content

Commit

Permalink
Merge branch 'main' of github.com:mongodb/leafygreen-ui into LG-4105-…
Browse files Browse the repository at this point in the history
…codemod-scaffold
  • Loading branch information
shaneeza committed May 22, 2024
2 parents 00a2070 + 3273045 commit fc2feac
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/clever-laws-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@leafygreen-ui/form-field': patch
---

Correctly sets the `readOnly` prop in the `useFormFieldProps` hook. Previously, `readOnly` was only added to an input if `disabled` was true. Now, passing the `readOnly` prop adds it to the resulting input.
5 changes: 5 additions & 0 deletions .changeset/eight-clocks-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@leafygreen-ui/text-input': patch
---

Passes `readOnly` prop to `<FormField>` so that it correctly sets the `readOnly` prop.
5 changes: 5 additions & 0 deletions packages/date-picker/src/DatePicker.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ export const LiveExample: StoryFn<typeof DatePicker> = props => {
</div>
);
};
LiveExample.parameters = {
chromatic: {
disableSnapshots: true,
},
};

export const Uncontrolled: StoryFn<typeof DatePicker> = props => {
return <DatePicker {...props} />;
Expand Down
43 changes: 43 additions & 0 deletions packages/form-field/src/FormField.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,49 @@ describe('packages/form-field', () => {
expect(label.getAttribute('for')).toBe(id);
});

describe('readonly', () => {
test('input has readonly prop', () => {
const { getByTestId } = render(
<FormField label="Label" data-testid="form-field" readOnly>
<FormFieldInputContainer>
<div data-testid="input" />
</FormFieldInputContainer>
</FormField>,
);
const input = getByTestId('input');
expect(input).toHaveAttribute('readonly');
});

test('disabled overrides readOnly prop', () => {
const { getByTestId } = render(
<FormField
label="Label"
data-testid="form-field"
readOnly={false}
disabled
>
<FormFieldInputContainer>
<div data-testid="input" />
</FormFieldInputContainer>
</FormField>,
);
const input = getByTestId('input');
expect(input).toHaveAttribute('readonly');
});

test('input does not have readonly prop', () => {
const { getByTestId } = render(
<FormField label="Label" data-testid="form-field" readOnly={false}>
<FormFieldInputContainer>
<div data-testid="input" />
</FormFieldInputContainer>
</FormField>,
);
const input = getByTestId('input');
expect(input).not.toHaveAttribute('readonly');
});
});

// eslint-disable-next-line jest/no-disabled-tests
test.skip('Types', () => {
render(
Expand Down
5 changes: 5 additions & 0 deletions packages/form-field/src/FormField/FormField.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,9 @@ export type FormFieldProps = Omit<HTMLElementProps<'div'>, 'children'> &
* Whether or not the field is labeled as optional.
*/
optional?: boolean;

/**
* Whether or not the field is readonly.
*/
readOnly?: boolean;
};
2 changes: 1 addition & 1 deletion packages/form-field/src/FormField/useFormFieldProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const useFormFieldProps = ({
'aria-describedby': ariaDescribedby,
'aria-label': ariaLabel,
'aria-disabled': disabled,
readOnly: disabled,
readOnly: rest.readOnly ? rest.readOnly : disabled,
'aria-invalid': ariaInvalid,
};

Expand Down
2 changes: 2 additions & 0 deletions packages/text-input/src/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const TextInput = React.forwardRef<HTMLInputElement, TextInputProps>(
state = State.None,
type = TextInputType.Text,
id,
readOnly,
value: controlledValue,
className,
darkMode: darkModeProp,
Expand Down Expand Up @@ -158,6 +159,7 @@ const TextInput = React.forwardRef<HTMLInputElement, TextInputProps>(
optional,
size,
state,
readOnly,
...ariaProps,
} as const;

Expand Down

0 comments on commit fc2feac

Please sign in to comment.