import * as React from 'react' import styled, { css } from 'styled-components' import type { IconProps } from '@planview/pv-icons' import Label from '../label' import Error from '../error' import { spacingPx, useResizeObserver } from '@planview/pv-utilities' import Counter from '../counter' import Warning from '../warning' /** * Under this width, the left placed label will get placed above */ const LEFT_LABEL_MIN_WIDTH = 300 const FieldWrap = styled.div<{ $labelPosition: 'top' | 'left' $hasBottom: boolean $hasLabel?: boolean }>` /* stylelint-disable declaration-block-no-redundant-longhand-properties -- turning off so grid-template rules below can be separate */ display: grid; ${(props) => props.$labelPosition === 'left' ? props.$hasBottom ? css` grid-template-columns: 160px auto; grid-template-rows: auto auto; grid-template-areas: 'left main' 'left bottom'; ` : css` grid-template-columns: 160px auto; grid-template-rows: auto; grid-template-areas: 'left main'; ` : css` grid-template-areas: 'left' 'main' ${props.$hasBottom ? "'bottom'" : undefined}; `}; gap: ${(props) => !props.$hasBottom && !props.$hasLabel ? 0 : spacingPx.xsmall}; width: 100%; margin-top: ${spacingPx.xsmall}; margin-bottom: ${spacingPx.small}; height: fit-content; ` const StyledLabel = styled(Label)<{ $alignRight: boolean }>` grid-area: left; display: block; ${(props) => props.$alignRight ? `margin-top: ${spacingPx.small}; margin-right: ${spacingPx.medium}` : null} ` const BottomWrap = styled.div<{ $alignRight: boolean }>` display: flex; justify-content: ${(props) => props.$alignRight ? 'flex-end' : 'space-between'}; grid-area: bottom; ` const VisuallyHidden = styled.p` display: none; ` type LabelConfig = { text: string required?: boolean requiredTooltip?: string icon?: React.ReactElement iconTooltip?: string } type ReturnProps = { 'aria-errormessage'?: string 'aria-describedby'?: string 'aria-labelledby': string id: string } export type FieldProps = { /** * id of field */ id: string /** * Label associated with the field, can be string or configuration */ label?: string | LabelConfig /** * Layout of label */ labelPosition?: 'top' | 'left' /** * Error message placed bottom left */ error?: string | React.ReactNode /** * Warning message placed bottom left. Will be replaced by error message if both are provided */ warning?: string | React.ReactNode /** * Function to return children */ children: (props: ReturnProps) => React.ReactNode /** * If asterisk should be displayed in label. This prop will be overridden if using a label configuration */ withAsterisk?: boolean /** * Counter placed bottom right. If an object is provided, the color of the counter * text can be controlled by passing a status. */ count?: string | { text: string; status: 'default' | 'warning' | 'error' } /** * Determines if the field label is rendered as a `label` or `span` element. When true, an `htmlFor` will be added to * the internal label to properly associate it with a valid form control. Use false when * this relationship would be semantically or technically incorrect. */ useLabelElement?: boolean /** * For use when emulating native browser behavior when clicking a label focuses the related element. * If useLabelElement is false, you may want to provide this callback and programmatically focus the * related element. */ onLabelClick?: React.MouseEventHandler } function getLabelId(id: string, type: string) { return id.includes('pv-form') ? `${id}-${type}` : `${id}-pv-form-${type}` } /** * Implementation of the [Form Field specification](https://design.planview.com/components/forms/form-field) * * Field wrapper to connect form elements with label and error messages. * The components uses a [render-props](https://reactjs.org/docs/render-props.html) function with a single argument, to provide the children with the props to be applied to the form element * * __Note: This is a generic wrapper for use when there is no out-of-the-box solution in this library. Always defer to the regular components in `pv-form` if they handle the use-case.__ * * ### Example usage * ```jsx * import { Field } from '@planview/pv-form' * * 1/20
} > * {(props) => { * return * }} * * ``` * */ const Field: React.FC = ({ id, label, error, warning, children, withAsterisk, labelPosition = 'top', useLabelElement = true, onLabelClick, count, }) => { const ref = React.useRef(null) const { width } = useResizeObserver({ ref, }) const labelId = getLabelId(id, 'label') const errorId = error ? getLabelId(id, 'error') : '' const warningId = !error && warning ? getLabelId(id, 'warning') : '' const counterId = count ? getLabelId(id, 'counter') : '' const iconTooltipId = typeof label !== 'string' && label?.iconTooltip ? getLabelId(id, 'tooltip') : '' const iconTooltip = typeof label !== 'string' && label?.iconTooltip ? label.iconTooltip : '' const returnProps: ReturnProps = React.useMemo(() => { const ret: ReturnProps = { id: id, 'aria-labelledby': labelId, 'aria-describedby': [iconTooltipId, errorId, counterId, warningId] .filter((id) => id.length) .join(' '), } return ret }, [id, labelId, iconTooltipId, errorId, counterId, warningId]) labelPosition = !width || width > LEFT_LABEL_MIN_WIDTH ? labelPosition : 'top' const contentAbove = React.useMemo(() => { if (label) { const labelProps: { useLabelElement: boolean $alignRight: boolean id: string htmlFor?: string onClick?: React.MouseEventHandler } = { useLabelElement, $alignRight: labelPosition === 'left', id: labelId, onClick: onLabelClick, } if (useLabelElement) { labelProps.htmlFor = id } if (typeof label === 'string') { return ( {label} ) } else { const { text, required, requiredTooltip, iconTooltip, icon } = label return ( <> {text} ) } } return null }, [ id, labelId, label, withAsterisk, labelPosition, useLabelElement, onLabelClick, ]) const hasBottom = !!(warning || error || count !== undefined) return ( <> {contentAbove} {iconTooltipId ? ( {iconTooltip} ) : null} {children(returnProps)} {hasBottom ? ( {error ? ( {error} ) : warning ? ( {warning} ) : null} {count !== undefined && ( {typeof count === 'string' ? count : count.text} )} ) : null} ) } export { Field } export default Field