import React, { createContext, HTMLAttributes, ReactNode, useMemo, } from 'react'; import classnames from 'classnames'; import { FormElement } from './FormElement'; import { createFC } from './common'; /** * */ export const FieldSetColumnContext = createContext<{ isFieldSetColumn?: boolean; totalCols?: number; }>({}); /** * */ type FieldSetRowProps = { className?: string; cols?: number; children?: ReactNode; }; /** * */ export const FieldSetRow = createFC< FieldSetRowProps, { isFormElement: boolean } >( (props) => { const { className, cols, children } = props; const totalCols = cols || React.Children.count(children); const ctx = useMemo( () => ({ isFieldSetColumn: true, totalCols }), [totalCols] ); const rowClassNames = classnames(className, 'slds-form-element__row'); return (
{React.Children.map(children, (child) => { if ( React.isValidElement(child) && !(child.type as unknown as { isFormElement?: boolean }) .isFormElement ) { return {child}; } return child; })}
); }, { isFormElement: true } ); /** * */ export type FieldSetProps = { label?: string; } & HTMLAttributes; /** * */ export const FieldSet = createFC< FieldSetProps, { isFormElement: boolean; Row: typeof FieldSetRow } >( ({ className, label, children, ...props }) => { const fsClassNames = classnames( className, 'slds-form-element', 'slds-form-element_compound' ); const legendClassNames = classnames( 'slds-form-element__legend', 'slds-form-element__label' ); return (
{label ? {label} : null}
{children}
); }, { isFormElement: true, Row: FieldSetRow } );