Skip to content

Commit

Permalink
Merge pull request #75 from trixtateam/fix/update_children_types
Browse files Browse the repository at this point in the history
fix: children type props
  • Loading branch information
jacqueswho authored Oct 10, 2022
2 parents 622f515 + 477a5ee commit b328afa
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 27 deletions.
13 changes: 2 additions & 11 deletions src/React/components/TrixtaAuthComponent/TrixtaAuthComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,13 @@ import { TrixtaAuthProps } from './types';
const TrixtaAuthComponent = ({
children,
roleName,
...rest
}: TrixtaAuthProps & { children: React.ReactNode }): JSX.Element | null => {
}: TrixtaAuthProps): React.ReactElement | null => {
const roleAccessSelector = useMemo(makeSelectHasTrixtaRoleAccessForRole, []);
const hasRoleAccess = useSelector((state: { trixta: TrixtaState }) =>
roleAccessSelector(state, { roleName }),
);

if (!hasRoleAccess) return null;

if (typeof children === 'function') {
return children(rest);
}

if (React.isValidElement(children)) {
return React.cloneElement(children, rest);
}
return null;
return typeof children === 'function' ? children() : <>{children}</>;
};
export default TrixtaAuthComponent;
2 changes: 1 addition & 1 deletion src/React/components/TrixtaAuthComponent/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export interface TrixtaAuthProps {
/**
* Children can be a render props function or a react component
*/
children?: React.ReactNode;
children?: React.ReactNode | (() => React.ReactNode);
}
2 changes: 1 addition & 1 deletion src/React/components/TrixtaFormComponent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function TrixtaFormComponent<TFormData = never>({
formData,
uiSchema,
formContext,
}: TrixtaReactJsonSchemaFormProps<TFormData>): JSX.Element {
}: TrixtaReactJsonSchemaFormProps<TFormData>): React.ReactElement {
if (ThemedForm) {
const { formContext: formContextThemeProp, ...formProps } = config.props;
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ function TrixtaActionComponent({
};

if (!renderResponse) {
if (React.isValidElement(children)) {
return React.cloneElement(children, { ...actionProps, ...rest });
}
if (typeof children === 'function') {
return children({ ...actionProps, ...rest });
}
if (React.isValidElement(children)) {
return React.cloneElement(children, { ...actionProps, ...rest });
}

if (!children && common) {
const formData =
Expand Down
5 changes: 4 additions & 1 deletion src/React/components/actions/TrixtaActionComponent/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
TrixtaActionBaseProps,
TrixtaActionDebugOptions,
} from '../../../types';
import { TrixtaActionComponentArgs } from '../types';

export interface TrixtaActionComponentProps extends TrixtaActionBaseProps {
/**
Expand Down Expand Up @@ -60,5 +61,7 @@ export interface TrixtaActionComponentProps extends TrixtaActionBaseProps {
/**
* Children can be a render props function or a react component
*/
children?: React.ReactNode;
children?:
| React.ReactNode
| ((props: TrixtaActionComponentArgs) => React.ReactNode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ export interface TrixtaActionResponseComponentProps
/**
* Children can be a render props function or a react component
*/
children?: React.ReactNode;
children?:
| React.ReactNode
| ((props: TrixtaActionResponseComponentArgs) => React.ReactNode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { RespondToReactionsComponentProps } from './types';
export const RespondToReactionsComponent = ({
roleName,
reactions,
}: RespondToReactionsComponentProps): JSX.Element => (
}: RespondToReactionsComponentProps): React.ReactElement => (
<>
{reactions.map((reaction) => (
<RespondToReactionComponent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ export interface TrixtaReactionComponentProps extends TrixtaReactionBaseProps {
* Enables Trixta console debugging
*/
debugMode?: boolean;
/**
* Children can be a render props function or a react component
*/
children?: React.ReactNode;
/**
* Default component to render if there are no Trixta reaction response instances
*/
defaultComponent?: React.ReactNode;
/**
* Children can be a render props function or a react component
*/
children?: React.ReactNode;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TrixtaCommon, TrixtaReactionBaseProps } from '../../../types';
import { TrixtaReactionComponentArgs } from '../types';

export interface TrixtaReactionInstanceComponentProps
extends TrixtaReactionBaseProps {
Expand Down Expand Up @@ -38,9 +39,11 @@ export interface TrixtaReactionInstanceComponentProps
* Enables Trixta console debbugging. If 'true', open the console and see verbose logging
*/
debugMode: boolean;
instanceRef: string;
/**
* Children can be a render props function or a react component
*/
children?: React.ReactNode;
instanceRef: string;
children?:
| React.ReactNode
| ((props: TrixtaReactionComponentArgs) => React.ReactNode);
}
2 changes: 1 addition & 1 deletion src/React/widgets/TrixtaLoginWidget/TrixtaLoginWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { TrixtaLoginWidgetProps } from './types';
*/
const TrixtaLoginWidget = (
props: TrixtaLoginWidgetProps,
): JSX.Element | null => {
): React.ReactElement | null => {
return (
<TrixtaActionComponent
roleName={ReservedTrixtaRoles.EVERYONE_ANON}
Expand Down
2 changes: 1 addition & 1 deletion src/stories/utils/FormComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function TrixtaFormComponent<TFormData = never>({
formData,
uiSchema,
formContext,
}: TrixtaReactJsonSchemaFormProps<TFormData>): JSX.Element {
}: TrixtaReactJsonSchemaFormProps<TFormData>): React.ReactElement {
if (ThemedForm) {
return (
<ThemedForm
Expand Down

0 comments on commit b328afa

Please sign in to comment.