Skip to content

Commit

Permalink
makes InputField validation props smarter
Browse files Browse the repository at this point in the history
  • Loading branch information
mperrotti committed Nov 22, 2021
1 parent 6054f1c commit 910ee68
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 55 deletions.
27 changes: 15 additions & 12 deletions src/_InputField/InputField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import InputValidation from '../InputValidation'
import {ComponentProps} from '../utils/types'
import {uniqueId} from '../utils/uniqueId'
import InputFieldCaption from './InputFieldCaption'
import InputFieldInput from './InputFieldInput'
import InputFieldLabel from './InputFieldLabel'
import InputFieldValidation from './InputFieldValidation'
import {Slots} from './slots'
import ValidationAnimationContainer from './ValidationAnimationContainer'
export interface Props {
// TODO: limit children to specific components
// children: any;
export interface Props<T = Record<string, 'error' | 'warning' | 'success'>> {
children?: React.ReactNode
/**
* Whether the field is ready for user input
*/
Expand All @@ -28,22 +26,29 @@ export interface Props {
* A map of validation statuses and their associated validation keys. When one of the validation keys is passed to the `validationResult` prop,
* the associated validation message will be rendered in the correct style
*/
validationMap?: Record<string, 'error' | 'warning' | 'success'>
validationMap?: T
/**
* The key of the validation message to show
*/
// TODO: figure out how to type this as a string union of `validationMap` values
// something like `keyof Props['validationMap']`
validationResult?: string
validationResult?: keyof T
}

type InputFieldValidationProps = ComponentProps<typeof InputFieldValidation>
export interface InputFieldContext extends Pick<Props, 'disabled' | 'id' | 'required'> {
export interface InputFieldContext
extends Pick<Props<Record<string, 'error' | 'warning' | 'success'>>, 'disabled' | 'id' | 'required'> {
captionId: string
validationMessageId: string
}

const InputField: React.FC<Props> = ({children, disabled, id: idProp, required, validationMap, validationResult}) => {
// adding `extends unknown` beacuse `<T>` is interpretted as a JSX tag
const InputField = <T extends Record<string, 'error' | 'warning' | 'success'>>({
children,
disabled,
id: idProp,
required,
validationMap,
validationResult
}: Props<T>) => {
const id = idProp || uniqueId()
const validationChildren: React.ReactElement<InputFieldValidationProps>[] | undefined | null = React.Children.map(
children,
Expand Down Expand Up @@ -85,10 +90,8 @@ const InputField: React.FC<Props> = ({children, disabled, id: idProp, required,
}

export type InputFieldComponentProps = ComponentProps<typeof InputField>
export type {Props as InputFieldInputProps} from './InputFieldInput'
export default Object.assign(InputField, {
Caption: InputFieldCaption,
Input: InputFieldInput,
Label: InputFieldLabel,
Validation: InputFieldValidation
})
31 changes: 0 additions & 31 deletions src/_InputField/InputFieldInput.tsx

This file was deleted.

14 changes: 2 additions & 12 deletions src/_InputField/ToggleInputField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,9 @@ import {get} from '../constants'
import {uniqueId} from '../utils/uniqueId'
import {Slots} from './slots'
import ToggleInputLeadingVisual from './ToggleInputLeadingVisual'
import {Props as InputFieldProps} from './InputField'

//TODO: DRY out - some of this is repeated in the `InputField` Props interface
export interface Props {
// TODO: limit children to specific components
// children: any;
/**
* Whether the field is ready for user input
*/
disabled?: boolean
/**
* The unique identifier for this field. Used to associate the label, validation text, and caption text
*/
id?: string
export interface Props extends Pick<InputFieldProps, 'disabled' | 'id'> {
/**
* Styles the field to visually communicate the result of form validation
*/
Expand Down

0 comments on commit 910ee68

Please sign in to comment.