UNPKG

9.59 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var toObjectMap = function () {
4 var items = [];
5 for (var _i = 0; _i < arguments.length; _i++) {
6 items[_i] = arguments[_i];
7 }
8 var result = {};
9 for (var _a = 0, items_1 = items; _a < items_1.length; _a++) {
10 var item = items_1[_a];
11 var keys = Array.isArray(item) ? item : Object.keys(item);
12 for (var _b = 0, keys_1 = keys; _b < keys_1.length; _b++) {
13 var key = keys_1[_b];
14 result[key] = 1;
15 }
16 }
17 return result;
18};
19/**
20 * An array of events that are allowed on every html element type.
21 *
22 * @public
23 */
24exports.baseElementEvents = toObjectMap([
25 'onCopy',
26 'onCut',
27 'onPaste',
28 'onCompositionEnd',
29 'onCompositionStart',
30 'onCompositionUpdate',
31 'onFocus',
32 'onFocusCapture',
33 'onBlur',
34 'onBlurCapture',
35 'onChange',
36 'onInput',
37 'onSubmit',
38 'onLoad',
39 'onError',
40 'onKeyDown',
41 'onKeyDownCapture',
42 'onKeyPress',
43 'onKeyUp',
44 'onAbort',
45 'onCanPlay',
46 'onCanPlayThrough',
47 'onDurationChange',
48 'onEmptied',
49 'onEncrypted',
50 'onEnded',
51 'onLoadedData',
52 'onLoadedMetadata',
53 'onLoadStart',
54 'onPause',
55 'onPlay',
56 'onPlaying',
57 'onProgress',
58 'onRateChange',
59 'onSeeked',
60 'onSeeking',
61 'onStalled',
62 'onSuspend',
63 'onTimeUpdate',
64 'onVolumeChange',
65 'onWaiting',
66 'onClick',
67 'onClickCapture',
68 'onContextMenu',
69 'onDoubleClick',
70 'onDrag',
71 'onDragEnd',
72 'onDragEnter',
73 'onDragExit',
74 'onDragLeave',
75 'onDragOver',
76 'onDragStart',
77 'onDrop',
78 'onMouseDown',
79 'onMouseDownCapture',
80 'onMouseEnter',
81 'onMouseLeave',
82 'onMouseMove',
83 'onMouseOut',
84 'onMouseOver',
85 'onMouseUp',
86 'onMouseUpCapture',
87 'onSelect',
88 'onTouchCancel',
89 'onTouchEnd',
90 'onTouchMove',
91 'onTouchStart',
92 'onScroll',
93 'onWheel',
94 'onPointerCancel',
95 'onPointerDown',
96 'onPointerEnter',
97 'onPointerLeave',
98 'onPointerMove',
99 'onPointerOut',
100 'onPointerOver',
101 'onPointerUp',
102 'onGotPointerCapture',
103 'onLostPointerCapture',
104]);
105/**
106 * An array of element attributes which are allowed on every html element type.
107 *
108 * @public
109 */
110exports.baseElementProperties = toObjectMap([
111 'accessKey',
112 'children',
113 'className',
114 'contentEditable',
115 'dir',
116 'draggable',
117 'hidden',
118 'htmlFor',
119 'id',
120 'lang',
121 'ref',
122 'role',
123 'style',
124 'tabIndex',
125 'title',
126 'translate',
127 'spellCheck',
128 'name',
129]);
130/**
131 * An array of HTML element properties and events.
132 *
133 * @public
134 */
135exports.htmlElementProperties = toObjectMap(exports.baseElementProperties, exports.baseElementEvents);
136/**
137 * An array of LABEL tag properties and events.
138 *
139 * @public
140 */
141exports.labelProperties = toObjectMap(exports.htmlElementProperties, [
142 'form',
143]);
144/**
145 * An array of AUDIO tag properties and events.
146
147 * @public
148 */
149exports.audioProperties = toObjectMap(exports.htmlElementProperties, [
150 'height',
151 'loop',
152 'muted',
153 'preload',
154 'src',
155 'width',
156]);
157/**
158 * An array of VIDEO tag properties and events.
159 *
160 * @public
161 */
162exports.videoProperties = toObjectMap(exports.audioProperties, [
163 'poster',
164]);
165/**
166 * An array of OL tag properties and events.
167 *
168 * @public
169 */
170exports.olProperties = toObjectMap(exports.htmlElementProperties, [
171 'start',
172]);
173/**
174 * An array of LI tag properties and events.
175 *
176 * @public
177 */
178exports.liProperties = toObjectMap(exports.htmlElementProperties, [
179 'value',
180]);
181/**
182 * An array of A tag properties and events.
183 *
184 * @public
185 */
186exports.anchorProperties = toObjectMap(exports.htmlElementProperties, [
187 'download',
188 'href',
189 'hrefLang',
190 'media',
191 'rel',
192 'target',
193 'type',
194]);
195/**
196 * An array of BUTTON tag properties and events.
197 *
198 * @public
199 */
200exports.buttonProperties = toObjectMap(exports.htmlElementProperties, [
201 'autoFocus',
202 'disabled',
203 'form',
204 'formAction',
205 'formEncType',
206 'formMethod',
207 'formNoValidate',
208 'formTarget',
209 'type',
210 'value',
211]);
212/**
213 * An array of INPUT tag properties and events.
214 *
215 * @public
216 */
217exports.inputProperties = toObjectMap(exports.buttonProperties, [
218 'accept',
219 'alt',
220 'autoCapitalize',
221 'autoComplete',
222 'checked',
223 'dirname',
224 'form',
225 'height',
226 'inputMode',
227 'list',
228 'max',
229 'maxLength',
230 'min',
231 'multiple',
232 'pattern',
233 'placeholder',
234 'readOnly',
235 'required',
236 'src',
237 'step',
238 'size',
239 'type',
240 'value',
241 'width',
242]);
243/**
244 * An array of TEXTAREA tag properties and events.
245 *
246 * @public
247 */
248exports.textAreaProperties = toObjectMap(exports.buttonProperties, [
249 'autoCapitalize',
250 'cols',
251 'dirname',
252 'form',
253 'maxLength',
254 'placeholder',
255 'readOnly',
256 'required',
257 'rows',
258 'wrap',
259]);
260/**
261 * An array of SELECT tag properties and events.
262 *
263 * @public
264 */
265exports.selectProperties = toObjectMap(exports.buttonProperties, [
266 'form',
267 'multiple',
268 'required',
269]);
270exports.optionProperties = toObjectMap(exports.htmlElementProperties, [
271 'selected',
272 'value',
273]);
274/**
275 * An array of TABLE tag properties and events.
276 *
277 * @public
278 */
279exports.tableProperties = toObjectMap(exports.htmlElementProperties, [
280 'cellPadding',
281 'cellSpacing',
282]);
283/**
284 * An array of TR tag properties and events.
285 *
286 * @public
287 */
288exports.trProperties = exports.htmlElementProperties;
289/**
290 * An array of TH tag properties and events.
291 *
292 * @public
293 */
294exports.thProperties = toObjectMap(exports.htmlElementProperties, [
295 'rowSpan',
296 'scope',
297]);
298/**
299 * An array of TD tag properties and events.
300 *
301 * @public
302 */
303exports.tdProperties = toObjectMap(exports.htmlElementProperties, [
304 'colSpan',
305 'headers',
306 'rowSpan',
307 'scope',
308]);
309exports.colGroupProperties = toObjectMap(exports.htmlElementProperties, [
310 'span',
311]);
312exports.colProperties = toObjectMap(exports.htmlElementProperties, [
313 'span',
314]);
315/**
316 * An array of FORM tag properties and events.
317 *
318 * @public
319 */
320exports.formProperties = toObjectMap(exports.htmlElementProperties, [
321 'acceptCharset',
322 'action',
323 'encType',
324 'encType',
325 'method',
326 'noValidate',
327 'target',
328]);
329/**
330 * An array of IFRAME tag properties and events.
331 *
332 * @public
333 */
334exports.iframeProperties = toObjectMap(exports.htmlElementProperties, [
335 'allow',
336 'allowFullScreen',
337 'allowPaymentRequest',
338 'allowTransparency',
339 'csp',
340 'height',
341 'importance',
342 'referrerPolicy',
343 'sandbox',
344 'src',
345 'srcDoc',
346 'width',
347]);
348/**
349 * An array of IMAGE tag properties and events.
350 *
351 * @public
352 */
353exports.imgProperties = toObjectMap(exports.htmlElementProperties, [
354 'alt',
355 'crossOrigin',
356 'height',
357 'src',
358 'srcSet',
359 'useMap',
360 'width',
361]);
362/**
363 * @deprecated Use imgProperties for img elements.
364 */
365exports.imageProperties = exports.imgProperties;
366/**
367 * An array of DIV tag properties and events.
368 *
369 * @public
370 */
371exports.divProperties = exports.htmlElementProperties;
372/**
373 * Gets native supported props for an html element provided the allowance set. Use one of the property
374 * sets defined (divProperties, buttonPropertes, etc) to filter out supported properties from a given
375 * props set. Note that all data- and aria- prefixed attributes will be allowed.
376 * NOTE: getNativeProps should always be applied first when adding props to a react component. The
377 * non-native props should be applied second. This will prevent getNativeProps from overriding your custom props.
378 * For example, if props passed to getNativeProps has an onClick function and getNativeProps is added to
379 * the component after an onClick function is added, then the getNativeProps onClick will override it.
380 *
381 * @public
382 * @param props - The unfiltered input props
383 * @param allowedPropsNames - The array or record of allowed prop names.
384 * @returns The filtered props
385 */
386// eslint-disable-next-line @typescript-eslint/no-explicit-any
387function getNativeProps(
388// eslint-disable-next-line @typescript-eslint/no-explicit-any
389props, allowedPropNames, excludedPropNames) {
390 // It'd be great to properly type this while allowing 'aria-` and 'data-' attributes like TypeScript does for
391 // JSX attributes, but that ability is hardcoded into the TS compiler with no analog in TypeScript typings.
392 // Then we'd be able to enforce props extends native props (including aria- and data- attributes), and then
393 // return native props.
394 // We should be able to do this once this PR is merged: https://github.com/microsoft/TypeScript/pull/26797
395 var _a;
396 var isArray = Array.isArray(allowedPropNames);
397 // eslint-disable-next-line @typescript-eslint/no-explicit-any
398 var result = {};
399 var keys = Object.keys(props);
400 for (var _i = 0, keys_2 = keys; _i < keys_2.length; _i++) {
401 var key = keys_2[_i];
402 var isNativeProp = (!isArray && allowedPropNames[key]) ||
403 (isArray && allowedPropNames.indexOf(key) >= 0) ||
404 key.indexOf('data-') === 0 ||
405 key.indexOf('aria-') === 0;
406 if (isNativeProp && (!excludedPropNames || ((_a = excludedPropNames) === null || _a === void 0 ? void 0 : _a.indexOf(key)) === -1)) {
407 // eslint-disable-next-line @typescript-eslint/no-explicit-any
408 result[key] = props[key];
409 }
410 }
411 return result;
412}
413exports.getNativeProps = getNativeProps;
414//# sourceMappingURL=properties.js.map
\No newline at end of file