Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ErrorProp]: Implements an error prop (fixes #693) #852

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Checkboxes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface CheckboxesProps extends Partial<Omit<MuiCheckboxProps, 'onChang
formControlLabelProps?: Partial<FormControlLabelProps>;
formHelperTextProps?: Partial<FormHelperTextProps>;
showError?: ShowErrorFunc;
error?: boolean;
}

export function Checkboxes(props: CheckboxesProps) {
Expand All @@ -52,13 +53,14 @@ export function Checkboxes(props: CheckboxesProps) {
formControlLabelProps,
formHelperTextProps,
showError = showErrorOnChange,
error,
...restCheckboxes
} = props;

const itemsData = Array.isArray(data) ? data : [data];
const single = !Array.isArray(data);
const field = useFieldForErrors(name);
const isError = showError(field);
const isError = error == null ? showError(field) : error;

return (
<FormControl required={required} error={isError} {...formControlProps}>
Expand Down
9 changes: 6 additions & 3 deletions src/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface DatePickerProps extends Partial<Omit<MuiDatePickerProps<any, an
fieldProps?: Partial<FieldProps<any, any>>;
required?: boolean;
showError?: ShowErrorFunc;
error?: boolean;
}

export function DatePicker(props: DatePickerProps) {
Expand All @@ -37,13 +38,15 @@ function DatePickerWrapper(props: DatePickerWrapperProps) {
meta,
showError = showErrorOnChange,
required,
error,
...rest
} = props;

const { error, submitError } = meta;
const isError = showError({ meta });
const { error: metaError, submitError } = meta;
const isError = error == null ? showError({ meta }) : error;

const { helperText, ...lessrest } = rest;
const takeText = error ? helperText || metaError || submitError : isError ? metaError || submitError : helperText;

return (
<MuiDatePicker
Expand All @@ -54,7 +57,7 @@ function DatePickerWrapper(props: DatePickerWrapperProps) {
<TextField
{...inputProps}
fullWidth={true}
helperText={isError ? error || submitError : helperText}
helperText={takeText}
error={inputProps.error || isError}
name={name}
required={required}
Expand Down
10 changes: 6 additions & 4 deletions src/DateTimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface DateTimePickerProps extends Partial<Omit<MuiDateTimePickerProps
fieldProps?: Partial<FieldProps<any, any>>;
required?: boolean;
showError?: ShowErrorFunc;
error?: boolean;
}

export function DateTimePicker(props: DateTimePickerProps) {
Expand All @@ -41,13 +42,14 @@ function DateTimePickerWrapper(props: DateTimePickerWrapperProps) {
meta,
showError = showErrorOnChange,
required,
error,
...rest
} = props;

const { error, submitError } = meta;
const isError = showError({ meta });

const { error: metaError, submitError } = meta;
const isError = error == null ? showError({ meta }) : error;
const { helperText, ...lessrest } = rest;
const takeText = error ? helperText || metaError || submitError : isError ? metaError || submitError : helperText;

return (
<MuiDateTimePicker
Expand All @@ -58,7 +60,7 @@ function DateTimePickerWrapper(props: DateTimePickerWrapperProps) {
<TextField
{...inputProps}
fullWidth={true}
helperText={isError ? error || submitError : helperText}
helperText={takeText}
error={inputProps.error || isError}
name={name}
required={required}
Expand Down
4 changes: 3 additions & 1 deletion src/Radios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface RadiosProps extends Partial<Omit<MuiRadioProps, 'onChange'>> {
radioGroupProps?: Partial<RadioGroupProps>;
formHelperTextProps?: Partial<FormHelperTextProps>;
showError?: ShowErrorFunc;
error?: boolean;
}

export function Radios(props: RadiosProps) {
Expand All @@ -51,12 +52,13 @@ export function Radios(props: RadiosProps) {
formControlProps,
radioGroupProps,
formHelperTextProps,
error,
showError = showErrorOnChange,
...restRadios
} = props;

const field = useFieldForErrors(name);
const isError = showError(field);
const isError = error == null ? showError(field) : error;

return (
<FormControl required={required} error={isError} {...formControlProps}>
Expand Down
4 changes: 3 additions & 1 deletion src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface SelectProps extends Partial<Omit<MuiSelectProps, 'onChange'>> {
inputLabelProps?: Partial<InputLabelProps>;
formHelperTextProps?: Partial<FormHelperTextProps>;
showError?: ShowErrorFunc;
error?: boolean;
menuItemProps?: Partial<MenuItemProps>;
data?: SelectData[];
children?: ReactNode;
Expand All @@ -52,6 +53,7 @@ export function Select(props: SelectProps) {
formHelperTextProps,
menuItemProps,
showError = showErrorOnChange,
error,
...restSelectProps
} = props;

Expand All @@ -61,7 +63,7 @@ export function Select(props: SelectProps) {

const { variant } = restSelectProps;
const field = useFieldForErrors(name);
const isError = showError(field);
const isError = error == null ? showError(field) : error;

return (
<Field
Expand Down
4 changes: 3 additions & 1 deletion src/Switches.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface SwitchesProps extends Partial<Omit<MuiSwitchProps, 'onChange'>>
formControlLabelProps?: Partial<FormControlLabelProps>;
formHelperTextProps?: Partial<FormHelperTextProps>;
showError?: ShowErrorFunc;
error?: boolean;
}

export function Switches(props: SwitchesProps) {
Expand All @@ -52,13 +53,14 @@ export function Switches(props: SwitchesProps) {
formControlLabelProps,
formHelperTextProps,
showError = showErrorOnChange,
error,
...restSwitches
} = props;

const itemsData = Array.isArray(data) ? data : [data];
const single = !Array.isArray(data);
const field = useFieldForErrors(name);
const isError = showError(field);
const isError = error == null ? showError(field) : error;

return (
<FormControl required={required} error={isError} {...formControlProps}>
Expand Down
9 changes: 6 additions & 3 deletions src/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type TextFieldProps = Partial<Omit<MuiTextFieldProps, 'type' | 'onChange'
type?: TEXT_FIELD_TYPE;
fieldProps?: Partial<FieldProps<any, any>>;
showError?: ShowErrorFunc;
error?: boolean;
};

export function TextField(props: TextFieldProps) {
Expand All @@ -64,17 +65,19 @@ export function TextFieldWrapper(props: TextWrapperProps) {
required,
fullWidth = true,
helperText,
error,
showError = showErrorOnChange,
...rest
} = props;

const { error, submitError } = meta;
const isError = showError({ meta });
const { error: metaError, submitError } = meta;
const isError = error == null ? showError({ meta }) : error;
const takeText = error ? helperText || metaError || submitError : isError ? metaError || submitError : helperText;

return (
<MuiTextField
fullWidth={fullWidth}
helperText={isError ? error || submitError : helperText}
helperText={takeText}
error={isError}
onChange={onChange}
onBlur={onBlur}
Expand Down
10 changes: 6 additions & 4 deletions src/TimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface TimePickerProps extends Partial<Omit<MuiTimePickerProps<any, an
fieldProps?: Partial<FieldProps<any, any>>;
required?: boolean;
showError?: ShowErrorFunc;
error?: boolean;
}

export function TimePicker(props: TimePickerProps) {
Expand All @@ -36,14 +37,15 @@ function TimePickerWrapper(props: TimePickerWrapperProps) {
input: { name, onChange, value, ...restInput },
meta,
showError = showErrorOnChange,
error,
required,
...rest
} = props;

const { error, submitError } = meta;
const isError = showError({ meta });

const { error: metaError, submitError } = meta;
const isError = error == null ? showError({ meta }) : error;
const { helperText, ...lessrest } = rest;
const takeText = error ? helperText || metaError || submitError : isError ? metaError || submitError : helperText;

return (
<MuiTimePicker
Expand All @@ -54,7 +56,7 @@ function TimePickerWrapper(props: TimePickerWrapperProps) {
<TextField
{...inputProps}
fullWidth={true}
helperText={isError ? error || submitError : helperText}
helperText={takeText}
error={inputProps.error || isError}
name={name}
required={required}
Expand Down