Skip to content

Commit

Permalink
refactor: update useProviderParent and useProviderChild composable
Browse files Browse the repository at this point in the history
  • Loading branch information
mlmoravek committed Nov 26, 2024
1 parent 383c433 commit 5a8089f
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 59 deletions.
7 changes: 5 additions & 2 deletions packages/oruga/src/components/carousel/Carousel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function restartTimer(): void {
startTimer();
}
// provided data is a computed ref to enjure reactivity
const provideData = computed<CarouselComponent>(() => ({
restartTimer,
itemWidth: itemWidth.value,
Expand All @@ -96,8 +97,8 @@ const provideData = computed<CarouselComponent>(() => ({
setActive: (index: number): void => switchTo(index),
}));
/** Provide functionalities and data to child item components */
const { childItems } = useProviderParent(rootRef, { data: provideData });
/** provide functionalities and data to child item components */
const { childItems } = useProviderParent({ rootRef, data: provideData });
const activeIndex = defineModel<number>({ default: 0 });
const scrollIndex = ref(props.modelValue);
Expand Down Expand Up @@ -471,6 +472,7 @@ function indicatorItemAppliedClasses(index: number): ClassBind[] {
-->
<slot />
</div>

<!--
@slot Override the arrows
@binding {boolean} has-prev has prev arrow button
Expand Down Expand Up @@ -545,6 +547,7 @@ function indicatorItemAppliedClasses(index: number): ClassBind[] {
</div>
</template>
</slot>

<template v-if="overlay">
<!-- @slot Overlay element -->
<slot name="overlay" />
Expand Down
4 changes: 2 additions & 2 deletions packages/oruga/src/components/carousel/CarouselItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const props = withDefaults(defineProps<CarouselItemProps>(), {
ariaRole: () => getDefault("carousel.ariaRole", "option"),
});
// Inject functionalities and data from the parent carousel component
/** inject functionalities and data from the parent component */
const { parent, item } = useProviderChild<CarouselComponent>();
const isActive = computed(() => parent.value.activeIndex === item.value.index);
Expand Down Expand Up @@ -54,8 +54,8 @@ const itemClasses = defineClasses(
v-if="parent"
:class="itemClasses"
:style="itemStyle"
:data-id="`carousel-${item.identifier}`"
data-oruga="carousel-item"
:data-id="`carousel-${item.identifier}`"
:role="ariaRole"
aria-roledescription="item"
:aria-selected="isActive"
Expand Down
6 changes: 3 additions & 3 deletions packages/oruga/src/components/dropdown/Dropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,16 @@ function selectItem(value: T): void {
}
}
// Provided data is a computed ref to enjure reactivity.
// provided data is a computed ref to enjure reactivity
const provideData = computed<DropdownComponent<T>>(() => ({
disabled: props.disabled,
multiple: isTrueish(props.multiple),
selected: vmodel.value,
selectItem,
}));
/** Provide functionalities and data to child item components */
useProviderParent(contentRef, { data: provideData });
/** provide functionalities and data to child item components */
useProviderParent({ data: provideData });
// --- Computed Component Classes ---
Expand Down
10 changes: 6 additions & 4 deletions packages/oruga/src/components/dropdown/DropdownItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const emits = defineEmits<{
click: [value: T, event: Event];
}>();
// Inject functionalities and data from the parent component
const { parent } = useProviderChild<DropdownComponent<T>>();
/** inject functionalities and data from the parent component */
const { parent, item } = useProviderChild<DropdownComponent<T>>();
const isClickable = computed(
() => !parent.value.disabled && !props.disabled && props.clickable,
Expand Down Expand Up @@ -81,12 +81,14 @@ const rootClasses = defineClasses(
<component
:is="tag"
:class="rootClasses"
data-oruga="dropdown-item"
:data-id="`dropdown-${item.identifier}`"
:role="ariaRole"
:tabindex="tabindex"
data-oruga="dropdown-item"
:aria-selected="isActive"
:aria-disabled="disabled"
@click="selectItem">
@click="selectItem"
@keypress.enter="selectItem">
<!--
@slot Override the label, default is label prop
-->
Expand Down
3 changes: 2 additions & 1 deletion packages/oruga/src/components/menu/Menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ const provideData = computed<MenuComponent>(() => ({
}));
/** provide functionalities and data to child item components */
const { childItems } = useProviderParent<MenuItemComponent>(rootRef, {
const { childItems } = useProviderParent<MenuItemComponent>({
rootRef,
data: provideData,
});
Expand Down
8 changes: 4 additions & 4 deletions packages/oruga/src/components/menu/MenuItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ const providedData = computed<MenuItemComponent>(() => ({
reset,
}));
// inject functionalities and data from the parent menu component
/** inject functionalities and data from the parent menu component */
const { parent, item } = useProviderChild<MenuComponent, MenuItemComponent>({
data: providedData,
});
// inject functionalities and data from the parent menu-item component
/** inject functionalities and data from the parent menu-item component */
const providedItem = useProviderChild<MenuItemProvider>({
key: "menu-item",
needParent: false,
Expand Down Expand Up @@ -114,7 +114,7 @@ const provideData = computed<MenuItemProvider>(() => ({
}));
/** provide functionalities and data to child item components */
useProviderParent(rootRef, { key: "menu-item", data: provideData });
useProviderParent({ rootRef, key: "menu-item", data: provideData });
// --- Computed Component Classes ---
Expand Down Expand Up @@ -148,8 +148,8 @@ const submenuClasses = defineClasses([
ref="rootElement"
:role="ariaRole"
:class="itemClasses"
:data-id="identifier"
data-oruga="menu-item"
:data-id="identifier"
aria-roledescription="item">
<component
:is="tag"
Expand Down
6 changes: 3 additions & 3 deletions packages/oruga/src/components/slider/Slider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ const sliderRef = useTemplateRef("sliderElement");
const thumbStartRef = useTemplateRef("thumbStartComponent");
const thumbEndRef = useTemplateRef("thumbEndComponent");
// Provided data is a computed ref to enjure reactivity.
// provided data is a computed ref to enjure reactivity
const provideData = computed<SliderComponent>(() => ({
max: props.max,
min: props.min,
}));
/** Provide functionalities and data to child item components */
useProviderParent(undefined, { data: provideData });
/** provide functionalities and data to child item components */
useProviderParent({ data: provideData });
const valueStart = ref<number>(0);
const valueEnd = ref<number>(0);
Expand Down
4 changes: 2 additions & 2 deletions packages/oruga/src/components/slider/SliderTick.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ const props = defineProps({
},
});
// Inject functionalities and data from the parent carousel component
const { parent } = useProviderChild<SliderComponent>();
/** inject functionalities and data from the parent component */
const { parent } = useProviderChild<SliderComponent>({ register: false });
const position = computed(() => {
const pos =
Expand Down
5 changes: 3 additions & 2 deletions packages/oruga/src/components/steps/StepItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const itemValue = props.value || useId();
const slots = useSlots();
// provided data is a computed ref to enjure reactivity
const providedData = computed<StepItemComponent<T>>(() => ({
...props,
value: itemValue,
Expand All @@ -60,7 +61,7 @@ const providedData = computed<StepItemComponent<T>>(() => ({
deactivate,
}));
// inject functionalities and data from the parent carousel component
/** inject functionalities and data from the parent component */
const { parent, item } = useProviderChild<StepsComponent>({
data: providedData,
});
Expand Down Expand Up @@ -180,8 +181,8 @@ const panelClasses = defineClasses(["stepPanelClass", "o-steps__panel"]);
v-bind="$attrs"
:id="`tabpanel-${item.identifier}`"
:class="panelClasses"
:data-id="`steps-${item.identifier}`"
data-oruga="steps-item"
:data-id="`steps-${item.identifier}`"
:aria-labelledby="`tab-${item.identifier}`"
aria-roledescription="item">
<template v-if="!parent.destroyOnHide || (isActive && visible)">
Expand Down
8 changes: 4 additions & 4 deletions packages/oruga/src/components/steps/Steps.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const rootRef = useTemplateRef("rootElement");
/** The selected item value, use v-model to make it two-way binding */
const vmodel = defineModel<ModelValue>({ default: undefined });
// Provided data is a computed ref to enjure reactivity.
// provided data is a computed ref to enjure reactivity
const provideData = computed<StepsComponent>(() => ({
activeIndex: activeItem.value?.index || 0,
labelPosition: props.labelPosition,
Expand All @@ -101,10 +101,10 @@ const provideData = computed<StepsComponent>(() => ({
variant: props.variant,
}));
/** Provide functionalities and data to child item components */
const { childItems } = useProviderParent<StepItemComponent<T>>(rootRef, {
/** provide functionalities and data to child item components */
const { childItems } = useProviderParent<StepItemComponent<T>>({
rootRef,
data: provideData,
sorted: true,
});
const items = computed<StepItem<T>[]>(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/oruga/src/components/table/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,9 @@ const provideData = computed<TableComponent>(() => ({
}));
/** provide functionalities and data to child item components */
const { childItems } = useProviderParent<TableColumnComponent<T>>(slotRef, {
const { childItems } = useProviderParent<TableColumnComponent<T>>({
rootRef: slotRef,
data: provideData,
sorted: true,
});
/** all defined columns */
Expand Down
4 changes: 3 additions & 1 deletion packages/oruga/src/components/table/TableColumn.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const isHeaderUnselectable = computed(
const vm = getCurrentInstance();
// provided data is a computed ref to enjure reactivity
const providedData = computed<TableColumnComponent<T>>(() => ({
...props,
$el: vm!.proxy!,
Expand All @@ -54,6 +55,7 @@ const providedData = computed<TableColumnComponent<T>>(() => ({
tdClasses: tdClasses.value,
}));
/** inject functionalities and data from the parent component */
const { parent, item } = useProviderChild<TableComponent>({
data: providedData,
});
Expand Down Expand Up @@ -120,7 +122,7 @@ const filters = {} as Record<string, string>;
</script>

<template>
<span :data-id="item.identifier" data-oruga="table-column">
<span data-oruga="table-column" :data-id="item.identifier">
{{ label }}

<!--
Expand Down
5 changes: 3 additions & 2 deletions packages/oruga/src/components/tabs/TabItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const itemValue = props.value || useId();
const slots = useSlots();
// provided data is a computed ref to enjure reactivity
const providedData = computed<TabItemComponent<T>>(() => ({
...props,
value: itemValue,
Expand All @@ -57,7 +58,7 @@ const providedData = computed<TabItemComponent<T>>(() => ({
deactivate,
}));
// Inject functionalities and data from the parent component
/** inject functionalities and data from the parent component */
const { parent, item } = useProviderChild<TabsComponent>({
data: providedData,
});
Expand Down Expand Up @@ -164,8 +165,8 @@ const panelClasses = defineClasses(["tabPanelClass", "o-tabs__panel"]);
v-bind="$attrs"
:id="`tabpanel-${item.identifier}`"
:class="panelClasses"
:data-id="`tabs-${item.identifier}`"
data-oruga="tabs-item"
:data-id="`tabs-${item.identifier}`"
:aria-labelledby="`tab-${item.identifier}`"
aria-roledescription="item">
<template v-if="!parent.destroyOnHide || (isActive && visible)">
Expand Down
8 changes: 4 additions & 4 deletions packages/oruga/src/components/tabs/Tabs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const rootRef = useTemplateRef("rootElement");
/** The selected item value, use v-model to make it two-way binding */
const vmodel = defineModel<ModelValue>({ default: undefined });
// Provided data is a computed ref to enjure reactivity.
// provided data is a computed ref to enjure reactivity
const provideData = computed<TabsComponent>(() => ({
activeIndex: activeItem.value?.index || 0,
type: props.type,
Expand All @@ -92,10 +92,10 @@ const provideData = computed<TabsComponent>(() => ({
destroyOnHide: props.destroyOnHide,
}));
/** Provide functionalities and data to child item components */
const { childItems } = useProviderParent<TabItemComponent<T>>(rootRef, {
/** provide functionalities and data to child item components */
const { childItems } = useProviderParent<TabItemComponent<T>>({
rootRef,
data: provideData,
sorted: true,
});
const items = computed<TabItem<T>[]>(() => {
Expand Down
Loading

0 comments on commit 5a8089f

Please sign in to comment.