Migrate Section component to TS (#36298)

* Convert to ts

* Add changelog

* Fix style

* Remove propTypes

* Remove unused import

* Only allow false

* Update packages/js/components/src/section/section.tsx

Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com>

* Update packages/js/components/src/section/section.tsx

Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com>

* Update packages/js/components/src/section/section.tsx

Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com>

Co-authored-by: Chi-Hsuan Huang <chihsuan.tw@gmail.com>
This commit is contained in:
Moon 2023-01-12 13:57:15 -08:00 committed by GitHub
parent 04d371b7e0
commit 4ff99a8555
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 57 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: dev
Migrate Section component to TS

View File

@ -15,7 +15,7 @@ import { Level } from './context';
*
* @type {HTMLElement}
*/
export function H( props ) {
export function H( props: React.HTMLAttributes< HTMLHeadingElement > ) {
const level = useContext( Level );
const Heading = 'h' + Math.min( level, 6 );

View File

@ -1,56 +0,0 @@
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import { createElement } from '@wordpress/element';
/**
* Internal dependencies
*/
import { Level } from './context';
/**
* The section wrapper, used to indicate a sub-section (and change the header level context).
*
* @param {Object} props
* @param {import('react').ComponentType=} props.component
* @param {import('react').ReactNode} props.children Children to render in the tip.
* @param {string=} props.className
* @return {JSX.Element} -
*/
export function Section( { component, children, ...props } ) {
const Component = component || 'div';
return (
<Level.Consumer>
{ ( level ) => (
<Level.Provider value={ level + 1 }>
{ component === false ? (
children
) : (
<Component { ...props }>{ children }</Component>
) }
</Level.Provider>
) }
</Level.Consumer>
);
}
Section.propTypes = {
/**
* The wrapper component for this section. Optional, defaults to `div`. If passed false, no wrapper is used. Additional props
* passed to Section are passed on to the component.
*/
component: PropTypes.oneOfType( [
PropTypes.func,
PropTypes.string,
PropTypes.bool,
] ),
/**
* The children inside this section, rendered in the `component`. This increases the context level for the next heading used.
*/
children: PropTypes.node,
/**
* Optional classname
*/
className: PropTypes.string,
};

View File

@ -0,0 +1,42 @@
/**
* External dependencies
*/
import { createElement } from '@wordpress/element';
/**
* Internal dependencies
*/
import { Level } from './context';
type SectionProps = {
/** The wrapper component for this section. Optional, defaults to `div`. If passed false, no wrapper is used. Additional props passed to Section are passed on to the component. */
component?: React.ComponentType | string | false;
/** Optional classname */
className?: string;
/** The children inside this section, rendered in the `component`. This increases the context level for the next heading used. */
children: React.ReactNode;
};
/**
* The section wrapper, used to indicate a sub-section (and change the header level context).
*/
export const Section: React.VFC< SectionProps > = ( {
component,
children,
...props
} ) => {
const Component = component || 'div';
return (
<Level.Consumer>
{ ( level ) => (
<Level.Provider value={ level + 1 }>
{ component === false ? (
children
) : (
<Component { ...props }>{ children }</Component>
) }
</Level.Provider>
) }
</Level.Consumer>
);
};

View File

@ -2,6 +2,7 @@
* External dependencies
*/
import { H, Section } from '@woocommerce/components';
import { createElement } from '@wordpress/element';
export const Basic = () => (
<div>