/
FormFile.tsx
236 lines (211 loc) · 6.03 KB
/
FormFile.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import classNames from 'classnames';
import PropTypes from 'prop-types';
import React, { useContext, useMemo } from 'react';
import all from 'prop-types-extra/lib/all';
import Feedback from './Feedback';
import FormFileInput from './FormFileInput';
import FormFileLabel from './FormFileLabel';
import FormContext from './FormContext';
import { useBootstrapPrefix } from './ThemeProvider';
import {
BsCustomPrefixProps,
BsPrefixPropsWithChildren,
BsPrefixRefForwardingComponent,
} from './helpers';
export interface FormFileProps
extends BsPrefixPropsWithChildren,
BsCustomPrefixProps,
Pick<React.HTMLAttributes<HTMLElement>, 'style'> {
inputAs?: React.ElementType;
id?: string;
disabled?: boolean;
label?: React.ReactNode;
custom?: boolean;
isValid?: boolean;
isInvalid?: boolean;
feedback?: React.ReactNode;
feedbackTooltip?: boolean;
lang?: string;
'data-browse'?: any; // ???
}
type FormFile = BsPrefixRefForwardingComponent<'input', FormFileProps> & {
Input: typeof FormFileInput;
Label: typeof FormFileLabel;
};
const propTypes = {
/**
* @default 'form-file'
*/
bsPrefix: PropTypes.string,
/**
* A seperate bsPrefix used for custom controls
*
* @default 'custom-file'
*/
bsCustomPrefix: PropTypes.string,
/**
* The wrapping HTML element to use when rendering the FormFile.
*
* @type {('div'|elementType)}
*/
as: PropTypes.elementType,
/**
* The underlying HTML element to use when rendering the FormFile.
*
* @type {('input'|elementType)}
*/
inputAs: PropTypes.elementType,
/** A HTML id attribute, necessary for proper form accessibility. */
id: PropTypes.string,
/**
* Provide a function child to manually handle the layout of the FormFile's inner components.
*
* If not using the custom prop <code>FormFile.Label></code> should be before <code><FormFile.Input isInvalid /></code>
* ```jsx
* <FormFile>
* <FormFile.Label>Allow us to contact you?</FormFile.Label>
* <FormFile.Input isInvalid />
* <Feedback type="invalid">Yo this is required</Feedback>
* </FormFile>
* ```
*
* If using the custom prop <code><FormFile.Input isInvalid /></code> should be before <code>FormFile.Label></code>
* ```jsx
* <FormFile custom>
* <FormFile.Input isInvalid />
* <FormFile.Label>Allow us to contact you?</FormFile.Label>
* <Feedback type="invalid">Yo this is required</Feedback>
* </FormFile>
* ```
*/
children: PropTypes.node,
disabled: PropTypes.bool,
label: PropTypes.node,
/** Use Bootstrap's custom form elements to replace the browser defaults */
custom: PropTypes.bool,
/** Manually style the input as valid */
isValid: PropTypes.bool,
/** Manually style the input as invalid */
isInvalid: PropTypes.bool,
/** Display feedback as a tooltip. */
feedbackTooltip: PropTypes.bool,
/** A message to display when the input is in a validation state */
feedback: PropTypes.node,
/**
* The string for the "Browse" text label when using custom file input
*
* @type string
*/
'data-browse': all(
PropTypes.string,
({ custom, 'data-browse': dataBrowse }) =>
dataBrowse && !custom
? Error(
'`data-browse` attribute value will only be used when custom is `true`',
)
: null,
),
/** The language for the button when using custom file input and SCSS based strings */
lang: all(PropTypes.string, ({ custom, lang }) =>
lang && !custom
? Error('`lang` can only be set when custom is `true`')
: null,
),
};
const FormFile: FormFile = (React.forwardRef(
(
{
id,
bsPrefix,
bsCustomPrefix,
disabled = false,
isValid = false,
isInvalid = false,
feedbackTooltip = false,
feedback,
className,
style,
label,
children,
custom,
lang,
'data-browse': dataBrowse,
// Need to define the default "as" during prop destructuring to be compatible with styled-components github.com/react-bootstrap/react-bootstrap/issues/3595
as: Component = 'div',
inputAs = 'input',
...props
}: FormFileProps,
ref,
) => {
const [prefix, defaultPrefix] = custom
? [bsCustomPrefix, 'custom']
: [bsPrefix, 'form-file'];
bsPrefix = useBootstrapPrefix(prefix, defaultPrefix);
const type = 'file';
const { controlId } = useContext(FormContext);
const innerFormContext = useMemo(
() => ({
controlId: id || controlId,
custom,
}),
[controlId, custom, id],
);
const hasLabel = label != null && label !== false && !children;
const input = (
<FormFileInput
{...props}
ref={ref}
isValid={isValid}
isInvalid={isInvalid}
disabled={disabled}
as={inputAs}
lang={lang}
/>
);
return (
<FormContext.Provider value={innerFormContext}>
<Component
style={style}
className={classNames(
className,
bsPrefix,
custom && `custom-${type}`,
)}
>
{children || (
<>
{custom ? (
<>
{input}
{hasLabel && (
<FormFileLabel data-browse={dataBrowse}>
{label}
</FormFileLabel>
)}
</>
) : (
<>
{hasLabel && <FormFileLabel>{label}</FormFileLabel>}
{input}
</>
)}
{(isValid || isInvalid) && (
<Feedback
type={isValid ? 'valid' : 'invalid'}
tooltip={feedbackTooltip}
>
{feedback}
</Feedback>
)}
</>
)}
</Component>
</FormContext.Provider>
);
},
) as unknown) as FormFile;
FormFile.displayName = 'FormFile';
FormFile.propTypes = propTypes;
FormFile.Input = FormFileInput;
FormFile.Label = FormFileLabel;
export default FormFile;