Skip to content

Commit

Permalink
feat(core/presentation): Always call onBlur in Checklist to "mark as …
Browse files Browse the repository at this point in the history
…touched" (#7134)

Also: mark `stringOptions` as `readonly` to allow read only arrays of options to be provided.
  • Loading branch information
christopherthielen committed Jun 20, 2019
1 parent cdd6f23 commit 0a3bd68
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { IFormInputProps, OmitControlledInputPropsFrom } from '../interface';

import { createFakeReactSyntheticEvent, isStringArray, orEmptyString, validationClassName } from './utils';

const { useEffect } = React;

interface IChecklistInputProps extends IFormInputProps, OmitControlledInputPropsFrom<React.InputHTMLAttributes<any>> {
options?: IChecklistInputOption[];
stringOptions?: string[];
stringOptions?: readonly string[];
inline?: boolean;
showSelectAll?: boolean;
}
Expand All @@ -28,6 +30,13 @@ export function ChecklistInput(props: IChecklistInputProps) {
...otherProps
} = props;

// Naively call the the field's onBlur handler
// This is what Formik uses to mark the field as touched
function touchField() {
props.onBlur && props.onBlur(createFakeReactSyntheticEvent({ name: props.name, value }));
}
useEffect(touchField, []);

const className = `${orEmptyString(inputClassName)} ${validationClassName(validation)}`;

const selectedValues = value || [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ export const createFakeReactSyntheticEvent = (target: { name?: string; value?: a
target,
} as React.ChangeEvent<any>);

export const isStringArray = (opts: any[]): opts is string[] => opts && opts.length && opts.every(isString);
export const isStringArray = (opts: readonly any[]): opts is string[] => opts && opts.length && opts.every(isString);

0 comments on commit 0a3bd68

Please sign in to comment.