Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "chore: deprecate getNativeElementProps in favor of getIntrinsicElementProps",
"packageName": "@fluentui/react-components",
"email": "bernardo.sunderhus@gmail.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "chore: deprecate getNativeElementProps in favor of getIntrinsicElementProps",
"packageName": "@fluentui/react-utilities",
"email": "bernardo.sunderhus@gmail.com",
"dependentChangeType": "patch"
}
2 changes: 2 additions & 0 deletions packages/react-components/react-components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export {
usePortalMountNode,
} from '@fluentui/react-shared-contexts';
export {
// getNativeElementProps is deprecated but removing it would be a breaking change
// eslint-disable-next-line deprecation/deprecation
getNativeElementProps,
getIntrinsicElementProps,
getPartitionedNativeProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function getEventClientCoords(event: TouchOrMouseEvent): {
// @public
export const getIntrinsicElementProps: <Props extends UnknownSlotProps, ExcludedPropKeys extends Extract<keyof Props, string> = never>(tagName: NonNullable<Props["as"]>, props: Props & React_2.RefAttributes<InferredElementRefType<Props>>, excludedPropNames?: ExcludedPropKeys[] | undefined) => OmitWithoutExpanding<Props, ExcludedPropKeys | Exclude<keyof Props, "as" | keyof HTMLAttributes>>;

// @public
// @public @deprecated
export function getNativeElementProps<TAttributes extends React_2.HTMLAttributes<any>>(tagName: string, props: {}, excludedPropNames?: string[]): TAttributes;

// @internal
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getIntrinsicElementProps } from './getIntrinsicElementProps';

describe('getIntrinsicElementProps', () => {
type Props = { as?: 'span' | 'div' | 'input'; id?: string; checked?: boolean };
it('can filter native element properties', () => {
expect(getIntrinsicElementProps<Props>('div', { id: '123', checked: true })).toEqual({ id: '123' });
expect(getIntrinsicElementProps<Props>('input', { id: '123', checked: true })).toEqual({
id: '123',
checked: true,
});
expect(getIntrinsicElementProps<Props, 'id'>('input', { id: '123', checked: true }, ['id'])).toEqual({
checked: true,
});
});

it('includes `as` as a native prop', () => {
expect(getIntrinsicElementProps<Props>('div', { as: 'span' })).toEqual({ as: 'span' });
});

it('excludes props regardless of the allowed', () => {
expect(getIntrinsicElementProps<Props, 'as'>('div', { as: 'span' }, ['as'])).toEqual({});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const getIntrinsicElementProps = <
/** List of native props to exclude from the returned value */
excludedPropNames?: ExcludedPropKeys[],
) => {
// eslint-disable-next-line deprecation/deprecation
return getNativeElementProps<
OmitWithoutExpanding<Props, Exclude<keyof Props, keyof HTMLAttributes | keyof UnknownSlotProps> | ExcludedPropKeys>
>(props.as ?? tagName, props, excludedPropNames);
Expand Down
1 change: 1 addition & 0 deletions packages/react-components/react-utilities/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export { canUseDOM, useIsSSR, SSRProvider } from './ssr/index';

export {
clamp,
// eslint-disable-next-line deprecation/deprecation
getNativeElementProps,
getPartitionedNativeProps,
getRTLSafeKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ import { getNativeElementProps } from './getNativeElementProps';

describe('getNativeElementProps', () => {
it('can filter native element properties', () => {
// eslint-disable-next-line deprecation/deprecation
expect(getNativeElementProps('div', { id: '123', checked: true })).toEqual({ id: '123' });
// eslint-disable-next-line deprecation/deprecation
expect(getNativeElementProps('input', { id: '123', checked: true })).toEqual({ id: '123', checked: true });
// eslint-disable-next-line deprecation/deprecation
expect(getNativeElementProps('input', { id: '123', checked: true }, ['id'])).toEqual({ checked: true });
});

it('includes `as` as a native prop', () => {
// eslint-disable-next-line deprecation/deprecation
expect(getNativeElementProps('div', { as: 'span' })).toEqual({ as: 'span' });
});

it('excludes props regardless of the allowed', () => {
// eslint-disable-next-line deprecation/deprecation
expect(getNativeElementProps('div', { as: 'span' }, ['as'])).toEqual({});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ const nativeElementMap: Record<string, Record<string, number>> = {
* @param tagName - Tag name (e.g. "div")
* @param props - Props object
* @param excludedPropNames - List of props to disallow
*
* @deprecated use getIntrinsicElementProps instead, it is a type-safe version of this method
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getNativeElementProps<TAttributes extends React.HTMLAttributes<any>>(
Expand Down Expand Up @@ -99,6 +101,7 @@ export const getPartitionedNativeProps = <
}) => {
return {
root: { style: props.style, className: props.className },
// eslint-disable-next-line deprecation/deprecation
primary: getNativeElementProps<Omit<Props, ExcludedPropKeys>>(primarySlotTagName, props, [
...(excludedPropNames || []),
'style',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { getNativeElementProps, slot } from '@fluentui/react-utilities';
import { getIntrinsicElementProps, slot } from '@fluentui/react-utilities';
import type { <%= componentName %>Props, <%= componentName %>State } from './<%= componentName %>.types';

/**
Expand All @@ -9,9 +9,9 @@ import type { <%= componentName %>Props, <%= componentName %>State } from './<%=
* before being passed to render<%= componentName %>_unstable.
*
* @param props - props from this instance of <%= componentName %>
* @param ref - reference to root HTMLElement of <%= componentName %>
* @param ref - reference to root HTMLDivElement of <%= componentName %>
*/
export const use<%= componentName %>_unstable = (props: <%= componentName %>Props, ref: React.Ref<HTMLElement>): <%= componentName %>State => {
export const use<%= componentName %>_unstable = (props: <%= componentName %>Props, ref: React.Ref<HTMLDivElement>): <%= componentName %>State => {
return {
// TODO add appropriate props/defaults
components: {
Expand All @@ -20,7 +20,7 @@ export const use<%= componentName %>_unstable = (props: <%= componentName %>Prop
},
// TODO add appropriate slots, for example:
// mySlot: resolveShorthand(props.mySlot),
root: slot.always(getNativeElementProps('div', {
root: slot.always(getIntrinsicElementProps('div', {
ref,
...props,
}), {elementType: 'div'}),
Expand Down