UNPKG

3.56 kBTypeScriptView Raw
1import * as React from 'react';
2import * as PropTypes from 'prop-types';
3// @ts-ignore
4import ConditionalWrap from 'conditional-wrap';
5
6import { Omit } from '../types';
7import Group from '../Group';
8import Input, { LocalInputProps, InputProps, inputDefaultProps, inputPropTypes } from './Input';
9import FieldWrapper, {
10 LocalFieldWrapperProps,
11 fieldWrapperDefaultProps,
12 fieldWrapperPropTypes
13} from '../FieldWrapper/FieldWrapper';
14
15export type LocalInputFieldProps = Omit<LocalFieldWrapperProps, 'children'> &
16 LocalInputProps & {
17 /** Addon component to the input (before). Similar to the addon components in Input. */
18 addonBefore?: React.ReactElement<any>;
19 /** Addon component to the input (after). Similar to the addon components in Input. */
20 addonAfter?: React.ReactElement<any>;
21 /** If addonBefore or addonAfter exists, then the addons will render vertically. */
22 isVertical?: boolean;
23 };
24export type InputFieldProps = LocalInputFieldProps & InputProps;
25
26export const InputField: React.FunctionComponent<LocalInputFieldProps> = ({
27 addonBefore,
28 addonAfter,
29 a11yId,
30 description,
31 hint,
32 isOptional,
33 isVertical,
34 label,
35 validationText,
36 after,
37 a11yLabel,
38 autoComplete,
39 autoFocus,
40 before,
41 children,
42 className,
43 defaultValue,
44 disabled,
45 inputRef,
46 isLoading,
47 isRequired,
48 name,
49 size,
50 max,
51 maxLength,
52 min,
53 minLength,
54 multiple,
55 pattern,
56 placeholder,
57 readOnly,
58 spellCheck,
59 step,
60 state,
61 type,
62 value,
63 onBlur,
64 onChange,
65 onFocus,
66 ...props
67}) => (
68 <FieldWrapper
69 a11yId={a11yId}
70 description={description}
71 hint={hint}
72 isOptional={isOptional}
73 isRequired={isRequired}
74 label={label}
75 state={state}
76 validationText={validationText}
77 {...props}
78 >
79 {({ elementProps }) => (
80 <ConditionalWrap
81 condition={addonBefore || addonAfter}
82 wrap={(children: React.ReactNode) => <Group isVertical={isVertical}>{children}</Group>}
83 >
84 {addonBefore}
85 <Input
86 after={after}
87 a11yId={a11yId}
88 a11yLabel={a11yLabel}
89 autoComplete={autoComplete}
90 autoFocus={autoFocus}
91 before={before}
92 className={className}
93 defaultValue={defaultValue}
94 disabled={disabled}
95 inputRef={inputRef}
96 isLoading={isLoading}
97 isRequired={isRequired}
98 name={name}
99 size={size}
100 max={max}
101 maxLength={maxLength}
102 min={min}
103 minLength={minLength}
104 multiple={multiple}
105 pattern={pattern}
106 placeholder={placeholder}
107 readOnly={readOnly}
108 spellCheck={spellCheck}
109 step={step}
110 state={state}
111 type={type}
112 value={value}
113 onBlur={onBlur}
114 onChange={onChange}
115 onFocus={onFocus}
116 {...elementProps}
117 >
118 {children}
119 </Input>
120 {addonAfter}
121 </ConditionalWrap>
122 )}
123 </FieldWrapper>
124);
125
126export const inputFieldPropTypes = {
127 addonBefore: PropTypes.element,
128 addonAfter: PropTypes.element,
129 isVertical: PropTypes.bool,
130 ...fieldWrapperPropTypes,
131 ...inputPropTypes
132};
133InputField.propTypes = inputFieldPropTypes;
134
135export const inputFieldDefaultProps = {
136 addonBefore: undefined,
137 addonAfter: undefined,
138 isVertical: false,
139 ...fieldWrapperDefaultProps,
140 ...inputDefaultProps
141};
142InputField.defaultProps = inputFieldDefaultProps;
143
144// @ts-ignore
145const C: React.FunctionComponent<InputFieldProps> = InputField;
146export default C;