Skip to content

Commit

Permalink
Workaround to allow arbitrary data-* types with Flow
Browse files Browse the repository at this point in the history
Allow arbitrary data-* props at runtime. On web, the check is only
performed during development.

To workaround Flow's lack of support for typing arbitary data-* props, a
separate type is created just for data-* props which can be used to
maintain an app's list of supported data-* props.

Ref #71
Close #75
  • Loading branch information
nmn authored and necolas committed Apr 4, 2024
1 parent 8c8aba3 commit 43b8181
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### New features

* [Native] Add `del`, `ins`, `kbd`, `s`, `u` semantic elements ([#57](https://github.com/facebook/react-strict-dom/pull/57))
* [Native] Add `del`, `ins`, `kbd`, `s`, `u` semantic elements ([#57](https://github.com/facebook/react-strict-dom/pull/57)).

## 0.0.3 (Mar 10, 2024)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import type { StrictHTMLElement } from '../../types/StrictHTMLElement';

import * as React from 'react';
import * as stylex from '@stylexjs/stylex';
import { strictAttributeSet } from '../../shared/strictAttributes';
import { errorMsg } from '../../shared/errorMsg';
import { isPropAllowed } from '../../shared/isPropAllowed';

// $FlowFixMe[unclear-type]
function validateStrictProps(props: any) {
Object.keys(props).forEach((key) => {
const isValid = strictAttributeSet.has(key);
const isValid = isPropAllowed(key);
if (!isValid) {
errorMsg(`"${key}" is not a valid prop`);
delete props[key];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import { flattenStyle } from './flattenStyle';
import { errorMsg } from '../../shared/errorMsg';
import { extractStyleThemes } from './extractStyleThemes';
import { getLocaleDirection } from '../../shared/getLocaleDirection';
import { isPropAllowed } from '../../shared/isPropAllowed';
import { mergeRefs } from '../../shared/mergeRefs';
import { strictAttributeSet } from '../../shared/strictAttributes';
import { useHoverHandlers } from './useHoverHandlers';
import { useStrictDOMElement } from './useStrictDOMElement';
import { useStyleProps } from './useStyleProps';
Expand Down Expand Up @@ -124,7 +124,7 @@ function isString(str: mixed): boolean %checks {

function validateStrictProps(props: any) {
Object.keys(props).forEach((key) => {
const isValidProp = strictAttributeSet.has(key);
const isValidProp = isPropAllowed(key);
const isUnsupportedProp = unsupportedProps.has(key);
if (!isValidProp) {
errorMsg(`"${key}" is not a valid prop`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow strict
*/

export const strictAttributeSet: Set<string> = new Set([
const strictAttributeSet: Set<string> = new Set([
'alt', // img
'aria-activedescendant',
'aria-atomic',
Expand Down Expand Up @@ -62,7 +62,6 @@ export const strictAttributeSet: Set<string> = new Set([
'checked', // input
'children',
'crossOrigin', // img
'data-testid',
'decoding', // img
'defaultChecked', // input
'defaultValue', // input, textarea
Expand Down Expand Up @@ -142,8 +141,6 @@ export const strictAttributeSet: Set<string> = new Set([
'value', // input
'width', // img

'data-imgperflogname', // TEMPORARY
'data-visualcompletion', // TEMPORARY
'onMouseDown', // TEMPORARY
'onMouseEnter', // TEMPORARY
'onMouseLeave', // TEMPORARY
Expand All @@ -156,3 +153,7 @@ export const strictAttributeSet: Set<string> = new Set([
'onTouchMove', // TEMPORARY
'onTouchStart' // TEMPORARY
]);

export function isPropAllowed(key: string): boolean {
return strictAttributeSet.has(key) || key.indexOf('data-') > -1;
}
12 changes: 12 additions & 0 deletions packages/react-strict-dom/src/types/DataProps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/

export type DataProps = $ReadOnly<{
'data-testid'?: string
}>;
5 changes: 2 additions & 3 deletions packages/react-strict-dom/src/types/StrictReactDOMProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import type { StrictElement } from './StrictElement';
import type { DataProps } from './DataProps';
import type { ReactRef } from './Utilities';
import type { Styles } from './styles';

Expand Down Expand Up @@ -202,12 +203,10 @@ type EventProps = $ReadOnly<{
export type StrictReactDOMProps = $ReadOnly<{
...AriaProps,
...EventProps,
...DataProps,
autoCapitalize?: 'off' | 'none' | 'on' | 'sentences' | 'words' | 'characters',
autoFocus?: boolean,
children?: React$Node,
'data-imgperflogname'?: string, // TEMPORARY
'data-testid'?: string,
'data-visualcompletion'?: string, // TEMPORARY
dir?: 'auto' | 'ltr' | 'rtl',
elementTiming?: string,
enterKeyHint?:
Expand Down

0 comments on commit 43b8181

Please sign in to comment.