diff --git a/dev_plugin/src/grid-view.tsx b/dev_plugin/src/grid-view.tsx index 1180676..f051a2f 100644 --- a/dev_plugin/src/grid-view.tsx +++ b/dev_plugin/src/grid-view.tsx @@ -11,10 +11,10 @@ export default function GridView(): ReactElement { const [val4, setValue4] = useStorage("test", ""); return ( - {}}> + { numbers.map(value => ( - + Test Paragraph {value} @@ -24,7 +24,7 @@ export default function GridView(): ReactElement { )) } - + Test Paragraph Section 1 1 @@ -33,14 +33,14 @@ export default function GridView(): ReactElement { - + Test Paragraph Section 2 1 - + Test Paragraph Section 2 2 diff --git a/dev_plugin/src/hooks-view.tsx b/dev_plugin/src/hooks-view.tsx index babc285..5a0852c 100644 --- a/dev_plugin/src/hooks-view.tsx +++ b/dev_plugin/src/hooks-view.tsx @@ -6,77 +6,20 @@ export default function ListView(): ReactElement { const { pushView } = useNavigation(); return ( - { - switch (id) { - case "UsePromiseTestBasic": { - pushView() - break; - } - case "UsePromiseTestExecuteFalse": { - pushView() - break; - } - case "UsePromiseTestRevalidate": { - pushView() - break; - } - case "UsePromiseTestAbortableRevalidate": { - pushView() - break; - } - case "UsePromiseTestMutate": { - pushView() - break; - } - case "UsePromiseTestMutateOptimistic": { - pushView() - break; - } - case "UsePromiseTestMutateOptimisticRollback": { - pushView() - break; - } - case "UsePromiseTestMutateNoRevalidate": { - pushView() - break; - } - case "UsePromiseTestThrow": { - pushView() - break; - } - case "UseCachedPromiseBasic": { - pushView() - break; - } - case "UseCachedPromiseInitialState": { - pushView() - break; - } - case "UseFetchBasic": { - pushView() - break; - } - case "UseFetchMap": { - pushView() - break; - } - } - }} - > - - - - - - - - - - - - - + + pushView()}/> + pushView()}/> + pushView()}/> + pushView()}/> + pushView()}/> + pushView()}/> + pushView()}/> + pushView()}/> + pushView()}/> + pushView()}/> + pushView()}/> + pushView()}/> + pushView()}/> ) } @@ -91,12 +34,9 @@ function UsePromiseTestBasic(): ReactElement { printState(data, error, isLoading) return ( - + - + popView()}/> ) @@ -112,12 +52,9 @@ function UseCachedPromiseBasic(): ReactElement { printState(data, error, isLoading) return ( - + - + popView()}/> ) @@ -136,12 +73,9 @@ function UseCachedPromiseInitialState(): ReactElement { printState(data, error, isLoading) return ( - + - + popView()}/> ) @@ -160,12 +94,9 @@ function UsePromiseTestExecuteFalse(): ReactElement { printState(data, error, isLoading) return ( - + - + popView()}/> ) @@ -182,13 +113,10 @@ function UsePromiseTestRevalidate(): ReactElement { printState(data, error, isLoading) return ( - revalidate())} - > + - - + revalidate()}/> + popView()}/> ) @@ -211,13 +139,10 @@ function UsePromiseTestAbortableRevalidate(): ReactElement { printState(data, error, isLoading) return ( - revalidate())} - > + - - + revalidate()}/> + popView()}/> ) @@ -232,16 +157,14 @@ function UsePromiseTestMutate(): ReactElement { printState(data, error, isLoading) + const onClick = async () => { + await mutate(inNSec(5)) + }; return ( - { - await mutate(inNSec(5)) - })} - > + - - + + popView()}/> ) @@ -256,21 +179,20 @@ function UsePromiseTestMutateOptimistic(): ReactElement { printState(data, error, isLoading) + const onClick = async () => { + await mutate( + inNSec(5), + { + optimisticUpdate: data1 => data1 + " optimistic", + } + ) + }; + return ( - { - await mutate( - inNSec(5), - { - optimisticUpdate: data1 => data1 + " optimistic", - } - ) - })} - > + - - + + popView()}/> ) @@ -285,29 +207,28 @@ function UsePromiseTestMutateOptimisticRollback(): ReactElement { printState(data, error, isLoading) - return ( - { - await mutate( - new Promise((_resolve, reject) => { - setTimeout( - () => { - reject("fail") - }, - 5 * 1000 - ); - }), - { - optimisticUpdate: data1 => data1 + " optimistic", - rollbackOnError: data1 => data1 + " failed", - } + const onClick = async () => { + await mutate( + new Promise((_resolve, reject) => { + setTimeout( + () => { + reject("fail") + }, + 5 * 1000 ); - })} - > + }), + { + optimisticUpdate: data1 => data1 + " optimistic", + rollbackOnError: data1 => data1 + " failed", + } + ); + }; + + return ( + - - + + popView()}/> ) @@ -323,20 +244,17 @@ function UsePromiseTestMutateNoRevalidate(): ReactElement { printState(data, error, isLoading) return ( - { - await mutate( - inNSec(5), - { - shouldRevalidateAfter: false, - } - ) - })} - > + - - + async () => { + await mutate( + inNSec(5), + { + shouldRevalidateAfter: false, + } + ) + }}/> + popView()}/> ) @@ -354,12 +272,9 @@ function UsePromiseTestThrow(): ReactElement { printState(data, error, isLoading) return ( - + - + popView()}/> ) @@ -379,12 +294,9 @@ function UseFetchBasic(): ReactElement { printState(data, error, isLoading) return ( - + - + popView()}/> ) @@ -406,32 +318,14 @@ function UseFetchMap(): ReactElement { printState(data, error, isLoading) return ( - + - + popView()}/> ) } -function onSelectionChangeHandler(popView: () => void, handler?: () => void): (id: string) => void { - return (id) => { - switch (id) { - case "back": { - popView() - break; - } - case "run": { - handler?.() - break; - } - } - } -} - async function inNSec(n: number): Promise { return new Promise(resolve => { setTimeout( diff --git a/dev_plugin/src/list-view.tsx b/dev_plugin/src/list-view.tsx index ac0f04a..225962d 100644 --- a/dev_plugin/src/list-view.tsx +++ b/dev_plugin/src/list-view.tsx @@ -5,25 +5,25 @@ export default function ListView(): ReactElement { const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; const [id, setId] = useState("default"); + const onClick = () => { + console.log("onClick " + id) + setId(id); + }; + return ( - { - console.log("onSelectionChange " + id) - setId(id); - }} - > + { numbers.map(value => ( - + )) } - + - - - + + + ) diff --git a/dev_plugin/src/test-list-detail.tsx b/dev_plugin/src/test-list-detail.tsx index 72d6a6b..d5d5854 100644 --- a/dev_plugin/src/test-list-detail.tsx +++ b/dev_plugin/src/test-list-detail.tsx @@ -4,16 +4,16 @@ import { List } from "@project-gauntlet/api/components"; export default function Main(): ReactElement { return ( - - - - - - - - - - + + + + + + + + + + Sentient diff --git a/docs/js/components/grid_item/props/id.md b/docs/js/components/grid_item/props/id.md deleted file mode 100644 index eb10e7e..0000000 --- a/docs/js/components/grid_item/props/id.md +++ /dev/null @@ -1 +0,0 @@ -Identifier of the grid item. Used in the grid event functions \ No newline at end of file diff --git a/docs/js/components/grid/props/onSelectionChange.md b/docs/js/components/grid_item/props/onClick.md similarity index 52% rename from docs/js/components/grid/props/onSelectionChange.md rename to docs/js/components/grid_item/props/onClick.md index cc61cc0..4463e3d 100644 --- a/docs/js/components/grid/props/onSelectionChange.md +++ b/docs/js/components/grid_item/props/onClick.md @@ -1,2 +1 @@ -Function that will be called when user selects an item on the grid. -The only parameter is an id of the selected grid item \ No newline at end of file +Function that will be called when user selects an item on the grid. \ No newline at end of file diff --git a/docs/js/components/list/props/onSelectionChange.md b/docs/js/components/list/props/onSelectionChange.md deleted file mode 100644 index 2d2edf7..0000000 --- a/docs/js/components/list/props/onSelectionChange.md +++ /dev/null @@ -1,2 +0,0 @@ -Function that will be called when user selects an item on the list. -The only parameter is an id of the selected list item \ No newline at end of file diff --git a/docs/js/components/list_item/props/id.md b/docs/js/components/list_item/props/id.md deleted file mode 100644 index 3c98fc0..0000000 --- a/docs/js/components/list_item/props/id.md +++ /dev/null @@ -1 +0,0 @@ -Identifier of the list item. Used in the list event functions \ No newline at end of file diff --git a/docs/js/components/list_item/props/onClick.md b/docs/js/components/list_item/props/onClick.md new file mode 100644 index 0000000..4463e3d --- /dev/null +++ b/docs/js/components/list_item/props/onClick.md @@ -0,0 +1 @@ +Function that will be called when user selects an item on the grid. \ No newline at end of file diff --git a/js/api/src/gen/components.tsx b/js/api/src/gen/components.tsx index 410f2d2..046ff65 100644 --- a/js/api/src/gen/components.tsx +++ b/js/api/src/gen/components.tsx @@ -124,10 +124,10 @@ declare global { image?: ImageSource | Icons; }; ["gauntlet:list_item"]: { - id: string; title: string; subtitle?: string; icon?: ImageSource | Icons; + onClick?: () => void; }; ["gauntlet:list_section"]: { children?: ElementComponent; @@ -137,13 +137,12 @@ declare global { ["gauntlet:list"]: { children?: ElementComponent; isLoading?: boolean; - onSelectionChange?: (id: string) => void; }; ["gauntlet:grid_item"]: { children?: ElementComponent; - id: string; title: string; subtitle?: string; + onClick?: () => void; }; ["gauntlet:grid_section"]: { children?: ElementComponent; @@ -155,7 +154,6 @@ declare global { children?: ElementComponent; isLoading?: boolean; columns?: number; - onSelectionChange?: (id: string) => void; }; } } @@ -637,13 +635,13 @@ export const EmptyView: FC = (props: EmptyViewProps): ReactNode return ; }; export interface ListItemProps { - id: string; title: string; subtitle?: string; icon?: ImageSource | Icons; + onClick?: () => void; } export const ListItem: FC = (props: ListItemProps): ReactNode => { - return ; + return ; }; export interface ListSectionProps { children?: ElementComponent; @@ -660,7 +658,6 @@ export interface ListProps { children?: ElementComponent; actions?: ElementComponent; isLoading?: boolean; - onSelectionChange?: (id: string) => void; } export const List: FC & { EmptyView: typeof EmptyView; @@ -668,7 +665,7 @@ export const List: FC & { Item: typeof ListItem; Section: typeof ListSection; } = (props: ListProps): ReactNode => { - return {props.actions as any}{props.children}; + return {props.actions as any}{props.children}; }; List.EmptyView = EmptyView; List.Detail = Detail; @@ -676,14 +673,14 @@ List.Item = ListItem; List.Section = ListSection; export interface GridItemProps { children?: ElementComponent; - id: string; title: string; subtitle?: string; + onClick?: () => void; } export const GridItem: FC & { Content: typeof Content; } = (props: GridItemProps): ReactNode => { - return {props.children}; + return {props.children}; }; GridItem.Content = Content; export interface GridSectionProps { @@ -703,14 +700,13 @@ export interface GridProps { isLoading?: boolean; actions?: ElementComponent; columns?: number; - onSelectionChange?: (id: string) => void; } export const Grid: FC & { EmptyView: typeof EmptyView; Item: typeof GridItem; Section: typeof GridSection; } = (props: GridProps): ReactNode => { - return {props.actions as any}{props.children}; + return {props.actions as any}{props.children}; }; Grid.EmptyView = EmptyView; Grid.Item = GridItem; diff --git a/rust/client/src/ui/widget.rs b/rust/client/src/ui/widget.rs index 0ef09ab..abd28a7 100644 --- a/rust/client/src/ui/widget.rs +++ b/rust/client/src/ui/widget.rs @@ -883,11 +883,7 @@ impl ComponentWidgetWrapper { .center_y() .into() } - ComponentWidget::ListItem { id, title, subtitle, icon } => { - let ComponentRenderContext::List { widget_id: list_widget_id } = context else { - panic!("not supposed to be passed to list item: {:?}", context) - }; - + ComponentWidget::ListItem { title, subtitle, icon } => { let icon: Option> = icon.as_ref() .map(|icon| { match icon { @@ -931,7 +927,7 @@ impl ComponentWidgetWrapper { .into(); button(content) - .on_press(ComponentWidgetEvent::SelectListItem { list_widget_id, item_id: id.to_owned() }) + .on_press(ComponentWidgetEvent::ListItemClick { widget_id }) .width(Length::Fill) .themed(ButtonStyle::ListItem) } @@ -1031,11 +1027,7 @@ impl ComponentWidgetWrapper { render_root(show_action_panel, widget_id, children, content, context, is_loading.unwrap_or(false)) } - ComponentWidget::GridItem { children, id, title, subtitle } => { - let ComponentRenderContext::Grid { widget_id: grid_widget_id } = context else { - panic!("not supposed to be passed to grid item: {:?}", context) - }; - + ComponentWidget::GridItem { children, title, subtitle } => { let content: Element<_> = column(render_children(children, ComponentRenderContext::GridItem)) .height(130) // TODO dynamic height .into(); @@ -1055,7 +1047,7 @@ impl ComponentWidgetWrapper { .into(); let content: Element<_> = button(content) - .on_press(ComponentWidgetEvent::SelectGridItem { grid_widget_id, item_id: id.to_owned() }) + .on_press(ComponentWidgetEvent::GridItemClick { widget_id }) .themed(ButtonStyle::GridItem); content @@ -1521,13 +1513,11 @@ pub enum ComponentWidgetEvent { ToggleActionPanel { widget_id: UiWidgetId, }, - SelectListItem { - list_widget_id: UiWidgetId, - item_id: String, + ListItemClick { + widget_id: UiWidgetId, }, - SelectGridItem { - grid_widget_id: UiWidgetId, - item_id: String, + GridItemClick { + widget_id: UiWidgetId, }, PreviousView, } @@ -1639,11 +1629,11 @@ impl ComponentWidgetEvent { None } - ComponentWidgetEvent::SelectListItem { list_widget_id, item_id } => { - Some(create_list_on_selection_change_event(list_widget_id, item_id)) + ComponentWidgetEvent::ListItemClick { widget_id } => { + Some(create_list_item_on_click_event(widget_id)) } - ComponentWidgetEvent::SelectGridItem { grid_widget_id, item_id } => { - Some(create_grid_on_selection_change_event(grid_widget_id, item_id)) + ComponentWidgetEvent::GridItemClick { widget_id } => { + Some(create_grid_item_on_click_event(widget_id)) } ComponentWidgetEvent::PreviousView => { panic!("handle event on PreviousView event is not supposed to be called") @@ -1664,8 +1654,8 @@ impl ComponentWidgetEvent { ComponentWidgetEvent::OnChangeTextField { widget_id, .. } => widget_id, ComponentWidgetEvent::OnChangePasswordField { widget_id, .. } => widget_id, ComponentWidgetEvent::ToggleActionPanel { widget_id } => widget_id, - ComponentWidgetEvent::SelectListItem { list_widget_id, .. } => list_widget_id, - ComponentWidgetEvent::SelectGridItem { grid_widget_id, .. } => grid_widget_id, + ComponentWidgetEvent::ListItemClick { widget_id, .. } => widget_id, + ComponentWidgetEvent::GridItemClick { widget_id, .. } => widget_id, ComponentWidgetEvent::PreviousView => panic!("widget_id on PreviousView event is not supposed to be called"), }.to_owned() } diff --git a/rust/component_model/src/lib.rs b/rust/component_model/src/lib.rs index de3e8b8..223632a 100644 --- a/rust/component_model/src/lib.rs +++ b/rust/component_model/src/lib.rs @@ -925,11 +925,11 @@ pub fn create_component_model() -> Vec { mark_doc!("/list_item/description.md"), "ListItem", [ - property("id", mark_doc!("/list_item/props/id.md"),false, PropertyType::String), property("title", mark_doc!("/list_item/props/title.md"),false, PropertyType::String), property("subtitle", mark_doc!("/list_item/props/subtitle.md"),true, PropertyType::String), property("icon", mark_doc!("/list_item/props/icon.md"),true, PropertyType::Union { items: vec![PropertyType::ImageSource, PropertyType::Enum { name: "Icons".to_owned() }] }), // accessories + event("onClick", mark_doc!("/list_item/props/onClick.md"), true, []) ], children_none(), ); @@ -954,9 +954,6 @@ pub fn create_component_model() -> Vec { [ property("actions", mark_doc!("/list/props/actions.md"), true, component_ref(&action_panel_component)), property("isLoading", mark_doc!("/list/props/isLoading.md"), true, PropertyType::Boolean), - event("onSelectionChange", mark_doc!("/list/props/onSelectionChange.md"), true, [ - property("id", "".to_string(), false, PropertyType::String) - ]), ], children_members([ member("EmptyView", &empty_view_component), @@ -971,10 +968,10 @@ pub fn create_component_model() -> Vec { mark_doc!("/grid_item/description.md"), "GridItem", [ - property("id", mark_doc!("/grid_item/props/id.md"), false, PropertyType::String), property("title", mark_doc!("/grid_item/props/title.md"), false, PropertyType::String), property("subtitle", mark_doc!("/grid_item/props/subtitle.md"), true, PropertyType::String), // accessories + event("onClick", mark_doc!("/grid_item/props/onClick.md"), true, []) ], children_members([ member("Content", &content_component), @@ -1009,9 +1006,6 @@ pub fn create_component_model() -> Vec { property("columns", mark_doc!("/grid/props/columns.md"),true, PropertyType::Number), // TODO default // fit // inset - event("onSelectionChange", mark_doc!("/grid/props/onSelectionChange.md"), true, [ - property("id", "".to_string(), false, PropertyType::String) - ]), ], children_members([ member("EmptyView", &empty_view_component),