Skip to content

Commit

Permalink
refactor: remove all deprecated api (#1188)
Browse files Browse the repository at this point in the history
  • Loading branch information
danranVm committed Oct 9, 2022
1 parent da09bd5 commit 4dd2899
Show file tree
Hide file tree
Showing 109 changed files with 580 additions and 1,676 deletions.
5 changes: 2 additions & 3 deletions lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
"version": "1.0.0-rc.9",
"command": {
"publish": {
"allowBranch": ["main", "next", "hotfix"],
"allowBranch": ["main", "release", "next", "hotfix"],
"ignoreChanges": ["*.md", "**/__tests__/**", "**/demo/**", "**/docs/**"],
"message": "docs(release): publish %s",
"registry": "https://registry.npmjs.org",
"preid": "rc"
"registry": "https://registry.npmjs.org"
},
"bootstrap": {
"npmClient": "pnpm"
Expand Down
16 changes: 1 addition & 15 deletions packages/cdk/forms/__tests__/formControl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('formControl.ts', () => {
test('validate work', async () => {
expect(await control.validate()).toBeUndefined()

control.setValidator(Validators.required)
control.setValidators(Validators.required)

expect(await control.validate()).toEqual({ required: { message: zhCNMessages.required({}, control) } })
})
Expand Down Expand Up @@ -197,19 +197,5 @@ describe('formControl.ts', () => {

expect(control.getValue()).toEqual(' test ')
})

test('trim enable work', () => {
control = new FormControl<string>('', { trim: true })

expect(control.getValue()).toEqual('')

control.setValue('test ')

expect(control.getValue()).toEqual('test')

control.setValue(' test')

expect(control.getValue()).toEqual('test')
})
})
})
46 changes: 3 additions & 43 deletions packages/cdk/forms/src/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import {
watchEffect,
} from 'vue'

import { isArray, isNil, isPlainObject, isString } from 'lodash-es'
import { isArray, isNil, isPlainObject } from 'lodash-es'

import { Logger, hasOwnProperty } from '@idux/cdk/utils'
import { hasOwnProperty } from '@idux/cdk/utils'

import {
type AsyncValidatorFn,
Expand Down Expand Up @@ -154,17 +154,6 @@ export abstract class AbstractControl<T = any> {
return this._trigger ?? this._parent?.trigger ?? 'change'
}

/**
* @deprecated
*
* Whether to remove the first and tail space
* Possible value: true | false
* Default value: false
*/
get trim(): boolean {
return this._trim ?? this._parent?.trim ?? false
}

name?: string
example?: string

Expand All @@ -183,7 +172,6 @@ export abstract class AbstractControl<T = any> {
private _composedAsyncValidators: AsyncValidatorFn | undefined
private _parent: AbstractControl<T> | undefined
private _trigger?: 'change' | 'blur' | 'submit'
private _trim?: boolean

constructor(
controls?: GroupControls<T> | AbstractControl<ArrayElement<T>>[],
Expand All @@ -196,12 +184,6 @@ export abstract class AbstractControl<T = any> {
this._forEachControls(control => control.setParent(this))
this._convertOptions(validatorOrOptions, asyncValidator)
this._init()

if (__DEV__) {
if (isOptions(validatorOrOptions) && validatorOrOptions.trim) {
Logger.warn('cdk/forms', 'the `trim` of validatorOptions was deprecated.')
}
}
}

/**
Expand Down Expand Up @@ -337,16 +319,6 @@ export abstract class AbstractControl<T = any> {
}
}

/**
* @deprecated please use `setValidators` instead.
*/
setValidator(newValidator?: ValidatorFn | ValidatorFn[]): void {
if (__DEV__) {
Logger.warn('cdk/forms', 'the `setValidator` was deprecated, please use `setValidators` instead.')
}
this.setValidators(newValidator)
}

/**
* Sets the new sync validator for the form control, it overwrites existing sync validators.
*
Expand All @@ -357,16 +329,6 @@ export abstract class AbstractControl<T = any> {
this._composedValidators = toValidator(newValidators)
}

/**
* @deprecated please use `setAsyncValidators` instead.
*/
setAsyncValidator(newAsyncValidator?: AsyncValidatorFn | AsyncValidatorFn[]): void {
if (__DEV__) {
Logger.warn('cdk/forms', 'the `setAsyncValidator` was deprecated, please use `setAsyncValidators` instead.')
}
this.setAsyncValidators(newAsyncValidator)
}

/**
* Sets the new async validator for the form control, it overwrites existing async validators.
*
Expand Down Expand Up @@ -597,7 +559,6 @@ export abstract class AbstractControl<T = any> {
this.name = validatorOrOptions.name
this.example = validatorOrOptions.example
this._trigger = validatorOrOptions.trigger ?? this._trigger
this._trim = validatorOrOptions.trim ?? this._trim
this.setValidators(validatorOrOptions.validators)
this.setAsyncValidators(validatorOrOptions.asyncValidators)
if (validatorOrOptions.disabled) {
Expand Down Expand Up @@ -706,8 +667,7 @@ export class FormControl<T = any> extends AbstractControl<T> {
}

getValue(): T {
const value = this._valueRef.value
return this.trim && isString(value) ? (value as any).trim() : value
return this._valueRef.value
}

protected _forEachControls(_: (v: AbstractControl, k: never) => void): void {}
Expand Down
4 changes: 0 additions & 4 deletions packages/cdk/forms/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ export interface ValidatorOptions {
trigger?: 'change' | 'blur' | 'submit'
validators?: ValidatorFn | ValidatorFn[]
asyncValidators?: AsyncValidatorFn | AsyncValidatorFn[]
/**
* @deprecated
*/
trim?: boolean
}

export type ValidateStatus = 'valid' | 'invalid' | 'validating'
129 changes: 0 additions & 129 deletions packages/cdk/forms/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import {
type ComputedRef,
type InjectionKey,
type ShallowRef,
type WatchStopHandle,
computed,
getCurrentInstance,
inject,
reactive,
shallowReactive,
shallowRef,
toRaw,
unref,
Expand Down Expand Up @@ -218,129 +215,3 @@ export function useAccessorAndControl<T = any>(

return { accessor, control }
}

/**
* @deprecated
*/
export interface ValueControlOptions {
controlKey?: string
}

/**
* @deprecated please use `useControl` or `useAccessorAndControl` instead
*/
export function useValueControl<T = any>(
options: ValueControlOptions = {},
): ShallowRef<AbstractControl<T> | undefined> {
if (__DEV__) {
Logger.warn(
'cdk/forms',
'the `useValueControl` was deprecated, please use `useControl` or `useAccessorAndControl` instead.',
)
}
const { controlKey = 'control' } = options
const { props } = getCurrentInstance()!
const parentControl = inject(FORMS_CONTROL_TOKEN, shallowRef<AbstractControl>())

const control = shallowRef<AbstractControl>()
let watchStop: WatchStopHandle | undefined

watch(
[() => props[controlKey], parentControl],
([controlOrPath, pControl]) => {
if (watchStop) {
watchStop()
watchStop = undefined
}
if (isAbstractControl(controlOrPath)) {
control.value = controlOrPath
} else if (!!pControl && !isNil(controlOrPath)) {
watchStop = watch(
pControl.controls,
() => {
const _control = pControl.get(controlOrPath as ControlPathType)
if (__DEV__ && !_control) {
Logger.warn('cdk/forms', `not find control by [${controlOrPath}]`)
}
control.value = _control
},
{ immediate: true },
)
}
},
{ immediate: true },
)

return control
}

/**
* @deprecated
*/
export interface ValueAccessorOptions<T = any> {
control: ShallowRef<AbstractControl<T> | undefined>
valueKey?: string
disabledKey?: string
}

/**
* @deprecated
*/
export interface ValueAccessor<T = any> {
valueRef: ComputedRef<T>
disabled: ComputedRef<boolean>
markAsBlurred: () => void
setValue: (value: T) => void
}

/**
* @deprecated please use `useAccessor` or `useAccessorAndControl` instead
*/
export function useValueAccessor<T = any>(options: ValueAccessorOptions): ValueAccessor<T> {
if (__DEV__) {
Logger.warn(
'cdk/forms',
'the `useValueAccessor` was deprecated, please use `useAccessor` or `useAccessorAndControl` instead.',
)
}
const { control, valueKey = 'value', disabledKey = 'disabled' } = options
const { props } = getCurrentInstance()!

const accessor = shallowReactive({} as ValueAccessor<T>)
let watchStop: WatchStopHandle | undefined

watch(
control,
currControl => {
if (watchStop) {
watchStop()
watchStop = undefined
}

if (currControl) {
accessor.valueRef = currControl.valueRef
accessor.disabled = currControl.disabled
accessor.setValue = value => currControl.setValue(value, { dirty: true })
accessor.markAsBlurred = () => currControl.markAsBlurred()
} else {
const tempRef = shallowRef(props[valueKey])
watchStop = watch(
() => props[valueKey],
value => (tempRef.value = value),
)
accessor.valueRef = computed(() => props[valueKey] ?? tempRef.value) as ComputedRef<T>
accessor.disabled = computed(() => props[disabledKey]) as ComputedRef<boolean>
accessor.setValue = value => {
if (value != toRaw(accessor.valueRef.value)) {
tempRef.value = value
callEmit((props as any)[`onUpdate:${valueKey}`], value)
}
}
accessor.markAsBlurred = NoopFunction
}
},
{ immediate: true },
)

return accessor
}
2 changes: 1 addition & 1 deletion packages/cdk/resize/src/resize-observer/ResizeObserver.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default defineComponent({
([disabled, options]) => {
cleanup()
if (!disabled) {
stop = useResizeObserver(elementRef, handler, options).stop
stop = useResizeObserver(elementRef, handler, options)
}
},
{ immediate: true, flush: 'post' },
Expand Down
15 changes: 2 additions & 13 deletions packages/cdk/resize/src/resize-observer/useResizeObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

import { watch } from 'vue'

import { Logger, type MaybeElementRef, convertElement, tryOnScopeDispose } from '@idux/cdk/utils'
import { type MaybeElementRef, convertElement, tryOnScopeDispose } from '@idux/cdk/utils'

import { type ResizeListener, offResize, onResize } from './utils'

export function useResizeObserver(
target: MaybeElementRef,
listener: ResizeListener,
options?: ResizeObserverOptions,
): { stop: () => void } {
): () => void {
const stopWatch = watch(
() => convertElement(target),
(currElement, prevElement) => {
Expand All @@ -35,17 +35,6 @@ export function useResizeObserver(
stopWatch()
}

stop.stop = () => {
if (__DEV__) {
Logger.warn(
'cdk/resize',
'the `const { stop } = useResizeObserver()` was deprecated, please use `const stop = useResizeObserver()` instead.',
)
}
offResize(convertElement(target), listener)
stopWatch()
}

tryOnScopeDispose(stop)

return stop
Expand Down
7 changes: 1 addition & 6 deletions packages/cdk/scroll/src/virtual/VirtualScroll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { type ComponentPublicInstance, computed, defineComponent, provide, ref, watch } from 'vue'

import { Logger, callEmit, useState } from '@idux/cdk/utils'
import { callEmit, useState } from '@idux/cdk/utils'

import { useContainerHeight } from './composables/useContainerHeight'
import { useGetKey } from './composables/useGetKey'
Expand Down Expand Up @@ -75,11 +75,6 @@ export default defineComponent({
const mergedData = computed(() => props.dataSource.slice(startIndex.value, endIndex.value + 1))
watch(mergedData, data => callEmit(props.onScrolledChange, startIndex.value, endIndex.value, data))

if (__DEV__) {
if (props.itemKey) {
Logger.warn('cdk/scroll', 'the `itemKey` of VirtualScrollProps was deprecated, please use `getKey` instead.')
}
}
return () => {
const getKeyFn = getKey.value
const start = startIndex.value
Expand Down
Loading

0 comments on commit 4dd2899

Please sign in to comment.