2020-03-06 10:26:03 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import classNames from 'classnames';
|
2022-02-22 17:45:01 +00:00
|
|
|
import { useInstanceId } from '@wordpress/compose';
|
2020-03-06 10:26:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
|
2021-12-21 17:08:57 +00:00
|
|
|
export type CheckboxControlProps = {
|
2021-07-22 11:03:00 +00:00
|
|
|
className?: string;
|
|
|
|
label?: string;
|
|
|
|
id?: string;
|
|
|
|
onChange: ( value: boolean ) => void;
|
2022-02-22 17:45:01 +00:00
|
|
|
children?: React.ReactChildren;
|
|
|
|
hasError?: boolean;
|
|
|
|
checked?: boolean;
|
2021-07-22 11:03:00 +00:00
|
|
|
};
|
|
|
|
|
2020-03-06 10:26:03 +00:00
|
|
|
/**
|
|
|
|
* Component used to show a checkbox control with styles.
|
|
|
|
*/
|
2021-12-21 17:08:57 +00:00
|
|
|
export const CheckboxControl = ( {
|
2020-04-07 15:37:03 +00:00
|
|
|
className,
|
|
|
|
label,
|
|
|
|
id,
|
|
|
|
onChange,
|
2021-07-22 11:03:00 +00:00
|
|
|
children,
|
2021-09-14 11:26:41 +00:00
|
|
|
hasError = false,
|
2022-02-22 17:45:01 +00:00
|
|
|
checked = false,
|
2020-04-07 15:37:03 +00:00
|
|
|
...rest
|
2021-07-22 11:03:00 +00:00
|
|
|
}: CheckboxControlProps ): JSX.Element => {
|
2022-02-22 17:45:01 +00:00
|
|
|
const instanceId = useInstanceId( CheckboxControl );
|
2020-04-07 15:37:03 +00:00
|
|
|
const checkboxId = id || `checkbox-control-${ instanceId }`;
|
|
|
|
|
2020-03-06 10:26:03 +00:00
|
|
|
return (
|
2021-11-22 08:52:43 +00:00
|
|
|
<div
|
2020-06-17 09:53:42 +00:00
|
|
|
className={ classNames(
|
|
|
|
'wc-block-components-checkbox',
|
2021-09-14 11:26:41 +00:00
|
|
|
{
|
|
|
|
'has-error': hasError,
|
|
|
|
},
|
2020-06-17 09:53:42 +00:00
|
|
|
className
|
|
|
|
) }
|
2020-04-07 15:37:03 +00:00
|
|
|
>
|
2021-11-22 08:52:43 +00:00
|
|
|
<label htmlFor={ checkboxId }>
|
|
|
|
<input
|
|
|
|
id={ checkboxId }
|
|
|
|
className="wc-block-components-checkbox__input"
|
|
|
|
type="checkbox"
|
|
|
|
onChange={ ( event ) => onChange( event.target.checked ) }
|
|
|
|
aria-invalid={ hasError === true }
|
2022-02-22 17:45:01 +00:00
|
|
|
checked={ checked }
|
2021-11-22 08:52:43 +00:00
|
|
|
{ ...rest }
|
|
|
|
/>
|
|
|
|
<svg
|
|
|
|
className="wc-block-components-checkbox__mark"
|
|
|
|
aria-hidden="true"
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
viewBox="0 0 24 20"
|
|
|
|
>
|
|
|
|
<path d="M9 16.2L4.8 12l-1.4 1.4L9 19 21 7l-1.4-1.4L9 16.2z" />
|
|
|
|
</svg>
|
|
|
|
{ label && (
|
|
|
|
<span className="wc-block-components-checkbox__label">
|
|
|
|
{ label }
|
|
|
|
</span>
|
|
|
|
) }
|
|
|
|
{ children }
|
|
|
|
</label>
|
|
|
|
</div>
|
2020-03-06 10:26:03 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-02-22 17:45:01 +00:00
|
|
|
export default CheckboxControl;
|