Skip to content

Commit

Permalink
fix: renderItems
Browse files Browse the repository at this point in the history
  • Loading branch information
Col0ring committed Jan 20, 2025
1 parent 1bf3cdf commit 3000de2
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 152 deletions.
4 changes: 0 additions & 4 deletions frontend/antd/anchor/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import cls from 'classnames';
import { writable } from 'svelte/store';
import { getItems } from './context';
const AwaitedAnchor = importComponent(() => import('./anchor'));
export let gradio: Gradio;
Expand Down Expand Up @@ -53,7 +51,6 @@
as_item,
restProps: $$restProps,
});
const { items, default: children } = getItems(['items', 'default']);
</script>

{#if $mergedProps.visible}
Expand All @@ -66,7 +63,6 @@
{...$mergedProps.props}
{...bindEvents($mergedProps)}
slots={$slots}
slotItems={$items.length > 0 ? $items : $children}
>
<slot></slot>
</Anchor>
Expand Down
73 changes: 34 additions & 39 deletions frontend/antd/anchor/anchor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,42 @@ import { useFunction } from '@utils/hooks/useFunction';
import { renderItems } from '@utils/renderItems';
import { Anchor as AAnchor, type GetProps } from 'antd';

import { type Item } from './context';
import { useItems, withItemsContextProvider } from './context';

type AnchorProps = GetProps<typeof AAnchor>;
export const Anchor = sveltify<
AnchorProps & {
slotItems: Item[];
}
>(
({
getContainer,
getCurrentAnchor,
children,
items,
slotItems,
...props
}) => {
const getContainerFunction = useFunction(getContainer);
const getCurrentAnchorFunction = useFunction(getCurrentAnchor);
return (
<>
{children}
<AAnchor
{...props}
items={useMemo(() => {
// ["title"]
return (
items ||
renderItems<NonNullable<AnchorProps['items']>[number]>(
slotItems,
{
clone: true,
}
)
);
}, [items, slotItems])}
getContainer={getContainerFunction}
getCurrentAnchor={getCurrentAnchorFunction}
/>
</>
);
}
export const Anchor = sveltify<AnchorProps>(
withItemsContextProvider(
['items', 'default'],
({ getContainer, getCurrentAnchor, children, items, ...props }) => {
const getContainerFunction = useFunction(getContainer);
const getCurrentAnchorFunction = useFunction(getCurrentAnchor);
const { items: slotItems } = useItems<['items', 'default']>();
const resolvedSlotItems =
slotItems.items.length > 0 ? slotItems.items : slotItems.default;
return (
<>
<div style={{ display: 'none' }}>{children}</div>
<AAnchor
{...props}
items={useMemo(() => {
// ["title"]
return (
items ||
renderItems<NonNullable<AnchorProps['items']>[number]>(
resolvedSlotItems,
{
clone: true,
}
)
);
}, [items, resolvedSlotItems])}
getContainer={getContainerFunction}
getCurrentAnchor={getCurrentAnchorFunction}
/>
</>
);
}
)
);

export default Anchor;
8 changes: 4 additions & 4 deletions frontend/antd/anchor/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createItemsContext } from '@utils/createItemsContext';
import { createItemsContext } from '@utils/createItemsContext2';

const { getItems, getSetItemFn } = createItemsContext('anchor');
export const { useItems, withItemsContextProvider, ItemHandler } =
createItemsContext();

export { getItems, getSetItemFn };
export * from '@utils/createItemsContext';
export * from '@utils/createItemsContext2';
43 changes: 20 additions & 23 deletions frontend/antd/anchor/item/Index.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<svelte:options accessors={true} />

<script lang="ts">
import { bindEvents } from '@svelte-preprocess-react/component';
import {
bindEvents,
importComponent,
} from '@svelte-preprocess-react/component';
import {
getSlotContext,
getSlotKey,
Expand All @@ -12,7 +15,7 @@
import cls from 'classnames';
import { writable } from 'svelte/store';
import { getItems, getSetItemFn } from '../context';
const AwaitedAnchorItem = importComponent(() => import('./anchor.item'));
export let gradio: Gradio;
export let props: Record<string, any> = {};
Expand Down Expand Up @@ -59,28 +62,22 @@
as_item,
restProps: $$restProps,
});
const setItem = getSetItemFn();
const { default: items } = getItems();
$: setItem($slotKey, $mergedProps._internal.index || 0, {
props: {
style: $mergedProps.elem_style,
className: cls($mergedProps.elem_classes, 'ms-gr-antd-anchor-item'),
id: $mergedProps.elem_id,
...$mergedProps.restProps,
...$mergedProps.props,
...bindEvents($mergedProps),
},
slots: $slots,
children: $items.length > 0 ? $items : undefined,
});
</script>

