diff --git a/components/styles/__tests__/__snapshots__/theme-provider.test.tsx.snap b/components/styles/__tests__/__snapshots__/theme-provider.test.tsx.snap index 13d93b9..44a80ed 100644 --- a/components/styles/__tests__/__snapshots__/theme-provider.test.tsx.snap +++ b/components/styles/__tests__/__snapshots__/theme-provider.test.tsx.snap @@ -4,7 +4,7 @@ exports[`ThemeProvider should merge themes with custom function 1`] = ` initialize { "0": Object { "attribs": Object { - "class": "", + "class": " ", }, "children": Array [ Object { @@ -25,6 +25,10 @@ initialize { p { color: #ccc; } + + .custom-size { + font-size: inherit; + } ", "next": null, "parent": [Circular], @@ -70,6 +74,10 @@ initialize { p { color: #ccc; } + + .custom-size { + font-size: inherit; + } ", "next": null, "parent": [Circular], @@ -113,6 +121,10 @@ initialize { p { color: #ccc; } + + .custom-size { + font-size: inherit; + } ", "next": null, "parent": [Circular], @@ -126,7 +138,7 @@ initialize { "parent": null, "prev": Object { "attribs": Object { - "class": "", + "class": " ", }, "children": Array [ Object { @@ -170,7 +182,7 @@ initialize { "children": Array [ Object { "attribs": Object { - "class": "", + "class": " ", }, "children": Array [ Object { diff --git a/components/text/__tests__/__snapshots__/index.test.tsx.snap b/components/text/__tests__/__snapshots__/index.test.tsx.snap index 82f9922..9d4a0cc 100644 --- a/components/text/__tests__/__snapshots__/index.test.tsx.snap +++ b/components/text/__tests__/__snapshots__/index.test.tsx.snap @@ -1,109 +1,221 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Text should render P element in the default 1`] = ` -"

test-value

" +`; + +exports[`Text should be able to specify the size of text 2`] = ` +"

test-value

" +`; + +exports[`Text should render P element in the default 1`] = ` +"

test-value

" `; exports[`Text should render default color when type missing 1`] = ` -"

test-value

" `; exports[`Text should work with different styles 1`] = ` -"

test-value

test-value

test-value

test-value

test-value

test-value

test-value

" `; exports[`Text the combined style should be rendered 1`] = ` -"

test-value

" `; exports[`Text the specified element should be rendered 1`] = ` -"

test-value

test-value

test-value

test-value

test-value

test-value

test-value

test-value
test-value
test-value
test-value

test-value

test-value

test-valuetest-valuetest-valuetest-valuetest-valuetest-valuetest-valuetest-valuetest-valuetest-valuetest-valuetest-value
" `; diff --git a/components/text/__tests__/index.test.tsx b/components/text/__tests__/index.test.tsx index 5da7bed..2bc3078 100644 --- a/components/text/__tests__/index.test.tsx +++ b/components/text/__tests__/index.test.tsx @@ -54,4 +54,13 @@ describe('Text', () => { expect(wrapper.html()).toMatchSnapshot() expect(() => wrapper.unmount()).not.toThrow() }) + + it('should be able to specify the size of text', () => { + let wrapper = mount(test-value) + expect(wrapper.html()).toMatchSnapshot() + + wrapper = mount(test-value) + expect(wrapper.html()).toMatchSnapshot() + expect(() => wrapper.unmount()).not.toThrow() + }) }) diff --git a/components/text/child.tsx b/components/text/child.tsx index ddcc30e..1cfc413 100644 --- a/components/text/child.tsx +++ b/components/text/child.tsx @@ -7,6 +7,7 @@ import { ZeitUIThemesPalette } from '../styles/themes' export interface Props { tag: keyof JSX.IntrinsicElements type?: NormalTypes + size?: string | number className?: '' } @@ -31,7 +32,7 @@ type NativeAttrs = Omit, keyof Props> export type TextChildProps = Props & typeof defaultProps & NativeAttrs const TextChild: React.FC> = React.memo(({ - children, tag, className, type, ...props + children, tag, className, type, size, ...props }) => { const theme = useTheme() const Component = tag @@ -39,13 +40,26 @@ const TextChild: React.FC> = React.memo( () => getTypeColor(type, theme.palette), [type, theme.palette], ) + const fontSize = useMemo(() => { + if (!size) return 'inherit' + if (typeof size === 'number') return `${size}px` + return size + }, [size]) + return ( <> - {children} + + {children} + ) diff --git a/components/text/text.tsx b/components/text/text.tsx index c2cb9c0..610f68f 100644 --- a/components/text/text.tsx +++ b/components/text/text.tsx @@ -19,6 +19,7 @@ interface Props { em?: boolean blockquote?: boolean className?: string + size?: string | number type?: NormalTypes } @@ -48,19 +49,23 @@ export type TextProps = Props & typeof defaultProps & NativeAttrs type TextRenderableElements = Array -const getModifierChild = (tags: TextRenderableElements, children: ReactNode) => { +const getModifierChild = ( + tags: TextRenderableElements, + children: ReactNode, + size?: string | number +) => { if (!tags.length) return children const nextTag = tags.slice(1, tags.length) return ( - - {getModifierChild(nextTag, children)} + + {getModifierChild(nextTag, children, size)} ) } const Text: React.FC> = React.memo(({ h1, h2, h3, h4, h5, h6, p, b, small, i, span, del, em, blockquote, - children, className, ...props + size, children, className, ...props }) => { const elements: ElementMap = { h1, h2, h3, h4, h5, h6, p, blockquote } const inlineElements: ElementMap = { span, small, b, em, i, del } @@ -90,11 +95,11 @@ const Text: React.FC> = React.memo(({ const modifers = useMemo(() => { if (!renderableChildElements.length) return children - return getModifierChild(renderableChildElements, children) - }, [renderableChildElements, children]) + return getModifierChild(renderableChildElements, children, size) + }, [renderableChildElements, children, size]) return ( - {modifers} + {modifers} ) }) diff --git a/pages/en-us/components/text.mdx b/pages/en-us/components/text.mdx index 1ec87e3..90f93ae 100644 --- a/pages/en-us/components/text.mdx +++ b/pages/en-us/components/text.mdx @@ -93,6 +93,19 @@ Display text using well-defined typographic styles. `} /> + + Font Size: 12px; + Font Size: 14px; + Font Size: 100%; + Font Size: 1.25rem; + Font Size: 2em; + +`} /> + `} /> + + Font Size: 12px; + Font Size: 14px; + Font Size: 100%; + Font Size: 1.25rem; + Font Size: 2em; + +`} /> + Text.Props @@ -132,6 +145,7 @@ export const meta = { | **em** | 渲染的组件名 | `boolean` | - | `false` | | **b** | 渲染的组件名 | `boolean` | - | `false` | | **blockquote** | 渲染的组件名 | `boolean` | - | `false` | +| **size** | 手动设置文字大小 | `string` / `number` | - | 跟随元素变化 | | **type** | 文字类型 | `NormalTypes` | [TextTypes](#texttypes) | `default` | | ... | 原生属性 | `HTMLAttributes` | `'id', 'className', ...` | - |