Skip to content

Commit e0a028c

Browse files
authored
Merge pull request #99 from brawcode/issue-84/upstream
Allow React.ComponentType to be used for the `as` prop
2 parents 991afbf + 2c3bff0 commit e0a028c

File tree

4 files changed

+20
-29
lines changed

4 files changed

+20
-29
lines changed

src/components/Space.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ISpaceProps, CenterType, ResizeHandlePlacement, AnchorType } from "../core-types";
2-
import { useSpace, ParentContext, LayerContext, DOMRectContext } from "../core-react";
1+
import { CenterType, ResizeHandlePlacement, AnchorType } from "../core-types";
2+
import { useSpace, ParentContext, LayerContext, DOMRectContext, IReactSpaceProps } from "../core-react";
33
import * as React from "react";
44
import { Centered } from "./Centered";
55
import { CenteredVertically } from "./CenteredVertically";
@@ -15,13 +15,13 @@ function applyCentering(children: React.ReactNode, centerType: CenterType | unde
1515
return children;
1616
}
1717

18-
export class Space extends React.Component<ISpaceProps> {
18+
export class Space extends React.Component<IReactSpaceProps> {
1919
public render() {
2020
return <SpaceInner {...this.props} wrapperInstance={this} />;
2121
}
2222
}
2323

24-
const SpaceInner: React.FC<ISpaceProps & { wrapperInstance: Space }> = (props) => {
24+
const SpaceInner: React.FC<IReactSpaceProps & { wrapperInstance: Space }> = (props) => {
2525
if (!props.id && !props.wrapperInstance["_react_spaces_uniqueid"]) {
2626
props.wrapperInstance["_react_spaces_uniqueid"] = `s${shortuuid()}`;
2727
}

src/components/stories/Utils.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ export const StandardProps = () => (
8282
<PropsHeader>Standard properties</PropsHeader>
8383
<Prop
8484
name="as"
85-
type="string"
85+
type="string | React.ComponentType<ICommonProps>"
8686
default="div"
87-
description="Allows control over the outputted HTML element allowing HTML 5 semantic markup to be created."
87+
description="Allows control over the outputted HTML element either through HTML 5 semantic markup or a custom Component."
8888
/>
8989
<Prop
9090
name="centerContent"

src/core-react.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from "react";
22
import { createStore } from "./core";
3-
import { ISpaceProps, ISpaceStore, ISpaceDefinition, IPositionalProps, ResizeType, CenterType } from "./core-types";
3+
import { ISpaceProps, ISpaceStore, ISpaceDefinition, IPositionalProps, ResizeType, CenterType, ICommonProps } from "./core-types";
44
import { coalesce, shortuuid } from "./core-utils";
55
import { ResizeSensor } from "css-element-queries";
66
import * as PropTypes from "prop-types";
@@ -15,7 +15,7 @@ export const commonProps = {
1515
id: PropTypes.string,
1616
className: PropTypes.string,
1717
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
18-
as: PropTypes.string,
18+
as: PropTypes.any,
1919
centerContent: PropTypes.oneOf([CenterType.None, CenterType.Vertical, CenterType.HorizontalVertical]),
2020
zIndex: PropTypes.number,
2121
scrollable: PropTypes.bool,
@@ -30,10 +30,6 @@ export const commonProps = {
3030
onTouchEnd: PropTypes.func,
3131
};
3232

33-
export interface IReactSpacesOptions {
34-
debug?: boolean
35-
}
36-
3733
export interface IReactEvents {
3834
onClick?: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void;
3935
onDoubleClick?: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void;
@@ -46,6 +42,15 @@ export interface IReactEvents {
4642
onTouchEnd?: (event: React.TouchEvent<HTMLElement>) => void;
4743
}
4844

45+
export interface IReactSpaceProps extends ISpaceProps, IReactEvents {
46+
style?: React.CSSProperties;
47+
as?: keyof React.ReactDOM | React.ComponentType<ICommonProps>;
48+
}
49+
50+
export interface IReactSpacesOptions {
51+
debug?: boolean;
52+
}
53+
4954
export function useForceUpdate() {
5055
const [, setTick] = React.useState(0);
5156
const update = React.useCallback(() => {
@@ -54,7 +59,7 @@ export function useForceUpdate() {
5459
return update;
5560
}
5661

57-
export function useSpace(props: ISpaceProps) {
62+
export function useSpace(props: IReactSpaceProps) {
5863
const store = currentStore;
5964
const update = useForceUpdate();
6065
const parent = React.useContext(ParentContext);
@@ -179,6 +184,6 @@ export function useSpaceResizeHandles(store: ISpaceStore, space: ISpaceDefinitio
179184
}
180185

181186
return {
182-
mouseHandles
187+
mouseHandles,
183188
};
184189
}

src/core-types.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,9 @@ export enum CenterType {
4444
HorizontalVertical = "horizontalVertical",
4545
}
4646

47-
export interface IPassThroughEvents {
48-
onClick?: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void;
49-
onDoubleClick?: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void;
50-
onMouseDown?: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void;
51-
onMouseEnter?: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void;
52-
onMouseLeave?: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void;
53-
onMouseMove?: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void;
54-
onTouchStart?: (event: React.TouchEvent<HTMLElement>) => void;
55-
onTouchMove?: (event: React.TouchEvent<HTMLElement>) => void;
56-
onTouchEnd?: (event: React.TouchEvent<HTMLElement>) => void;
57-
}
58-
59-
export interface ICommonProps extends IPassThroughEvents {
47+
export interface ICommonProps {
6048
id?: string;
6149
className?: string;
62-
style?: React.CSSProperties;
63-
as?: string;
6450
centerContent?: CenterType;
6551
zIndex?: number;
6652
scrollable?: boolean;

0 commit comments

Comments
 (0)