{#if $mergedProps.visible}
<slot></slot>
{#await AwaitedAnchorItem then AnchorItem}
<AnchorItem
style={$mergedProps.elem_style}
className={cls($mergedProps.elem_classes, 'ms-gr-antd-anchor-item')}
id={$mergedProps.elem_id}
{...$mergedProps.restProps}
{...$mergedProps.props}
{...bindEvents($mergedProps)}
slots={$slots}
itemIndex={$mergedProps._internal.index || 0}
itemSlotKey={$slotKey}
>
<slot></slot>
</AnchorItem>
{/await}
{/if}

<style>
:global(.ms-gr-antd-noop-class) {
display: none;
}
</style>
23 changes: 23 additions & 0 deletions frontend/antd/anchor/item/anchor.item.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { sveltify } from '@svelte-preprocess-react';
import React from 'react';
import type { AnchorLinkItemProps } from 'antd/es/anchor/Anchor';

import { ItemHandler, type ItemHandlerProps } from '../context';

export const AnchorItem = sveltify<AnchorLinkItemProps & ItemHandlerProps>(
(props) => {
return (
<>
<ItemHandler<['default']>
{...props}
allowedSlots={['default']}
itemChildren={(items) => {
return items.default.length > 0 ? items.default : undefined;
}}
/>
</>
);
}
);

export default AnchorItem;
9 changes: 5 additions & 4 deletions frontend/antd/grid/col/Index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@
itemSlotKey={$slotKey}
itemElement={$slot}
slots={{}}
/>
>
<svelte-slot bind:this={$slot}>
<slot></slot>
</svelte-slot>
</Col>
{/await}
<svelte-slot bind:this={$slot}>
<slot></slot>
</svelte-slot>
{/if}

<style>
Expand Down
35 changes: 7 additions & 28 deletions frontend/antd/grid/col/col.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,13 @@
import { sveltify } from '@svelte-preprocess-react';
import React, { useEffect, useRef } from 'react';
import React from 'react';
import { type Col as ACol, type GetProps } from 'antd';
import { isEqual } from 'lodash-es';

import { type Item, useItems } from '../context';
import { ItemHandler, type ItemHandlerProps } from '../context';

export const Col = sveltify<
GetProps<typeof ACol> & {
itemIndex: number;
itemSlotKey?: string;
itemElement?: HTMLElement;
},
['children']
>(({ itemIndex, itemSlotKey, itemElement, slots, ...props }) => {
const prevValueRef = useRef<Item>();
const { setItem } = useItems();
useEffect(() => {
if (itemElement) {
const value: Item = {
el: itemElement,
props,
slots,
};
if (!isEqual(prevValueRef.current, value)) {
prevValueRef.current = value;
setItem(itemSlotKey, itemIndex, value);
}
}
}, [itemIndex, itemSlotKey, itemElement, props, setItem, slots]);
return <span />;
});
export const Col = sveltify<GetProps<typeof ACol> & ItemHandlerProps>(
(props) => {
return <ItemHandler {...props} />;
}
);

export default Col;
3 changes: 2 additions & 1 deletion frontend/antd/grid/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createItemsContext } from '@utils/createItemsContext2';

export const { withItemsContext, useItems } = createItemsContext();
export const { withItemsContextProvider, useItems, ItemHandler } =
createItemsContext();

export * from '@utils/createItemsContext2';
4 changes: 2 additions & 2 deletions frontend/antd/grid/row/row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { sveltify } from '@svelte-preprocess-react';
import { ReactSlot } from '@svelte-preprocess-react/react-slot';
import { Col as ACol, type GetProps, Row as ARow } from 'antd';

import { useItems, withItemsContext } from '../context';
import { useItems, withItemsContextProvider } from '../context';

export const Row = sveltify<GetProps<typeof ARow>>(
withItemsContext(['default'], ({ children, ...props }) => {
withItemsContextProvider(['default'], ({ children, ...props }) => {
const {
items: { default: cols },
} = useItems<['default']>();
Expand Down
Loading

0 comments on commit 3000de2

Please sign in to comment.