Skip to content

Commit a176c1f

Browse files
committed
refactor: replaced custom useManagedProp with builtin defineModel
1 parent 44ac386 commit a176c1f

File tree

32 files changed

+266
-419
lines changed

32 files changed

+266
-419
lines changed

apps/docs/src/stories/Components/Tooltip.story.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ onMounted(() => {
116116
}
117117
});
118118
119-
function checkTrigger(value: string, checked: boolean) {
119+
function checkTrigger(value: string, checked: boolean | null) {
120120
if (checked) {
121121
trigger.push(value);
122122
} else {

packages/core/src/components/Accordion/AccordionItem.vue

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
type="button"
66
:class="[
77
styles.accordionToggle, {
8-
[styles.modifiers.expanded]: managedExpanded,
8+
[styles.modifiers.expanded]: expanded,
99
},
1010
]"
11-
:aria-expanded="managedExpanded"
11+
:aria-expanded="expanded"
1212
@click="handleClick($event as PointerEvent)"
1313
>
1414
<span v-if="accordion?.togglePosition === 'start'" :class="styles.accordionToggleIcon">
@@ -29,10 +29,10 @@
2929
:class="[
3030
styles.accordionExpandableContent, {
3131
[styles.modifiers.fixed]: fixed,
32-
[styles.modifiers.expanded]: managedExpanded,
32+
[styles.modifiers.expanded]: expanded,
3333
},
3434
]"
35-
:hidden="!managedExpanded"
35+
:hidden="!expanded"
3636
>
3737
<div :class="styles.accordionExpandableContentBody">
3838
<slot />
@@ -44,7 +44,6 @@
4444
import { inject, type ButtonHTMLAttributes } from 'vue';
4545
import styles from '@patternfly/react-styles/css/components/Accordion/accordion';
4646
import AngleRightIcon from '@vue-patternfly/icons/angle-right-icon';
47-
import { useManagedProp } from '../../use';
4847
import { AccordionKey } from './Accordion.vue';
4948
import { useOUIAProps, type OUIAProps } from '../../helpers/ouia';
5049
@@ -58,16 +57,14 @@ export interface Props extends OUIAProps, /* @vue-ignore */ Omit<ButtonHTMLAttri
5857
toggleComponent?: string;
5958
contentComponent?: string;
6059
fixed?: boolean;
61-
expanded?: boolean;
6260
}
6361
64-
const props = withDefaults(defineProps<Props>(), {
65-
expanded: undefined,
66-
});
62+
const props = defineProps<Props>();
6763
const ouiaProps = useOUIAProps({id: props.ouiaId, safe: props.ouiaSafe});
6864
65+
const expanded = defineModel<boolean>('expanded', { default: false });
66+
6967
const emit = defineEmits<{
70-
(name: 'update:expanded', value: boolean): void;
7168
(name: 'click', event: PointerEvent): void;
7269
}>();
7370
@@ -77,10 +74,9 @@ defineSlots<{
7774
}>();
7875
7976
const accordion = inject(AccordionKey, undefined);
80-
const managedExpanded = useManagedProp('expanded', false);
8177
8278
function handleClick(event: PointerEvent) {
83-
managedExpanded.value = !managedExpanded.value;
79+
expanded.value = !expanded.value;
8480
emit('click', event);
8581
}
8682
</script>

