Skip to content

Commit

Permalink
feat: change folder structures
Browse files Browse the repository at this point in the history
  • Loading branch information
nakzyu committed Jun 11, 2024
1 parent 87d0ff0 commit f99b6e0
Show file tree
Hide file tree
Showing 18 changed files with 258 additions and 108 deletions.
7 changes: 3 additions & 4 deletions packages/example-ui/src/disclosure/disclosure-example.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import {
CloseButton,
Disclosure,
DisclosureButton,
DisclosurePanel,
} from "headlessui-react-native";
import React from "react";
import { View, Text, ViewStyle, TextStyle } from "react-native";
import { Text, TextStyle, View, ViewStyle } from "react-native";

export function DisclosureExample() {
return (
<View>
<Disclosure style={disclosureStyle}>
<Disclosure style={disclosureStyle} as="View">
<DisclosureButton style={disclosureButtonStyle}>
{({ open }) => (
<>
Expand All @@ -27,7 +26,7 @@ export function DisclosureExample() {
</Text>
</DisclosurePanel>
</Disclosure>
<Disclosure style={disclosureStyle}>
<Disclosure style={disclosureStyle} as="View">
<DisclosureButton style={disclosureButtonStyle}>
{({ open }) => (
<>
Expand Down
19 changes: 0 additions & 19 deletions packages/headlessui-react-native/src/closeButton.tsx

This file was deleted.

22 changes: 22 additions & 0 deletions packages/headlessui-react-native/src/components/close-button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
PropsType,
ReactNativeComponentProps,
ReactNativeComponentType,
} from "../constants";
import { useUIContext } from "../hooks";
import { createReactNativeElement } from "../utils";

export const CloseButton = <T extends ReactNativeComponentType = "Pressable">({
as = "Pressable" as T,
children,
...props
}: ReactNativeComponentProps<T>) => {
const { onClose } = useUIContext();
const Component = createReactNativeElement(
as,
{ ...(props as PropsType<T>), onPress: onClose },
children
);

return Component;
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import React, { useState } from "react";
import { View, ViewProps, PressableProps, Pressable } from "react-native";
import { UIContext, useUIContext } from "./useUIContext";
import { CallableChildren } from "./callableChildren";
import { RenderPropsCallableComponent } from "./types";
import { Pressable, PressableProps, Text, View, ViewProps } from "react-native";
import {
PropsType,
ReactNativeComponentProps,
ReactNativeComponentType,
RenderPropsCallableComponent,
} from "../constants";
import { UIContext, useUIContext } from "../hooks";
import { createReactNativeElement } from "../utils";
import { CallableChildren } from "./callable-children";
import { Menu } from "./dropdown-menu";

export type DisclosureProps = {
defaultOpen?: boolean;
Expand Down Expand Up @@ -40,6 +47,9 @@ export const Disclosure = ({
children={children}
props={{ close: onClose, open: !!isOpen }}
/>
<Menu as="Pressable">
<Text>dsasda</Text>
</Menu>
</View>
</UIContext.Provider>
);
Expand Down Expand Up @@ -96,3 +106,28 @@ export const DisclosurePanel = ({
</View>
);
};

export const ExmapleDisclosurePanel = <
T extends ReactNativeComponentType = "View"
>({
as = "View" as T,
children,
...props
}: ReactNativeComponentProps<T>) => {
const { onClose, open } = useUIContext();

if (!open) return null;

const Component = createReactNativeElement(
as,
{ ...(props as PropsType<T>), onPress: onClose },
<CallableChildren
children={children}
props={{ close: onClose, open: !!open }}
/>
);

return Component;
};

<ExmapleDisclosurePanel>{}</ExmapleDisclosurePanel>;
34 changes: 34 additions & 0 deletions packages/headlessui-react-native/src/components/dropdown-menu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ReactNode } from "react";
import {
PropsType,
ReactNativeComponentProps,
ReactNativeComponentType,
} from "../constants";
import { createReactNativeElement } from "../utils";

export const MenuButton = <T extends ReactNativeComponentType>({
as = "Button" as T,
children,
...rest
}: {
as?: T;
children?: ReactNode;
} & PropsType<T>) => {
// The rest props need to be typed correctly based on the component type
const props = rest as PropsType<T>;

const Component = createReactNativeElement(as, props, children);
return Component;
};

export const Menu = <T extends ReactNativeComponentType = "View">({
as = "View" as T,
children,
...rest
}: ReactNativeComponentProps<T>) => {
// The rest props need to be typed correctly based on the component type
const props = rest as PropsType<T>;

const Component = createReactNativeElement(as, props, children);
return Component;
};
3 changes: 3 additions & 0 deletions packages/headlessui-react-native/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./close-button";
export * from "./disclosure";
export * from "./modal";
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
View,
ViewProps,
} from "react-native";
import { UIContext, useUIContext } from "./useUIContext";
import { CallableChildren } from "./callableChildren";
import { RenderPropsCallableComponent } from "./types";
import { RenderPropsCallableComponent } from "../constants";
import { UIContext, useUIContext } from "../hooks";
import { CallableChildren } from "./callable-children";

export type ModalProps = {
onClose: () => void;
Expand Down
51 changes: 51 additions & 0 deletions packages/headlessui-react-native/src/constants/constatns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
ActivityIndicator,
Button,
DrawerLayoutAndroid,
FlatList,
Image,
ImageBackground,
InputAccessoryView,
KeyboardAvoidingView,
Modal,
Pressable,
RefreshControl,
ScrollView,
SectionList,
StatusBar,
Switch,
Text,
TextInput,
TouchableHighlight,
TouchableNativeFeedback,
TouchableOpacity,
TouchableWithoutFeedback,
View,
VirtualizedList,
} from "react-native";

export const COMPONENT_MAP = {
ActivityIndicator,
Button,
FlatList,
Image,
ImageBackground,
KeyboardAvoidingView,
Modal,
Pressable,
RefreshControl,
ScrollView,
SectionList,
StatusBar,
Switch,
Text,
TextInput,
TouchableHighlight,
TouchableOpacity,
TouchableWithoutFeedback,
View,
VirtualizedList,
DrawerLayoutAndroid,
TouchableNativeFeedback,
InputAccessoryView,
};
2 changes: 2 additions & 0 deletions packages/headlessui-react-native/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./constatns";
export * from "./types";
70 changes: 70 additions & 0 deletions packages/headlessui-react-native/src/constants/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { ReactNode } from "react";

import {
ActivityIndicatorProps,
ButtonProps,
DrawerLayoutAndroidProps,
FlatListProps,
ImageBackgroundProps,
ImageProps,
InputAccessoryViewProps,
KeyboardAvoidingViewProps,
ModalProps,
PressableProps,
RefreshControlProps,
ScrollViewProps,
SectionListProps,
StatusBarProps,
SwitchProps,
TextInputProps,
TextProps,
TouchableHighlightProps,
TouchableNativeFeedbackProps,
TouchableOpacityProps,
TouchableWithoutFeedbackProps,
ViewProps,
VirtualizedListProps,
} from "react-native";

export type RenderPropsCallableComponent<
T extends ViewProps | PressableProps,
/* props to return */
U extends unknown
> = {
children: ReactNode | ((props: U) => ReactNode);
} & Omit<T, "children">;

type ReactNativeComponentTypeMap = {
ActivityIndicator: ActivityIndicatorProps;
Button: ButtonProps;
FlatList: FlatListProps<any>;
Image: ImageProps;
ImageBackground: ImageBackgroundProps;
KeyboardAvoidingView: KeyboardAvoidingViewProps;
Modal: ModalProps;
Pressable: PressableProps;
RefreshControl: RefreshControlProps;
ScrollView: ScrollViewProps;
SectionList: SectionListProps<any>;
StatusBar: StatusBarProps;
Switch: SwitchProps;
Text: TextProps;
TextInput: TextInputProps;
TouchableHighlight: TouchableHighlightProps;
TouchableOpacity: TouchableOpacityProps;
TouchableWithoutFeedback: TouchableWithoutFeedbackProps;
View: ViewProps;
VirtualizedList: VirtualizedListProps<any>;
DrawerLayoutAndroid: DrawerLayoutAndroidProps;
TouchableNativeFeedback: TouchableNativeFeedbackProps;
InputAccessoryView: InputAccessoryViewProps;
};
export type ReactNativeComponentType = keyof ReactNativeComponentTypeMap;

export type PropsType<T extends ReactNativeComponentType> =
ReactNativeComponentTypeMap[T];

export type ReactNativeComponentProps<T extends ReactNativeComponentType> = {
as?: T;
children?: ReactNode;
} & PropsType<T>;
1 change: 1 addition & 0 deletions packages/headlessui-react-native/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./useUIContext";
7 changes: 4 additions & 3 deletions packages/headlessui-react-native/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./closeButton";
export * from "./disclosure";
export * from "./modal";
export * from "./components";
export * from "./constants";
export * from "./hooks";
export * from "./utils";
10 changes: 0 additions & 10 deletions packages/headlessui-react-native/src/types.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React, { ReactNode } from "react";
import {
COMPONENT_MAP,
PropsType,
ReactNativeComponentType,
} from "../constants";

export function createReactNativeElement<T extends ReactNativeComponentType>(
type: T,
props?: PropsType<T>,
children?: ReactNode
) {
const Component = COMPONENT_MAP[type];

return React.createElement(
// @ts-ignore
Component,
props,
children
);
}
1 change: 1 addition & 0 deletions packages/headlessui-react-native/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./createReactNativeElement";
Loading

0 comments on commit f99b6e0

Please sign in to comment.