-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aca86dc
commit 790fa13
Showing
9 changed files
with
162 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { | ||
InputGroup, | ||
type InputGroupCaptionProps, | ||
type InputGroupLabelProps, | ||
type InputGroupProps, | ||
type InputGroupValidationProps, | ||
} from '../InputGroup' | ||
ControlGroup, | ||
type ControlGroupCaptionProps, | ||
type ControlGroupLabelProps, | ||
type ControlGroupProps, | ||
type ControlGroupValidationProps, | ||
} from '../ControlGroup' | ||
|
||
export type CheckboxGroupProps = InputGroupProps | ||
export type CheckboxGroupLabelProps = InputGroupLabelProps | ||
export type CheckboxGroupCaptionProps = InputGroupCaptionProps | ||
export type CheckboxGroupValidationProps = InputGroupValidationProps | ||
export type CheckboxGroupProps = ControlGroupProps | ||
export type CheckboxGroupLabelProps = ControlGroupLabelProps | ||
export type CheckboxGroupCaptionProps = ControlGroupCaptionProps | ||
export type CheckboxGroupValidationProps = ControlGroupValidationProps | ||
|
||
export const CheckboxGroup = InputGroup | ||
export const CheckboxGroup = ControlGroup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import clsx from 'clsx' | ||
import React, {Children, createContext, forwardRef, isValidElement, useContext, type HTMLAttributes} from 'react' | ||
import {AlertFillIcon, CheckCircleFillIcon} from '@primer/octicons-react' | ||
|
||
import styles from './ControlGroup.module.css' | ||
import type {FormValidationStatus} from '..' | ||
import {useId} from '@reach/auto-id' | ||
|
||
type ControlGroupContext = { | ||
id?: string | ||
} | ||
|
||
const ControlGroupContext = createContext<ControlGroupContext | null>(null) | ||
|
||
const useControlGroup = () => { | ||
const context = useContext(ControlGroupContext) | ||
|
||
if (!context) { | ||
throw new Error( | ||
'useControlGroup must be used within an ControlGroupProvider. Did you forget to wrap your component in a <ControlGroupProvider>?', | ||
) | ||
} | ||
|
||
return context | ||
} | ||
|
||
export type ControlGroupProps = HTMLAttributes<HTMLFieldSetElement> | ||
const _ControlGroup = forwardRef<HTMLFieldSetElement, ControlGroupProps>(({className, id, ...props}, ref) => { | ||
const uniqueId = useId(id) | ||
|
||
const containsCaption = Children.toArray(props.children).some( | ||
child => isValidElement(child) && child.type === ControlGroupCaption, | ||
) | ||
const containsValidation = Children.toArray(props.children).some( | ||
child => isValidElement(child) && child.type === ControlGroupValidation, | ||
) | ||
|
||
const describedBy = | ||
[containsCaption && `${uniqueId}-caption`, containsValidation && `${uniqueId}-validation`] | ||
.filter(Boolean) | ||
.join(' ') || undefined | ||
|
||
return ( | ||
<ControlGroupContext.Provider value={{id: uniqueId}}> | ||
<fieldset | ||
ref={ref} | ||
className={clsx(styles.ControlGroup__container, className)} | ||
aria-describedby={describedBy} | ||
{...props} | ||
/> | ||
</ControlGroupContext.Provider> | ||
) | ||
}) | ||
|
||
export type ControlGroupLabelProps = {visuallyHidden?: boolean} & HTMLAttributes<HTMLLegendElement> | ||
const ControlGroupLabel = forwardRef<HTMLLegendElement, ControlGroupLabelProps>( | ||
({className, visuallyHidden, ...props}, ref) => { | ||
return ( | ||
<legend | ||
ref={ref} | ||
className={clsx(styles.ControlGroup__label, visuallyHidden && 'visually-hidden', className)} | ||
{...props} | ||
/> | ||
) | ||
}, | ||
) | ||
|
||
export type ControlGroupCaptionProps = HTMLAttributes<HTMLSpanElement> | ||
const ControlGroupCaption = forwardRef<HTMLSpanElement, ControlGroupCaptionProps>(({className, ...props}, ref) => { | ||
const {id} = useControlGroup() | ||
|
||
return <span ref={ref} id={`${id}-caption`} className={clsx(styles.ControlGroup__caption, className)} {...props} /> | ||
}) | ||
|
||
export type ControlGroupValidationProps = {variant: FormValidationStatus} & HTMLAttributes<HTMLSpanElement> | ||
const ControlGroupValidation = forwardRef<HTMLSpanElement, ControlGroupValidationProps>( | ||
({className, variant, children, ...props}, ref) => { | ||
const {id} = useControlGroup() | ||
|
||
return ( | ||
<span | ||
ref={ref} | ||
id={`${id}-validation`} | ||
className={clsx( | ||
styles.ControlGroup__validation, | ||
styles['ControlGroup__validation--animate-in'], | ||
variant === 'success' && styles['ControlGroup__validation--success'], | ||
variant === 'error' && styles['ControlGroup__validation--error'], | ||
className, | ||
)} | ||
{...props} | ||
> | ||
{variant === 'success' && ( | ||
<span className={styles['ControlGroup__validation-icon--success']}> | ||
<CheckCircleFillIcon /> | ||
</span> | ||
)} | ||
{variant === 'error' && ( | ||
<span className={styles['ControlGroup__validation-icon--error']}> | ||
<AlertFillIcon /> | ||
</span> | ||
)} | ||
|
||
{children} | ||
</span> | ||
) | ||
}, | ||
) | ||
|
||
export const ControlGroup = Object.assign(_ControlGroup, { | ||
Label: ControlGroupLabel, | ||
Caption: ControlGroupCaption, | ||
Validation: ControlGroupValidation, | ||
}) |
14 changes: 14 additions & 0 deletions
14
packages/react/src/forms/ControlGroup/InputGroup.module.css.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
declare const styles: { | ||
readonly "ControlGroup__container": string; | ||
readonly "ControlGroup__label": string; | ||
readonly "ControlGroup__caption": string; | ||
readonly "ControlGroup__validation": string; | ||
readonly "ControlGroup__validation--success": string; | ||
readonly "ControlGroup__validation--error": string; | ||
readonly "ControlGroup__validation-icon--success": string; | ||
readonly "ControlGroup__validation-icon--error": string; | ||
readonly "ControlGroup__validation--animate-in": string; | ||
readonly "ControlGroupValidationFadeIn": string; | ||
}; | ||
export = styles; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './ControlGroup' |
14 changes: 0 additions & 14 deletions
14
packages/react/src/forms/InputGroup/InputGroup.module.css.d.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { | ||
InputGroup, | ||
type InputGroupCaptionProps, | ||
type InputGroupLabelProps, | ||
type InputGroupProps, | ||
type InputGroupValidationProps, | ||
} from '../InputGroup' | ||
ControlGroup, | ||
type ControlGroupCaptionProps, | ||
type ControlGroupLabelProps, | ||
type ControlGroupProps, | ||
type ControlGroupValidationProps, | ||
} from '../ControlGroup' | ||
|
||
export type RadioGroupProps = InputGroupProps | ||
export type RadioGroupLabelProps = InputGroupLabelProps | ||
export type RadioGroupCaptionProps = InputGroupCaptionProps | ||
export type RadioGroupValidationProps = InputGroupValidationProps | ||
export type RadioGroupProps = ControlGroupProps | ||
export type RadioGroupLabelProps = ControlGroupLabelProps | ||
export type RadioGroupCaptionProps = ControlGroupCaptionProps | ||
export type RadioGroupValidationProps = ControlGroupValidationProps | ||
|
||
export const RadioGroup = InputGroup | ||
export const RadioGroup = ControlGroup |