Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Move CheckboxControl to components package and leave alias in checkout package #11662

Merged
merged 3 commits into from
Nov 21, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import { useEditorContext } from '@woocommerce/base-context';
import { CheckboxControl } from '@woocommerce/blocks-checkout';
import { CheckboxControl } from '@woocommerce/blocks-components';
import { useSelect, useDispatch } from '@wordpress/data';
import { CHECKOUT_STORE_KEY, PAYMENT_STORE_KEY } from '@woocommerce/block-data';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
RichText,
InspectorControls,
} from '@wordpress/block-editor';
import { CheckboxControl } from '@woocommerce/blocks-checkout';
import { CheckboxControl } from '@woocommerce/blocks-components';
import {
PanelBody,
ToggleControl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { __ } from '@wordpress/i18n';
import classnames from 'classnames';
import { useState, useEffect } from '@wordpress/element';
import { CheckboxControl } from '@woocommerce/blocks-checkout';
import { CheckboxControl } from '@woocommerce/blocks-components';
import { useCheckoutSubmit } from '@woocommerce/base-context/hooks';
import { withInstanceId } from '@wordpress/compose';
import { useDispatch, useSelect } from '@wordpress/data';
Expand Down
3 changes: 1 addition & 2 deletions assets/js/blocks/checkout/order-notes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import { CheckboxControl } from '@woocommerce/blocks-checkout';
import { Textarea } from '@woocommerce/blocks-components';
import { CheckboxControl, Textarea } from '@woocommerce/blocks-components';

interface CheckoutOrderNotesProps {
disabled: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Import styles we need to render the checkbox list and checkbox control.
@import "../../../../../../packages/components/checkbox-list/style";
@import "../../../../../../packages/checkout/components/checkbox-control/style";
@import "../../../../../../packages/components/checkbox-control/style";


.wp-block-woocommerce-stock-filter {
Expand Down
81 changes: 1 addition & 80 deletions packages/checkout/components/checkbox-control/index.tsx
Original file line number Diff line number Diff line change
@@ -1,80 +1 @@
/**
* External dependencies
*/
import classNames from 'classnames';
import { useInstanceId } from '@wordpress/compose';

/**
* Internal dependencies
*/
import './style.scss';

export type CheckboxControlProps = {
className?: string;
label?: string | React.ReactNode;
id?: string;
onChange: ( value: boolean ) => void;
children?: React.ReactChildren;
hasError?: boolean;
checked?: boolean;
disabled?: boolean;
};

/**
* Component used to show a checkbox control with styles.
*/
export const CheckboxControl = ( {
className,
label,
id,
onChange,
children,
hasError = false,
checked = false,
disabled = false,
...rest
}: CheckboxControlProps ): JSX.Element => {
const instanceId = useInstanceId( CheckboxControl );
const checkboxId = id || `checkbox-control-${ instanceId }`;

return (
<div
className={ classNames(
'wc-block-components-checkbox',
{
'has-error': hasError,
},
className
) }
>
<label htmlFor={ checkboxId }>
<input
id={ checkboxId }
className="wc-block-components-checkbox__input"
type="checkbox"
onChange={ ( event ) => onChange( event.target.checked ) }
aria-invalid={ hasError === true }
checked={ checked }
disabled={ disabled }
{ ...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>
);
};

export default CheckboxControl;
export { CheckboxControl } from '../../../components/checkbox-control';
10 changes: 7 additions & 3 deletions packages/checkout/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ export { default as Panel } from '../../components/panel';
export { default as Button } from '../../components/button';
export { default as Label } from './label';
export { default as StoreNoticesContainer } from '../../components/store-notices-container';
export { default as CheckboxControl } from './checkbox-control';
export { ValidationInputError } from './validation-input-error';
export { ValidatedTextInput, TextInput } from './text-input';
export { CheckboxControl } from './checkbox-control';
export {
default as ValidatedTextInput,
ValidatedTextInputHandle,
} from '../../components/text-input/validated-text-input';
export { default as TextInput } from '../../components/text-input/text-input';
export { default as ValidationInputError } from '../../components/validation-input-error';
export { default as StoreNotice } from '../../components/store-notice';
80 changes: 80 additions & 0 deletions packages/components/checkbox-control/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* External dependencies
*/
import classNames from 'classnames';
import { useInstanceId } from '@wordpress/compose';

/**
* Internal dependencies
*/
import './style.scss';

export type CheckboxControlProps = {
className?: string;
label?: string | React.ReactNode;
id?: string;
onChange: ( value: boolean ) => void;
children?: React.ReactChildren;
hasError?: boolean;
checked?: boolean;
disabled?: boolean;
};

/**
* Component used to show a checkbox control with styles.
*/
export const CheckboxControl = ( {
className,
label,
id,
onChange,
children,
hasError = false,
checked = false,
disabled = false,
...rest
}: CheckboxControlProps ): JSX.Element => {
const instanceId = useInstanceId( CheckboxControl );
const checkboxId = id || `checkbox-control-${ instanceId }`;

return (
<div
className={ classNames(
'wc-block-components-checkbox',
{
'has-error': hasError,
},
className
) }
>
<label htmlFor={ checkboxId }>
<input
id={ checkboxId }
className="wc-block-components-checkbox__input"
type="checkbox"
onChange={ ( event ) => onChange( event.target.checked ) }
aria-invalid={ hasError === true }
checked={ checked }
disabled={ disabled }
{ ...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>
);
};

export default CheckboxControl;
3 changes: 1 addition & 2 deletions packages/components/checkbox-list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import { __, _n, sprintf } from '@wordpress/i18n';
import { Fragment, useMemo, useState } from '@wordpress/element';
import classNames from 'classnames';
import { CheckboxControl } from '@woocommerce/blocks-checkout';

/**
* Internal dependencies
*/
import './style.scss';

import { CheckboxControl } from '../checkbox-control';
interface CheckboxListOptions {
label: React.ReactNode;
value: string;
Expand Down
1 change: 1 addition & 0 deletions packages/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as Button } from './button';
export { default as CheckboxControl } from './checkbox-control';
export { default as CheckboxList } from './checkbox-list';
export { Chip, RemovableChip } from './chip';
export { default as FormStep } from './form-step';
Expand Down
Loading