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

Develop fix checkbox hidden label uu #1944

Merged
merged 2 commits into from
Apr 29, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions component-overview/examples/form/Checkbox-hiddenLabel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Checkbox } from '@sb1/ffe-form-react';

<Checkbox
defaultChecked={true}
aria-label="Jeg har en ingen label"
hiddenLabel={true}
inline={false}
/>
>Jeg har en ingen label</Checkbox>
89 changes: 51 additions & 38 deletions packages/ffe-form-react/src/Checkbox.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,57 @@
import React, { Fragment } from 'react';
import React from 'react';
import { bool, node, string, func, oneOfType } from 'prop-types';
import { v4 as hash } from 'uuid';
import { v4 as uuid } from 'uuid';
import classNames from 'classnames';

const Checkbox = React.forwardRef((props, ref) => {
const { children, hiddenLabel, inline = true, noMargins, ...rest } = props;
const Checkbox = React.forwardRef(
(
{
children,
hiddenLabel,
inline = true,
noMargins,
id = `checkbox-${uuid()}`,
...rest
},
ref,
) => {
const labelProps = {
className: classNames({
'ffe-checkbox': true,
'ffe-checkbox--inline': inline,
'ffe-checkbox--no-margin': noMargins,
'ffe-checkbox--hidden-label': hiddenLabel,
}),
htmlFor: id,
};

const id = props.id || `checkbox-${hash()}`;
const labelProps = {
className: classNames({
'ffe-checkbox': true,
'ffe-checkbox--inline': inline,
'ffe-checkbox--no-margin': noMargins,
'ffe-checkbox--hidden-label': hiddenLabel,
}),
htmlFor: id,
};

return (
<Fragment>
<input
ref={ref}
className="ffe-hidden-checkbox"
id={id}
type="checkbox"
{...rest}
/>
{typeof children === 'function' ? (
children(labelProps)
) : (
// eslint-disable-next-line jsx-a11y/label-has-for
<label {...labelProps}>
<span className="ffe-checkbox__content">
{!hiddenLabel && children}
</span>
</label>
)}
</Fragment>
);
});
return (
<>
<input
ref={ref}
className="ffe-hidden-checkbox"
id={id}
type="checkbox"
{...rest}
/>
{typeof children === 'function' ? (
children(labelProps)
) : (
// eslint-disable-next-line jsx-a11y/label-has-for
<label {...labelProps}>
<span
className={classNames('ffe-checkbox__content', {
'ffe-screenreader-only': hiddenLabel,
})}
>
{children}
</span>
</label>
)}
</>
);
},
);

Checkbox.propTypes = {
/** Removes vertical margins from the checkbox */
Expand All @@ -49,7 +62,7 @@ Checkbox.propTypes = {
id: string,
inline: bool,
/** The label for the checkbox */
children: oneOfType([node, func]),
children: oneOfType([node, func]).isRequired,
};

export default Checkbox;
4 changes: 2 additions & 2 deletions packages/ffe-form-react/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as React from 'react';

export interface CheckboxProps
extends Omit<React.ComponentProps<'input'>, 'children'> {
extends Omit<React.ComponentPropsWithoutRef<'input'>, 'children'> {
noMargins?: boolean;
hiddenLabel?: boolean;
id?: string;
inline?: boolean;
children?:
| React.ReactNode
| NonNullable<React.ReactNode>
| ((labelProps: {
className: string;
htmlFor: string;
Expand Down
Loading