Skip to content

Commit

Permalink
fix: simplify use-stateful (#4477)
Browse files Browse the repository at this point in the history
  • Loading branch information
m0ksem authored Feb 13, 2025
1 parent 53ffd2c commit 469fa61
Show file tree
Hide file tree
Showing 32 changed files with 38 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<VaDropdown
:model-value="true"
:auto-placement="false"
:stateful="false"
trigger="none"
placement="top"
teleport=".demo-viewport"
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-accordion/VaAccordion.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const props = defineProps({
const emit = defineEmits([...useStatefulEmits])
const { valueComputed } = useStateful(props, emit, 'modelValue')
const valueComputed = useStateful(props, emit, 'modelValue')
const { items } = useAccordion(props, valueComputed)
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-alert/VaAlert.vue
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const {
borderStyle,
} = useAlertStyles(props)
const { valueComputed } = useStateful(props, emit)
const valueComputed = useStateful(props, emit)
const hide = () => { valueComputed.value = false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ const props = defineProps({
const emit = defineEmits(['update:modelValue', ...createEmits(), ...createMainButtonEmits()])
const { valueComputed } = useStateful(props, emit)
const valueComputed = useStateful(props, emit)
const computedIcon = computed(() => valueComputed.value ? props.openedIcon : props.icon)
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-carousel/VaCarousel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ const props = defineProps({
const emit = defineEmits([...useStatefulEmits])
const { valueComputed: currentSlide } = useStateful(props, emit, 'modelValue')
const currentSlide = useStateful(props, emit, 'modelValue')
const autoscrollIntervalComputed = useNumericProp('autoscrollInterval')
const autoscrollPauseDurationComputed = useNumericProp('autoscrollPauseDuration')
const ratioComputed = useNumericProp('ratio')
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-chip/VaChip.vue
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const shadowStyle = computed(() => {
return `0 0.125rem 0.19rem 0 ${getBoxShadowColor(colorComputed.value)}`
})
const { valueComputed } = useStateful(props, emit)
const valueComputed = useStateful(props, emit)
const { tagComputed, hrefComputed } = useRouterLink(props)
const close = () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-collapse/VaCollapse.vue
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const emit = defineEmits(['update:modelValue', ...useSelectableEmits])
const body = shallowRef<HTMLElement>()
const { valueComputed } = useStateful(props, emit, 'modelValue')
const valueComputed = useStateful(props, emit, 'modelValue')
const { getColor, getTextColor, setHSLAColor } = useColors()
const { accordionProps, accordionItemValue } = useAccordionItem()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const props = defineProps({
const emit = defineEmits([...useStatefulEmits])
const { valueComputed } = useStateful(props, emit)
const valueComputed = useStateful(props, emit)
const { getColor } = useColors()
const hasKeyboardFocus = useElementFocusedKeyboard(useCurrentElement())
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-color-input/VaColorInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const emit = defineEmits([...useStatefulEmits])
const colorPicker = shallowRef<HTMLInputElement>()
const { valueComputed } = useStateful(props, emit)
const valueComputed = useStateful(props, emit)
const callPickerDialog = () => !props.disabled && colorPicker.value?.click()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const props = defineProps({
const emit = defineEmits([...useStatefulEmits])
const { valueComputed } = useStateful(props, emit)
const valueComputed = useStateful(props, emit)
const isSelected = (color: string) => valueComputed.value === color
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-counter/VaCounter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ const {
blur,
} = useFocusableControl(input, props, emit)
const { valueComputed: statefulValue } = useStateful(props, emit)
const statefulValue = useStateful(props, emit)
function floatify (num: number | string) {
return parseFloat(Number(num).toFixed(10))
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-date-input/VaDateInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ watch([datePicker], () => {
trapFocus()
})
const { valueComputed: statefulValue } = useStateful(props, emit)
const statefulValue = useStateful(props, emit)
const { isOpenSync, dropdownProps } = useDropdownableControl(props, emit, {
defaultCloseOnValueUpdate: computed(() => {
if (Array.isArray(valueComputed.value)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-date-picker/VaDatePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const emit = defineEmits([
const currentPicker = ref<typeof VaDayPicker | typeof VaMonthPicker | typeof VaYearPicker>()
const { valueComputed } = useStateful(props, emit)
const valueComputed = useStateful(props, emit)
const { syncView } = useView(props, emit, { type: props.type })
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-dropdown/VaDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default defineComponent({
emits: [...useStatefulEmits, 'anchor-click', 'anchor-right-click', 'content-click', 'click-outside', 'focus-outside', 'close', 'open', 'anchor-dblclick'],
setup (props, { emit }) {
const { valueComputed } = useStateful(props, emit, 'modelValue')
const valueComputed = useStateful(props, emit, 'modelValue')
watch(valueComputed, (isOpened) => {
if (isOpened) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-form-field/VaFormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const props = withDefaults(defineProps<Props>(), {
const emit = defineEmits<ValidationEmits & StatefulEmits<T>>()
const { valueComputed } = useStateful(props, emit, 'modelValue')
const valueComputed = useStateful(props, emit, 'modelValue')
const reset = () => {
valueComputed.value = (props.clearValue as any)
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-hover/VaHover.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const props = defineProps({
const emit = defineEmits([...useStatefulEmits])
const { valueComputed } = useStateful(props, emit)
const valueComputed = useStateful(props, emit)
const onMouseEnter = () => {
if (!props.disabled) { valueComputed.value = true }
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-input/VaInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ useDeprecatedCondition([
const input = shallowRef<HTMLInputElement>()
const { valueComputed } = useStateful(props, emit, 'modelValue')
const valueComputed = useStateful(props, emit, 'modelValue')
const reset = () => withoutValidation(() => {
valueComputed.value = props.clearValue
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-modal/VaModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ const {
const { getColor } = useColors()
const textColorComputed = useElementTextColor(toRef(props, 'backgroundColor'))
const { valueComputed } = useStateful(props, emit)
const valueComputed = useStateful(props, emit)
const computedClass = computed(() => ({
'va-modal--fullscreen': props.fullscreen,
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-option-list/VaOptionList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const props = defineProps({
const emit = defineEmits([...useStatefulEmits, ...useValidationEmits, 'clear'])
const { valueComputed } = useStateful(props, emit, 'modelValue', { defaultValue: props.defaultValue })
const valueComputed = useStateful(props, emit, 'modelValue', { defaultValue: props.defaultValue })
const { getValue, getText, getTrackBy, getDisabled } = useSelectableList(props)
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-pagination/VaPagination.vue
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ const inputValue = ref('')
const usesTotal = computed(() => !!((totalComputed.value || totalComputed.value === 0) && pageSizeComputed.value))
const { valueComputed } = useStateful(props, emit)
const valueComputed = useStateful(props, emit)
const currentValue = computed({
get: () => usesTotal.value ? Math.ceil(valueComputed.value / pageSizeComputed.value!) || 1 : valueComputed.value,
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-rating/hooks/useRating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const useRatingProps = {
export const useRating = (props: ExtractPropTypes<typeof useRatingProps> & ExtractPropTypes<typeof useFormFieldProps>, el: Ref<HTMLElement | undefined>) => {
const { emit } = getContext()
const isHovered = useElementHovered(el)
const { valueComputed: modelValue } = useStateful(props, emit)
const modelValue = useStateful(props, emit)

const hoveredValue = ref(0)
const visibleValue = computed(() => !props.disabled && !props.readonly && props.hover && isHovered.value ? hoveredValue.value : modelValue.value)
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-slider/VaSlider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ const offset = ref(0)
const size = ref(0)
const defaultValue: number | number[] = props.range ? [0, 100] : 0
const { valueComputed }: { valueComputed: WritableComputedRef<number | number[]> } = useStateful(props, emit, 'modelValue', { defaultValue })
const valueComputed: WritableComputedRef<number | number[]> = useStateful(props, emit, 'modelValue', { defaultValue })
const currentSliderDotIndex = ref(0)
const hasMouseDown = ref(false)
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-split/VaSplit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const emit = defineEmits([...useStatefulEmits])
const splitPanelsContainer = shallowRef<HTMLElement>()
const { valueComputed } = useStateful(props, emit)
const valueComputed = useStateful(props, emit)
const containerSize = ref()
const bodyFontSize = ref(16)
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-stepper/VaStepper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const props = defineProps({
const emit = defineEmits(['update:modelValue', 'finish', 'update:steps'])
const stepperNavigation = shallowRef<HTMLElement>()
const { valueComputed: modelValue }: { valueComputed: Ref<number> } = useStateful(props, emit, 'modelValue')
const modelValue: Ref<number> = useStateful(props, emit, 'modelValue')
const stepsComputed = computed(() => {
if (!props.finishStep) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-tabs/VaTabs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const showPagination = ref(false)
const tabsContentOffset = ref(0)
const startingXPoint = ref(0)
const animationIncluded = ref(false)
const { valueComputed: tabSelected }: { valueComputed: WritableComputedRef<TabSelected> } = useStateful(props, emit)
const tabSelected: WritableComputedRef<TabSelected> = useStateful(props, emit)
const tabConfig = reactive({
VaTab: {
Expand Down
4 changes: 1 addition & 3 deletions packages/ui/src/components/va-textarea/VaTextarea.vue
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ const emit = defineEmits([...createEmits(), ...useValidationEmits])
const attrs = useAttrs()
const textarea = shallowRef<HTMLTextAreaElement>()
const { valueComputed } = useStateful(props, emit, 'modelValue', {
defaultValue: '',
})
const valueComputed = useStateful(props, emit, 'modelValue')
const focus = () => {
focusElement(textarea.value)
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-time-input/VaTimeInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const timePicker = shallowRef<typeof VaTimePicker>()
const { isOpenSync, dropdownProps } = useDropdownableControl(props, emit, {
defaultCloseOnValueUpdate: computed(() => Array.isArray(props.view) && props.view.length === 1),
})
const { valueComputed } = useStateful<Date>(props, emit)
const valueComputed = useStateful<Date>(props, emit)
const { parse, isValid } = useTimeParser(props)
const { format } = useTimeFormatter(props)
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/va-time-picker/VaTimePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const props = defineProps({
const emit = defineEmits([...useStatefulEmits])
const { valueComputed } = useStateful(props, emit)
const valueComputed = useStateful(props, emit)
const { columns, isPM } = useTimePicker(props, valueComputed)
const cellHeightComputed = useNumericProp('cellHeight') as ComputedRef<number>
const visibleCellsCountComputed = useNumericProp('visibleCellsCount') as ComputedRef<number>
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/src/components/va-tree-view/hooks/useTreeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ const useTreeView: UseTreeViewFunc = (props, emit) => {
getNodeProperty,
} = useTreeHelpers(props)
const { nodes, expandAll, filter, filterMethod, textBy } = toRefs(props)
const { valueComputed: expandedList } = useStateful(props, emit, 'expanded')
const { valueComputed: checkedList } = useStateful(props, emit, 'checked')
const expandedList = useStateful(props, emit, 'expanded')
const checkedList = useStateful(props, emit, 'checked')

const selectedNode = ref()
const selectedNodeComputed: WritableComputedRef<string | number | Record<string, unknown>> = computed({
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/composables/tests/useStateful.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const TestComponentRich = defineComponent({
template: '<p></p>',
props: { ...useStatefulProps },
setup (props, { emit }) {
const { valueComputed } = useStateful(props, emit)
const valueComputed = useStateful(props, emit)

return {
valueComputed,
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/composables/useSelectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const useSelectable = (
unwrapEl(input.value)?.focus()
}

const { valueComputed } = useStateful(props, emit)
const valueComputed = useStateful(props, emit)

const {
computedError,
Expand Down
12 changes: 6 additions & 6 deletions packages/ui/src/composables/useStateful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const useStateful = <

const defaultValuePassed = 'defaultValue' in options

// TODO: Prefer props[key] instead of defaultValue and remove defaultValue from options
const valueState = ref(
isUserProvidedProp.value
? props[key]
Expand All @@ -78,27 +79,26 @@ export const useStateful = <
stateful ? watchModelValue() : unwatchModelValue?.()
}, { immediate: true })

const valueComputed = computed({
const statefulValue = computed({
get: () => {
if (isUserProvidedProp.value) { return props[key] }
if (props.stateful) { return valueState.value }

return props[key]
},
set: (value) => {
if (props.stateful && valueComputed.value !== value) { valueState.value = value }
if (props.stateful && statefulValue.value !== value) { valueState.value = value }

emit(event, value)
},
}) as StatefulValue<P[Key]>

Object.defineProperty(valueComputed, 'stateful', {
Object.defineProperty(statefulValue, 'stateful', {
get: () => props.stateful,
})

Object.defineProperty(valueComputed, 'userProvided', {
Object.defineProperty(statefulValue, 'userProvided', {
get: () => isUserProvidedProp.value,
})

return { valueComputed }
return statefulValue
}

0 comments on commit 469fa61

Please sign in to comment.