forked from microsoft/fluentui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding getNativeElementProps (microsoft#13272)
* Exporting a getNativeElementProps function. * Change files * update api. * pr feedback. * fixing lint
- Loading branch information
Showing
5 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
change/@uifabric-utilities-2020-05-21-12-59-12-feat-getNativeElementProps.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"type": "minor", | ||
"comment": "Adding `getNativeElementProps` helper which takes the element tag name in + props and filters out unrecognized props.", | ||
"packageName": "@uifabric/utilities", | ||
"email": "dzearing@microsoft.com", | ||
"dependentChangeType": "patch", | ||
"date": "2020-05-21T19:59:12.182Z" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { getNativeElementProps } from './getNativeElementProps'; | ||
|
||
describe('getNativeElementProps', () => { | ||
it('can filter native element properties', () => { | ||
expect(getNativeElementProps('div', { id: '123', checked: true })).toEqual({ id: '123' }); | ||
expect(getNativeElementProps('input', { id: '123', checked: true })).toEqual({ id: '123', checked: true }); | ||
expect(getNativeElementProps('input', { id: '123', checked: true }, ['id'])).toEqual({ checked: true }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { | ||
labelProperties, | ||
audioProperties, | ||
videoProperties, | ||
olProperties, | ||
liProperties, | ||
anchorProperties, | ||
buttonProperties, | ||
inputProperties, | ||
textAreaProperties, | ||
selectProperties, | ||
optionProperties, | ||
tableProperties, | ||
trProperties, | ||
thProperties, | ||
tdProperties, | ||
colGroupProperties, | ||
colProperties, | ||
formProperties, | ||
iframeProperties, | ||
imgProperties, | ||
htmlElementProperties, | ||
getNativeProps, | ||
} from './properties'; | ||
import * as React from 'react'; | ||
|
||
const nativeElementMap: Record<string, string[]> = { | ||
label: labelProperties, | ||
audio: audioProperties, | ||
video: videoProperties, | ||
ol: olProperties, | ||
li: liProperties, | ||
a: anchorProperties, | ||
button: buttonProperties, | ||
input: inputProperties, | ||
textarea: textAreaProperties, | ||
select: selectProperties, | ||
option: optionProperties, | ||
table: tableProperties, | ||
tr: trProperties, | ||
th: thProperties, | ||
td: tdProperties, | ||
colGroup: colGroupProperties, | ||
col: colProperties, | ||
form: formProperties, | ||
iframe: iframeProperties, | ||
img: imgProperties, | ||
}; | ||
|
||
/** | ||
* Given an element tagname and user props, filters the props to only allowed props for the given | ||
* element type. | ||
* @param tagName - Tag name (e.g. "div") | ||
* @param props - Props object | ||
* @param excludedPropNames - List of props to disallow | ||
*/ | ||
// tslint:disable-next-line:no-any | ||
export function getNativeElementProps<TAttributes extends React.HTMLAttributes<any>>( | ||
tagName: keyof React.ReactHTML, | ||
props: {}, | ||
excludedPropNames?: string[], | ||
): TAttributes { | ||
const allowedPropNames = (tagName && nativeElementMap[tagName]) || htmlElementProperties; | ||
|
||
return getNativeProps(props, allowedPropNames, excludedPropNames); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters