generated from arvinxx/npm-template
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
40 additions
and
36 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,21 +1,17 @@ | ||
import { ReturnStyleToUse } from '@/types'; | ||
import { ReturnStyleToUse, StyleInputType } from '@/types'; | ||
import { createStyles, StyleOrGetStyleFn } from './createStyles'; | ||
|
||
/** | ||
* 业务应用中创建复合通用样式的进阶 | ||
*/ | ||
export function createStylish<Props, Styles>( | ||
cssStyleOrGetCssStyleFn: StyleOrGetStyleFn<Props, Styles>, | ||
export function createStylish<Props, Styles extends StyleInputType>( | ||
cssStyleOrGetCssStyleFn: StyleOrGetStyleFn<Styles, Props>, | ||
) { | ||
// FIXME :类型定义 | ||
// @ts-ignore | ||
const useStyles = createStyles(cssStyleOrGetCssStyleFn); | ||
|
||
return (props?: Props): ReturnStyleToUse<Styles> => { | ||
const { styles } = useStyles(props); | ||
|
||
// FIXME :类型定义 | ||
// @ts-ignore | ||
return styles; | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,24 +1,28 @@ | ||
import { CSSObject } from '@emotion/css'; | ||
|
||
type CSSParams<T> = T extends string ? string : CSSObject; | ||
type StyleObject = Record<string, CSSObject | string>; | ||
|
||
/** | ||
* 给用户提供类型定义的返回签名 | ||
* 用户可以使用 key: css` color:red `,或者 key :{ color : 'red' },都能正常提供类型定义 | ||
* 所有用户的可能的入参类型 | ||
*/ | ||
export type StyleDefinition<Style> = Style extends string | ||
? string | ||
: { | ||
[Key in keyof Style]: CSSParams<Style[Key]>; | ||
}; | ||
export type StyleInputType = string | CSSObject | StyleObject; | ||
|
||
type StyleObjectOnly<T extends StyleInputType> = T extends string ? never : T; | ||
|
||
/** | ||
* 根据用户输入的样式对象,导出可以给用户使用消费的类型泛型 | ||
* 譬如用户输入为 { a: css`color: red;`, b: { color: 'red' } | ||
* 输出的类型泛型为 { a:string; b:string } | ||
*/ | ||
type DefinitionToResult<T, K extends keyof T = keyof T> = { | ||
[P in K]: string; | ||
}; | ||
|
||
/** | ||
* 根据用户返回的样式对象,返回一个可以给用户使用的 | ||
* 譬如用户输入为 { a: css`color: red;`, b: { color: 'red' } | ||
* 输出的类型泛型为 { a:string; b:string } | ||
*/ | ||
export type ReturnStyleToUse<T> = T extends string | ||
? string | ||
: { | ||
[Key in keyof T]: string; | ||
}; | ||
export type ReturnStyleToUse<T extends StyleInputType> = T extends string | ||
? T | ||
: DefinitionToResult<StyleObjectOnly<T>>; |