Skip to content

Commit

Permalink
typescript: Allow setting ref to null and ran prettier on source files
Browse files Browse the repository at this point in the history
  • Loading branch information
Sampo Kivistö committed Oct 13, 2022
1 parent eb88122 commit 12be729
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/inferno-compat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ abstract class PureComponent<P, S> extends Component<P, S> {
}

interface ContextProps {
children?: Inferno.InfernoChild | undefined
children?: Inferno.InfernoChild | undefined;
context: any;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/inferno-create-class/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component, Inferno} from 'inferno';
import { Component, Inferno } from 'inferno';
import { isFunction, throwError } from 'inferno-shared';

export interface Mixin<P, S> extends Component<P, S> {
Expand Down
4 changes: 2 additions & 2 deletions packages/inferno-extras/src/findDOMNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component, VNode, findDOMFromVNode} from 'inferno';
import { Component, VNode, findDOMFromVNode } from 'inferno';

export function findDOMNode(ref: VNode | Component | Node): Node | null {
if (ref && (ref as Node).nodeType) {
Expand All @@ -14,7 +14,7 @@ export function findDOMNode(ref: VNode | Component | Node): Node | null {
}

if ((ref as VNode).flags) {
return findDOMFromVNode((ref as VNode), true);
return findDOMFromVNode(ref as VNode, true);
}

return null;
Expand Down
34 changes: 25 additions & 9 deletions packages/inferno/__tests__/types.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Component, createRef, Inferno, linkEvent, render} from 'inferno';
import {emptyFn} from "inferno-shared";
import { Component, createRef, Inferno, linkEvent, Ref, render } from 'inferno';
import { emptyFn } from 'inferno-shared';

describe('top level context', () => {
let container;
Expand Down Expand Up @@ -167,9 +167,12 @@ describe('top level context', () => {
describe('ChildFlags', function () {
it('Should allow special flags on all elements', function () {
const refObj = createRef<HTMLDivElement>();
const text = "foobar";
const row = <div className="floating-row-number" $HasTextChildren onClick={emptyFn}>{text}</div>;

const text = 'foobar';
const row = (
<div className="floating-row-number" $HasTextChildren onClick={emptyFn}>
{text}
</div>
);

render(
<div className="floating-row-numbers" ref={refObj} $HasVNodeChildren>
Expand All @@ -181,9 +184,12 @@ describe('top level context', () => {

it('Should allow special flags on SVG', function () {
const refObj = createRef<SVGSVGElement>();
const text = "foobar";
const row = <div className="floating-row-number" $HasTextChildren onClick={emptyFn}>{text}</div>;

const text = 'foobar';
const row = (
<div className="floating-row-number" $HasTextChildren onClick={emptyFn}>
{text}
</div>
);

render(
<svg className="floating-row-numbers" ref={refObj} $HasVNodeChildren>
Expand All @@ -200,5 +206,15 @@ describe('top level context', () => {
expect(row).not.toBeNull();
expect(aria).not.toBeNull();
});
})

it('Should allow null for ref attribute', function () {
const obj: {
refWrap: Ref<HTMLDivElement> | null;
} = {
refWrap: null
};

render(<div ref={obj.refWrap}></div>, container);
});
});
});
2 changes: 1 addition & 1 deletion packages/inferno/src/core/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export abstract class Component<P = {}, S = {}> implements IComponent<P, S> {
public $F: boolean = false; // Force update flag

constructor(props?: P, context?: any) {
this.props = (props || (EMPTY_OBJ)) as Readonly<{ children?: Inferno.InfernoNode | undefined }> & Readonly<P>;
this.props = (props || EMPTY_OBJ) as Readonly<{ children?: Inferno.InfernoNode | undefined }> & Readonly<P>;
this.context = context || EMPTY_OBJ; // context should not be mutable
}

Expand Down
12 changes: 7 additions & 5 deletions packages/inferno/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ export interface LinkedEvent<T, E extends Event> {
export interface IComponent<P, S> {
// Public
state: S | null;
props: Readonly<{
children?: Inferno.InfernoNode;
} & P>;
props: Readonly<
{
children?: Inferno.InfernoNode;
} & P
>;
context?: any;
displayName?: string;
refs?: any;
Expand Down Expand Up @@ -171,7 +173,7 @@ export declare namespace Inferno {
$ChildFlag?: number;
}
interface ClassAttributes<T> extends Attributes {
ref?: Ref<T> | RefObject<T> | undefined;
ref?: Ref<T> | RefObject<T> | null | undefined;
}

interface InfernoElement<P = any> {
Expand Down Expand Up @@ -1881,7 +1883,7 @@ declare global {

type LibraryManagedAttributes<C, P> = InfernoManagedAttributes<C, P>;

interface IntrinsicAttributes extends Inferno.Attributes { }
interface IntrinsicAttributes extends Inferno.Attributes {}
interface IntrinsicAttributes extends Inferno.Attributes, Refs<any> {}
interface IntrinsicClassAttributes<T> extends Inferno.ClassAttributes<T> {}

Expand Down
24 changes: 12 additions & 12 deletions scripts/rollup/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import nodeResolve from 'rollup-plugin-node-resolve';
import replacePlugin from 'rollup-plugin-replace';
import { terser } from 'rollup-plugin-terser';
import { aliasPlugin } from './alias.js';
import babel from "@rollup/plugin-babel";
import babel from '@rollup/plugin-babel';

export function createPlugins(version, options) {
const plugins = [
Expand All @@ -18,17 +18,17 @@ export function createPlugins(version, options) {
})
];

plugins.push(babel({
exclude: 'node_modules/**',
sourceMaps: false,
babelrc: false,
presets: !options.esnext ? [['@babel/env', {loose: true, modules: false}]] : null,
babelHelpers: 'runtime',
skipPreflightCheck: true,
plugins: [
["@babel/plugin-proposal-class-properties", {"loose": true}]
]
}));
plugins.push(
babel({
exclude: 'node_modules/**',
sourceMaps: false,
babelrc: false,
presets: !options.esnext ? [['@babel/env', { loose: true, modules: false }]] : null,
babelHelpers: 'runtime',
skipPreflightCheck: true,
plugins: [['@babel/plugin-proposal-class-properties', { loose: true }]]
})
);

const replaceValues = {
'process.env.INFERNO_VERSION': JSON.stringify(options.version)
Expand Down

0 comments on commit 12be729

Please sign in to comment.