Skip to content

Commit 632be77

Browse files
committed
Merge branch 'development'
2 parents eb70db3 + 6bfd74b commit 632be77

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

example/index.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,16 @@ const App = () => {
4444
>
4545
<Form size="large">
4646
<Input
47+
inputLabel={{ color: 'orange', content: 'email' }}
4748
name="email"
48-
icon="at"
49-
iconPosition="left"
5049
placeholder="Email"
5150
focus
5251
fluid
5352
errorPrompt
5453
/>
5554
<Input
55+
inputLabel={{ color: 'violet', content: 'password' }}
5656
name="password"
57-
icon="key"
58-
iconPosition="left"
5957
type="password"
6058
placeholder="Password"
6159
autoComplete="on"

src/Input.tsx

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import React, { forwardRef, Ref } from 'react';
2-
import { FieldProps as FormikFieldProps } from 'formik';
2+
import { FieldMetaProps, FieldProps as FormikFieldProps } from 'formik';
33
import {
44
FormField,
55
Input as _Input,
66
InputOnChangeData,
77
InputProps as _InputProps,
8+
Label,
89
} from 'semantic-ui-react';
910
import { FieldErrorProps, FieldProps } from './types';
10-
import { getErrorConfig } from './utils';
1111
import Field from './Field';
1212

13-
export type InputProps = FieldProps & _InputProps & FieldErrorProps;
13+
export interface InputProps extends FieldProps, _InputProps, FieldErrorProps {
14+
inputLabel?: string;
15+
}
1416

1517
export const Input = (
1618
{
@@ -20,15 +22,42 @@ export const Input = (
2022
onChange: _onChange,
2123
errorPrompt,
2224
errorConfig,
25+
label,
26+
inputLabel,
2327
...restProps
2428
}: InputProps,
2529
ref: Ref<_Input>,
2630
) => {
31+
const errorLabel = (error: string | undefined) => (
32+
<Label
33+
prompt={errorConfig?.prompt ?? true}
34+
basic={errorConfig?.basic}
35+
pointing={errorConfig?.pointing ?? true}
36+
color={errorConfig?.color}
37+
content={error}
38+
/>
39+
);
40+
41+
const errorLabelBefore = (meta: FieldMetaProps<_Input>) =>
42+
errorPrompt &&
43+
meta.touched &&
44+
meta.error &&
45+
(errorConfig?.pointing === 'below' || errorConfig?.pointing === 'right') &&
46+
errorLabel(meta.error);
47+
48+
const errorLabelAfter = (meta: FieldMetaProps<_Input>) =>
49+
errorPrompt &&
50+
meta.touched &&
51+
meta.error &&
52+
(errorConfig?.pointing === 'above' ||
53+
errorConfig?.pointing === 'left' ||
54+
!errorConfig?.pointing) &&
55+
errorLabel(meta.error);
56+
2757
return (
2858
<Field name={name} validate={validate} fast={fast}>
2959
{({ field: { value, onChange, onBlur }, meta }: FormikFieldProps) => (
3060
<FormField
31-
control={_Input}
3261
ref={ref}
3362
name={name}
3463
value={value}
@@ -40,9 +69,18 @@ export const Input = (
4069
_onChange && _onChange(event, data);
4170
}}
4271
onBlur={onBlur}
43-
error={getErrorConfig(meta, errorPrompt, errorConfig)}
44-
{...restProps}
45-
/>
72+
error={meta.touched && meta.error}
73+
>
74+
{label && <Label>{label}</Label>}
75+
{errorLabelBefore(meta)}
76+
<_Input
77+
name={name}
78+
label={inputLabel}
79+
{...restProps}
80+
defaultValue={value}
81+
/>
82+
{errorLabelAfter(meta)}
83+
</FormField>
4684
)}
4785
</Field>
4886
);

0 commit comments

Comments
 (0)