-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(select-input): 完成select-input&优化popup (#146)
* fix(icon): 修复icon不居中显示和class重复设置问题 * fix(icon): 修复icon不居中显示和class重复设置问题 * fix(icon): 修复icon不居中显示和class重复设置问题 * fix(watermark): 修复watermark无法生成水印及其它展示问题 * fix(message): 临时使用console代替 * feat(select-input): 增加select-input组件 * refactor(input): restore input * perf(popup): 优化popup dom结构 * feat(popup): 优化popup * feat(input-select): add input-select * fix(omi-transition): 动画enter、leave多次同时运行问题修复 * feat(select-input): add select-input * feat(select-input): add select-input * feat(select-input): 去除--force * feat(select-input): 优化css字符串
- Loading branch information
Showing
41 changed files
with
2,943 additions
and
160 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import upperFirst from 'lodash/upperFirst'; | ||
import { Component, setActiveComponent, signal, SignalValue } from 'omi'; | ||
|
||
export interface ChangeHandler<T, P extends any[]> { | ||
(value: T, ...args: P); | ||
} | ||
|
||
type Defaultoptions<T extends string> = `default${Capitalize<T>}`; | ||
|
||
type ToString<T extends string | number | symbol> = T extends string ? T : `${Extract<T, number>}`; | ||
|
||
const useControlled: <P extends any[], R extends object, K extends keyof R>( | ||
props: R, | ||
valueKey: K, | ||
onChange: ChangeHandler<R[K], P>, | ||
defaultOptions?: { | ||
[key in Defaultoptions<ToString<K>>]?: R[K]; | ||
} & { [key: string]: any; activeComponent?: Component }, | ||
) => [SignalValue<R[K]> | R[K], ChangeHandler<R[K], P>] = ( | ||
// eslint-disable-next-line default-param-last | ||
props = {} as any, | ||
valueKey, | ||
onChange, | ||
defaultOptions = {}, | ||
) => { | ||
// 外部设置 props,说明希望受控 | ||
const controlled = Reflect.has(props, valueKey); | ||
// 受控属性 | ||
const value = props[valueKey]; | ||
// 约定受控属性的非受控 key 为 defaultXxx,某些条件下要在运行时确定 defaultXxx 则通过 defaultOptions 来覆盖 | ||
const defaultValue = | ||
defaultOptions[`default${upperFirst(valueKey as string)}`] || props[`default${upperFirst(valueKey as string)}`]; | ||
|
||
// 受控模式 | ||
if (controlled) return [value, onChange || (() => {})]; | ||
|
||
// 无论是否受控,都要维护一个内部变量,默认值由 defaultValue 控制 | ||
const internalValue = signal(defaultValue); | ||
if (defaultOptions.activeComponent) { | ||
setActiveComponent(defaultOptions.activeComponent); | ||
} | ||
|
||
// 非受控模式 | ||
return [ | ||
internalValue.value, | ||
(newValue, ...args) => { | ||
internalValue.value = newValue; | ||
onChange?.(newValue, ...args); | ||
}, | ||
]; | ||
}; | ||
|
||
export default useControlled; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.