packages/core/src/components/Alert/Alert.vue

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[styles.modifiers.inline]: inline,
1010
[styles.modifiers.plain]: plain,
1111
[styles.modifiers.expandable]: expandable,
12-
[styles.modifiers.expanded]: managedExpanded,
12+
[styles.modifiers.expanded]: expanded,
1313
}
1414
]"
1515
:aria-live="liveRegion ? 'polite' : undefined"
@@ -20,9 +20,9 @@
2020
<div v-if="expandable" :class="styles.alertToggle">
2121
<pf-button
2222
variant="plain"
23-
:aria-expanded="managedExpanded"
23+
:aria-expanded="expanded"
2424
:aria-label="toggleAriaLabel || `Toggle ${variantLabel} ${title}`"
25-
@click="managedExpanded = !managedExpanded"
25+
@click="expanded = !expanded"
2626
>
2727
<span :class="styles.alertToggleIcon">
2828
<pf-angle-right-icon aria-hidden />
@@ -79,7 +79,6 @@ import PfAngleRightIcon from '@vue-patternfly/icons/angle-right-icon';
7979
import { useOUIAProps, type OUIAProps } from '../../helpers/ouia';
8080
import { ref, watch, type HTMLAttributes, onBeforeUnmount, onMounted, computed, type Component, useTemplateRef, type ComponentPublicInstance } from 'vue';
8181
import { useElementSize } from '@vueuse/core';
82-
import { useManagedProp } from '../../use';
8382
import type { Placement } from '../../helpers/FloatingUi.vue';
8483
8584
defineOptions({
@@ -93,8 +92,6 @@ export interface Props extends OUIAProps, /* @vue-ignore */ Omit<HTMLAttributes,
9392
id?: string;
9493
/** Flag indicating that the alert is expandable. */
9594
expandable?: boolean;
96-
/** Flag indicating that the alert is expanded */
97-
expanded?: boolean;
9895
/** Show close button */
9996
onClose?: (e: Event) => void;
10097
/** Flag to indicate if the alert is inline. */
@@ -128,7 +125,6 @@ export interface Props extends OUIAProps, /* @vue-ignore */ Omit<HTMLAttributes,
128125
}
129126
130127
const props = withDefaults(defineProps<Props>(), {
131-
expanded: undefined,
132128
variant: 'custom',
133129
truncateTitle: 0,
134130
timeoutAnimation: 3000,
@@ -137,6 +133,9 @@ const props = withDefaults(defineProps<Props>(), {
137133
});
138134
const ouiaProps = useOUIAProps({id: props.ouiaId, safe: props.ouiaSafe, variant: props.variant});
139135
136+
/** Flag indicating that the alert is expanded */
137+
const expanded = defineModel<boolean>('expanded', { default: false });
138+
140139
const emit = defineEmits<{
141140
(name: 'mouseenter', e: Event): void;
142141
(name: 'mouseleave', e: Event): void;
@@ -160,7 +159,6 @@ const timedOutAnimation = ref(true);
160159
const containsFocus = ref(false);
161160
const el = useTemplateRef('elRef');
162161
163-
const managedExpanded = useManagedProp('expanded', false);
164162
const variantLabel = computed(() => `${props.variant.charAt(0).toUpperCase()}${props.variant.slice(1)} alert:`);
165163
const dismissed = computed(() => timedOut.value && timedOutAnimation.value && !isMouseOver.value && !containsFocus.value);
166164

packages/core/src/components/Card/Card.vue

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
class="pf-v5-screen-reader"
55
type="checkbox"
66
:name="name"
7-
:checked="managedSelected"
7+
:checked="selected"
88
:disabled="selectableDisabled"
99
tabindex="-1"
1010
@change="emit('change', $event)"
@@ -14,7 +14,7 @@
1414
v-bind="{...ouiaProps, ...$attrs}"
1515
:class="[styles.card, ...selectableModifiers, {
1616
[styles.modifiers.compact]: compact,
17-
[styles.modifiers.expanded]: managedExpanded,
17+
[styles.modifiers.expanded]: expanded,
1818
[styles.modifiers.flat]: flat,
1919
[styles.modifiers.rounded]: rounded,
2020
[styles.modifiers.displayLg]: large && !compact,
@@ -29,7 +29,7 @@
2929
</template>
3030

3131
<script lang="ts">
32-
export const CardExpandedKey = Symbol('CardExpandedKey') as InjectionKey<Ref<boolean>>;
32+
export const CardExpandedKey = Symbol('CardExpandedKey') as InjectionKey<Ref<boolean | undefined>>;
3333
export const CardExpandableKey = Symbol('CardExpandableKey') as InjectionKey<ComputedRef<boolean>>;
3434
3535
export interface Props extends OUIAProps, /* @vue-ignore */ Omit<HTMLAttributes, 'tabindex'> {
@@ -54,9 +54,6 @@ export interface Props extends OUIAProps, /* @vue-ignore */ Omit<HTMLAttributes,
5454
/** Name of the optional hidden input that tracks the selected status */
5555
name?: string;
5656
57-
/** Modifies the card to include selected styling */
58-
selected?: boolean,
59-
6057
/** Modifies the card to include flat styling */
6158
flat?: boolean;
6259
@@ -74,16 +71,12 @@ export interface Props extends OUIAProps, /* @vue-ignore */ Omit<HTMLAttributes,
7471
7572
/** Modifies the card to be expandable */
7673
expandable?: boolean;
77-
78-
/** Flag indicating if a card is expanded. Modifies the card to be expandable. */
79-
expanded?: boolean;
8074
}
8175
</script>
8276

8377
<script lang="ts" setup>
8478
import styles from '@patternfly/react-styles/css/components/Card/card';
85-
import { provide, computed, type Component, type InjectionKey, type Ref, type ComputedRef, type HTMLAttributes } from 'vue';
86-
import { useManagedProp } from '../../use';
79+
import { provide, computed, type Component, type InjectionKey, type ComputedRef, type HTMLAttributes, type Ref } from 'vue';
8780
import { isDefined } from '@vueuse/shared';
8881
import { useOUIAProps, type OUIAProps } from '../../helpers/ouia';
8982
@@ -99,11 +92,15 @@ const props = withDefaults(defineProps<Props>(), {
9992
});
10093
const ouiaProps = useOUIAProps({id: props.ouiaId, safe: props.ouiaSafe});
10194
95+
/** Flag indicating if a card is expanded. Modifies the card to be expandable. */
96+
const expanded = defineModel<boolean>('expanded');
97+
98+
/** Modifies the card to include selected styling */
99+
const selected = defineModel<boolean>('selected', { default: false });
100+
102101
const emit = defineEmits<{
103102
(name: 'click', e: PointerEvent): void;
104103
(name: 'change', e: Event): void;
105-
(name: 'update:expanded', value: boolean): void;
106-
(name: 'update:selected', value: boolean): void;
107104
}>();
108105
109106
defineSlots<{
@@ -112,26 +109,24 @@ defineSlots<{
112109
badge?: (props?: Record<never, never>) => any;
113110
}>();
114111
115-
const managedExpanded = useManagedProp('expanded', false);
116-
const managedSelected = useManagedProp('selected', false);
117-
provide(CardExpandedKey, managedExpanded);
118-
provide(CardExpandableKey, computed(() => props.expandable || isDefined(props.expanded)));
112+
provide(CardExpandedKey, expanded);
113+
provide(CardExpandableKey, computed(() => props.expandable || isDefined(expanded.value)));
119114
120115
const selectableModifiers = computed(() => {
121116
if (props.selectableDisabled) {
122117
return [styles.modifiers.nonSelectableRaised];
123118
}
124119
if (props.selectableRaised) {
125-
return [styles.modifiers.selectableRaised, managedSelected.value && styles.modifiers.selectedRaised];
120+
return [styles.modifiers.selectableRaised, selected.value && styles.modifiers.selectedRaised];
126121
}
127122
if (props.selectable) {
128-
return [styles.modifiers.selectable, managedSelected.value && styles.modifiers.selected];
123+
return [styles.modifiers.selectable, selected.value && styles.modifiers.selected];
129124
}
130125
return [];
131126
});
132127
133128
function onClick(e: PointerEvent) {
134-
managedSelected.value = !managedSelected.value;
129+
selected.value = !selected.value;
135130
emit('click', e);
136131
}
137132
</script>

packages/core/src/components/Checkbox.vue

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ defineOptions({
6060
6161
export interface Props extends OUIAProps, /* @vue-ignore */ Omit<InputHTMLAttributes, 'onChange' | 'type' | 'checked'> {
6262
component?: string | Component;
63-
/** Flag to show if the checkbox is checked. */
64-
modelValue?: boolean | null;
6563
/** Flag to show if the checkbox is disabled. */
6664
disabled?: boolean;
6765
/** Flag to show if the checkbox is required. */
@@ -85,9 +83,11 @@ const props = withDefaults(defineProps<Props>(), {
8583
});
8684
const ouiaProps = useOUIAProps({id: props.ouiaId, safe: props.ouiaSafe});
8785
86+
/** Flag to show if the checkbox is checked. */
87+
const value = defineModel<boolean | null>({ default: false });
88+
8889
const emit = defineEmits<{
8990
(name: 'change', e: Event): void;
90-
(name: 'update:modelValue', value: boolean): void;
9191
}>();
9292
9393
defineSlots<{
@@ -99,20 +99,19 @@ defineSlots<{
9999
useChildrenTracker(FormInputsKey, getCurrentInstance()?.proxy);
100100
const input = useTemplateRef('inputRef');
101101
const wrapWithLabel = computed(() => (props.labelWrapped && !props.component) || props.component === 'label');
102-
const validId = computed(() => props.id || getUniqueId());
103102
104-
watch(() => props.modelValue, () => {
103+
watch(value, () => {
105104
if (!input.value) {
106105
return;
107106
}
108-
input.value.indeterminate = props.modelValue === null;
107+
input.value.indeterminate = value.value === null;
109108
}, {
110109
immediate: true,
111110
});
112111
113112
function onChange(e: Event) {
114113
emit('change', e);
115-
emit('update:modelValue', (e.currentTarget as HTMLInputElement).checked);
114+
value.value = (e.currentTarget as HTMLInputElement).checked;
116115
}
117116
118117
defineExpose({

packages/core/src/components/DataList/DataList.vue

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<ul
3-
v-bind="(ouiaProps as any)"
3+
v-bind="ouiaProps"
44
:class="[
55
styles.dataList,
66
gridBreackpointClass, {
@@ -16,20 +16,18 @@
1616

1717
<script lang="ts">
1818
export const DataListKey = Symbol('DataListSelectableKey') as InjectionKey<{
19-
emit: (event: 'update:selected', ...args: any[]) => void;
19+
emit: (event: 'update:selected', item: string | number | symbol | (string | number | symbol)[] | undefined) => void;
2020
selectable: Ref<boolean>,
2121
expandable: ComputedRef<boolean | undefined>,
2222
inputName: ComputedRef<string | undefined>,
2323
inputValue: ComputedRef<string | undefined>,
24-
itemSelection: WritableComputedRef<any>,
24+
itemSelection: Ref<string | number | symbol | (string | number | symbol)[] | undefined>,
2525
multipleSelection: ComputedRef<boolean>,
2626
}>;
2727
2828
export type DataListWrapModifier = 'nowrap' | 'truncate' | 'breakWord';
2929
3030
export interface Props extends OUIAProps, /* @vue-ignore */ HTMLAttributes {
31-
/** Array for multiple selection, single value for single selection, undefined to disable selection */
32-
selected?: string | number | symbol | (string | number | symbol)[];
3331
/** Name of the item input (radio or checkbox) when item selection is enabled */
3432
selectionInputName?: string;
3533
/** Defines the value for the input (radio or checkbox) */
@@ -53,8 +51,7 @@ export interface Props extends OUIAProps, /* @vue-ignore */ HTMLAttributes {
5351
import styles from '@patternfly/react-styles/css/components/DataList/data-list';
5452
import stylesGrid from '@patternfly/react-styles/css/components/DataList/data-list-grid';
5553
import { useOUIAProps, type OUIAProps } from '../../helpers/ouia';
56-
import { computed, type ComputedRef, type InjectionKey, provide, type Ref, ref, type WritableComputedRef, type HTMLAttributes } from "vue";
57-
import { useManagedProp } from '../../use';
54+
import { computed, type ComputedRef, type InjectionKey, provide, type Ref, ref, type HTMLAttributes } from "vue";
5855
5956
const gridBreakpointClasses = {
6057
none: stylesGrid.modifiers.gridNone,
@@ -72,30 +69,28 @@ defineOptions({
7269
7370
const props = withDefaults(defineProps<Props>(), {
7471
gridBreakpoint: 'md',
75-
selected: undefined,
7672
expandable: undefined,
7773
});
7874
const ouiaProps = useOUIAProps({id: props.ouiaId, safe: props.ouiaSafe});
7975
80-
const emit = defineEmits<{
81-
(name: 'update:selected', s: any): void;
82-
}>();
76+
const emit = defineEmits();
8377
8478
defineSlots<{
8579
default?: (props?: Record<never, never>) => any;
8680
}>();
8781
88-
const managedSelected = useManagedProp('selected', null);
82+
/** Array for multiple selection, single value for single selection, undefined to disable selection */
83+
const selected = defineModel<string | number | symbol | (string | number | symbol)[]>('selected');
8984
90-
const selectable = computed(() => !!props['onUpdate:selected'] || props.selected !== undefined || !!props.selectionInputName);
85+
const selectable = computed(() => !!props['onUpdate:selected'] || selected.value !== undefined || !!props.selectionInputName);
9186
9287
provide(DataListKey, {
9388
emit,
9489
selectable,
9590
expandable: computed(() => props.expandable),
9691
inputName: computed(() => props.selectionInputName),
9792
inputValue: computed(() => props.selectionInputValue),
98-
itemSelection: managedSelected,
93+
itemSelection: selected,
9994
multipleSelection: computed(() => props.selectionMultiple),
10095
});
10196

0 commit comments

Comments
 (0)