Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Checkbox][Joy UI] spread value, required, and readOnly to input #34477

Merged
merged 2 commits into from Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/mui-joy/src/Checkbox/Checkbox.test.js
Expand Up @@ -34,6 +34,18 @@ describe('<Checkbox />', () => {
expect(getByRole('checkbox')).to.have.property('name', 'bar');
});

it('renders a `role="checkbox"` with required attribute', () => {
const { getByRole } = render(<Checkbox name="bar" required />);

expect(getByRole('checkbox')).to.have.attribute('required');
});

it('renders a `role="checkbox"` with readOnly attribute', () => {
const { getByRole } = render(<Checkbox name="bar" readOnly />);

expect(getByRole('checkbox')).to.have.attribute('readonly');
});

it('renders a `role="checkbox"` with the Unchecked state by default', () => {
const { getByRole } = render(<Checkbox />);

Expand Down Expand Up @@ -108,5 +120,10 @@ describe('<Checkbox />', () => {
const { getByTestId } = render(<Checkbox indeterminate />);
expect(getByTestId('HorizontalRuleIcon')).not.to.equal(null);
});

it('should have aria-checked="mixed"', () => {
const { getByRole } = render(<Checkbox indeterminate />);
expect(getByRole('checkbox')).to.have.attribute('aria-checked', 'mixed');
});
});
});
22 changes: 22 additions & 0 deletions packages/mui-joy/src/Checkbox/Checkbox.tsx
Expand Up @@ -202,7 +202,9 @@ const Checkbox = React.forwardRef(function Checkbox(inProps, ref) {
onChange,
onFocus,
onFocusVisible,
readOnly,
required,
value,
color: colorProp,
variant,
size: sizeProp = 'md',
Expand Down Expand Up @@ -294,7 +296,14 @@ const Checkbox = React.forwardRef(function Checkbox(inProps, ref) {
additionalProps: {
id,
name,
value,
readOnly,
required,
'aria-describedby': formControl?.['aria-describedby'],
...(indeterminate && {
// https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-checked#values
'aria-checked': 'mixed' as const,
}),
},
className: classes.input,
});
Expand Down Expand Up @@ -438,6 +447,10 @@ Checkbox.propTypes /* remove-proptypes */ = {
* @default false
*/
overlay: PropTypes.bool,
/**
* If `true`, the component is read only.
*/
readOnly: PropTypes.bool,
/**
* If `true`, the `input` element is required.
*/
Expand All @@ -462,6 +475,15 @@ Checkbox.propTypes /* remove-proptypes */ = {
* The icon when `checked` is false.
*/
uncheckedIcon: PropTypes.node,
/**
* The value of the component. The DOM API casts this to a string.
* The browser uses "on" as the default value.
*/
value: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.string),
PropTypes.number,
PropTypes.string,
]),
/**
* The variant to use.
* @default 'solid'
Expand Down
5 changes: 5 additions & 0 deletions packages/mui-joy/src/Checkbox/CheckboxProps.ts
Expand Up @@ -92,6 +92,11 @@ export interface CheckboxTypeMap<P = {}, D extends React.ElementType = 'span'> {
* The icon when `checked` is false.
*/
uncheckedIcon?: React.ReactNode;
/**
* The value of the component. The DOM API casts this to a string.
* The browser uses "on" as the default value.
*/
value?: React.AllHTMLAttributes<HTMLInputElement>['value'];
};
defaultComponent: D;
}
Expand Down