From ab1d41ae14aaad3a5039688eb6950aa76a5a34bd Mon Sep 17 00:00:00 2001 From: unix Date: Sat, 23 May 2020 07:59:14 +0800 Subject: [PATCH 1/2] feat(checkbox): add support of different sizes --- components/checkbox/checkbox-group.tsx | 18 +++++++++++++++++- components/checkbox/checkbox.icon.tsx | 18 ++++-------------- components/checkbox/checkbox.tsx | 21 +++++++++++++-------- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/components/checkbox/checkbox-group.tsx b/components/checkbox/checkbox-group.tsx index 4f99df8..6a3f3db 100644 --- a/components/checkbox/checkbox-group.tsx +++ b/components/checkbox/checkbox-group.tsx @@ -2,26 +2,40 @@ import React, { useEffect, useMemo, useState } from 'react' import withDefaults from '../utils/with-defaults' import { CheckboxContext } from './checkbox-context' import useWarning from '../utils/use-warning' +import { NormalSizes } from '../utils/prop-types' interface Props { value: string[] disabled?: boolean + size?: NormalSizes onChange?: (values: string[]) => void className?: string } const defaultProps = { disabled: false, + size: 'small' as NormalSizes, className: '', } type NativeAttrs = Omit, keyof Props> export type CheckboxGroupProps = Props & typeof defaultProps & NativeAttrs +export const getCheckboxSize = (size: NormalSizes): string => { + const sizes: { [key in NormalSizes]: string } = { + mini: '.75rem', + small: '.875rem', + medium: '1rem', + large: '1.125rem', + } + return sizes[size] +} + const CheckboxGroup: React.FC> = ({ disabled, onChange, value, + size, children, className, ...props @@ -47,6 +61,7 @@ const CheckboxGroup: React.FC> = ({ values: selfVal, } }, [disabled, selfVal]) + const fontSize = useMemo(() => getCheckboxSize(size), [size]) useEffect(() => { setSelfVal(value) @@ -58,7 +73,8 @@ const CheckboxGroup: React.FC> = ({ {children} diff --git a/components/checkbox/checkbox.icon.tsx b/components/checkbox/checkbox.icon.tsx index 4c7bbb7..a89dd37 100644 --- a/components/checkbox/checkbox.icon.tsx +++ b/components/checkbox/checkbox.icon.tsx @@ -20,12 +20,7 @@ const CheckboxIcon: React.FC = ({ disabled, checked }) => { return ( <> {checked ? ( - + = ({ disabled, checked }) => { ) : ( - + = ({ disabled, checked }) => {
" +`; diff --git a/components/checkbox/__tests__/__snapshots__/index.test.tsx.snap b/components/checkbox/__tests__/__snapshots__/index.test.tsx.snap index d66d815..72e8f6a 100644 --- a/components/checkbox/__tests__/__snapshots__/index.test.tsx.snap +++ b/components/checkbox/__tests__/__snapshots__/index.test.tsx.snap @@ -10,9 +10,7 @@ initialize { Object { "attribs": Object { "fill": "none", - "height": "12px", "viewBox": "0 0 12 12", - "width": "12px", "xmlns": "http://www.w3.org/2000/svg", }, "children": Array [ @@ -47,8 +45,8 @@ initialize { "data": " svg { display: inline-flex; - width: 12px; - height: 12px; + width: calc(0.86 * var(--checkbox-size)); + height: calc(0.86 * var(--checkbox-size)); user-select: none; opacity: 1; cursor: pointer; @@ -90,20 +88,21 @@ initialize { Object { "data": " label { - height: 0.875rem; - line-height: 0.875rem; + --checkbox-size: .875rem; display: inline-flex; justify-content: center; align-items: center; width: auto; cursor: pointer; opacity: 1; + height: var(--checkbox-size); + line-height: var(--checkbox-size); } .text { - font-size: 0.875rem; - line-height: 0.875rem; - padding-left: 0.5rem; + font-size: var(--checkbox-size); + line-height: var(--checkbox-size); + padding-left: calc(var(--checkbox-size) * 0.57); user-select: none; cursor: pointer; } @@ -166,16 +165,12 @@ initialize { "type": "tag", "x-attribsNamespace": Object { "fill": undefined, - "height": undefined, "viewBox": undefined, - "width": undefined, "xmlns": "http://www.w3.org/2000/xmlns/", }, "x-attribsPrefix": Object { "fill": undefined, - "height": undefined, "viewBox": undefined, - "width": undefined, "xmlns": "", }, }, @@ -186,8 +181,8 @@ initialize { "data": " svg { display: inline-flex; - width: 12px; - height: 12px; + width: calc(0.86 * var(--checkbox-size)); + height: calc(0.86 * var(--checkbox-size)); user-select: none; opacity: 1; cursor: pointer; @@ -229,20 +224,21 @@ initialize { Object { "data": " label { - height: 0.875rem; - line-height: 0.875rem; + --checkbox-size: .875rem; display: inline-flex; justify-content: center; align-items: center; width: auto; cursor: pointer; opacity: 1; + height: var(--checkbox-size); + line-height: var(--checkbox-size); } .text { - font-size: 0.875rem; - line-height: 0.875rem; - padding-left: 0.5rem; + font-size: var(--checkbox-size); + line-height: var(--checkbox-size); + padding-left: calc(var(--checkbox-size) * 0.57); user-select: none; cursor: pointer; } @@ -298,9 +294,7 @@ initialize { "prev": Object { "attribs": Object { "fill": "none", - "height": "12px", "viewBox": "0 0 12 12", - "width": "12px", "xmlns": "http://www.w3.org/2000/svg", }, "children": Array [ @@ -334,16 +328,12 @@ initialize { "type": "tag", "x-attribsNamespace": Object { "fill": undefined, - "height": undefined, "viewBox": undefined, - "width": undefined, "xmlns": "http://www.w3.org/2000/xmlns/", }, "x-attribsPrefix": Object { "fill": undefined, - "height": undefined, "viewBox": undefined, - "width": undefined, "xmlns": "", }, }, @@ -379,20 +369,21 @@ initialize { Object { "data": " label { - height: 0.875rem; - line-height: 0.875rem; + --checkbox-size: .875rem; display: inline-flex; justify-content: center; align-items: center; width: auto; cursor: pointer; opacity: 1; + height: var(--checkbox-size); + line-height: var(--checkbox-size); } .text { - font-size: 0.875rem; - line-height: 0.875rem; - padding-left: 0.5rem; + font-size: var(--checkbox-size); + line-height: var(--checkbox-size); + padding-left: calc(var(--checkbox-size) * 0.57); user-select: none; cursor: pointer; } @@ -442,8 +433,8 @@ initialize { "data": " svg { display: inline-flex; - width: 12px; - height: 12px; + width: calc(0.86 * var(--checkbox-size)); + height: calc(0.86 * var(--checkbox-size)); user-select: none; opacity: 1; cursor: pointer; @@ -462,9 +453,7 @@ initialize { "prev": Object { "attribs": Object { "fill": "none", - "height": "12px", "viewBox": "0 0 12 12", - "width": "12px", "xmlns": "http://www.w3.org/2000/svg", }, "children": Array [ @@ -498,16 +487,12 @@ initialize { "type": "tag", "x-attribsNamespace": Object { "fill": undefined, - "height": undefined, "viewBox": undefined, - "width": undefined, "xmlns": "http://www.w3.org/2000/xmlns/", }, "x-attribsPrefix": Object { "fill": undefined, - "height": undefined, "viewBox": undefined, - "width": undefined, "xmlns": "", }, }, @@ -544,20 +529,21 @@ initialize { Object { "data": " label { - height: 0.875rem; - line-height: 0.875rem; + --checkbox-size: .875rem; display: inline-flex; justify-content: center; align-items: center; width: auto; cursor: pointer; opacity: 1; + height: var(--checkbox-size); + line-height: var(--checkbox-size); } .text { - font-size: 0.875rem; - line-height: 0.875rem; - padding-left: 0.5rem; + font-size: var(--checkbox-size); + line-height: var(--checkbox-size); + padding-left: calc(var(--checkbox-size) * 0.57); user-select: none; cursor: pointer; } @@ -606,8 +592,8 @@ initialize { "data": " svg { display: inline-flex; - width: 12px; - height: 12px; + width: calc(0.86 * var(--checkbox-size)); + height: calc(0.86 * var(--checkbox-size)); user-select: none; opacity: 1; cursor: pointer; @@ -626,9 +612,7 @@ initialize { "prev": Object { "attribs": Object { "fill": "none", - "height": "12px", "viewBox": "0 0 12 12", - "width": "12px", "xmlns": "http://www.w3.org/2000/svg", }, "children": Array [ @@ -662,16 +646,12 @@ initialize { "type": "tag", "x-attribsNamespace": Object { "fill": undefined, - "height": undefined, "viewBox": undefined, - "width": undefined, "xmlns": "http://www.w3.org/2000/xmlns/", }, "x-attribsPrefix": Object { "fill": undefined, - "height": undefined, "viewBox": undefined, - "width": undefined, "xmlns": "", }, }, @@ -701,20 +681,21 @@ initialize { Object { "data": " label { - height: 0.875rem; - line-height: 0.875rem; + --checkbox-size: .875rem; display: inline-flex; justify-content: center; align-items: center; width: auto; cursor: pointer; opacity: 1; + height: var(--checkbox-size); + line-height: var(--checkbox-size); } .text { - font-size: 0.875rem; - line-height: 0.875rem; - padding-left: 0.5rem; + font-size: var(--checkbox-size); + line-height: var(--checkbox-size); + padding-left: calc(var(--checkbox-size) * 0.57); user-select: none; cursor: pointer; } @@ -774,8 +755,8 @@ initialize { "data": " svg { display: inline-flex; - width: 12px; - height: 12px; + width: calc(0.86 * var(--checkbox-size)); + height: calc(0.86 * var(--checkbox-size)); user-select: none; opacity: 1; cursor: pointer; @@ -794,9 +775,7 @@ initialize { "prev": Object { "attribs": Object { "fill": "none", - "height": "12px", "viewBox": "0 0 12 12", - "width": "12px", "xmlns": "http://www.w3.org/2000/svg", }, "children": Array [ @@ -830,16 +809,12 @@ initialize { "type": "tag", "x-attribsNamespace": Object { "fill": undefined, - "height": undefined, "viewBox": undefined, - "width": undefined, "xmlns": "http://www.w3.org/2000/xmlns/", }, "x-attribsPrefix": Object { "fill": undefined, - "height": undefined, "viewBox": undefined, - "width": undefined, "xmlns": "", }, }, @@ -905,3 +880,171 @@ initialize { }, } `; + +exports[`Checkbox should work correctly with different sizes 1`] = ` +"
" +`; diff --git a/components/checkbox/__tests__/group.test.tsx b/components/checkbox/__tests__/group.test.tsx index 896a815..abf36b6 100644 --- a/components/checkbox/__tests__/group.test.tsx +++ b/components/checkbox/__tests__/group.test.tsx @@ -18,6 +18,27 @@ describe('Checkbox Group', () => { expect(rendered).toMatchSnapshot() }) + it('should work correctly with different sizes', () => { + const wrapper = mount( +
+ + Sydney + + + Sydney + + + Sydney + + + Sydney + +
, + ) + expect(wrapper.html()).toMatchSnapshot() + expect(() => wrapper.unmount()).not.toThrow() + }) + it('should work correctly with initial value', () => { let wrapper = mount( diff --git a/components/checkbox/__tests__/index.test.tsx b/components/checkbox/__tests__/index.test.tsx index ef46cd9..acd1f71 100644 --- a/components/checkbox/__tests__/index.test.tsx +++ b/components/checkbox/__tests__/index.test.tsx @@ -10,6 +10,19 @@ describe('Checkbox', () => { expect(rendered).toMatchSnapshot() }) + it('should work correctly with different sizes', () => { + const wrapper = mount( +
+ mini + small + medium + large +
, + ) + expect(wrapper.html()).toMatchSnapshot() + expect(() => wrapper.unmount()).not.toThrow() + }) + it('should work correctly with initial value', () => { let wrapper = mount(Sydney) let input = wrapper.find('input').getDOMNode() diff --git a/pages/en-us/components/checkbox.mdx b/pages/en-us/components/checkbox.mdx index 79244f9..6e13c02 100644 --- a/pages/en-us/components/checkbox.mdx +++ b/pages/en-us/components/checkbox.mdx @@ -10,7 +10,6 @@ export const meta = { Displays a boolean value. - Sydney `} /> + + mini + + small + + medium + + large + +`} /> + Checkbox.Group.Props @@ -68,7 +84,14 @@ Displays a boolean value. | **value** | checked children | `Array` | - | `[]` | | **disabled** | disable checkbox group | `boolean` | - | `false` | | **onChange** | change event handler | `(values: string[]) => void` | - | - | -| ... | native props | `HTMLAttributes` | `'spellCheck', 'style', 'className', ...` | - | +| **size** | size of all checkboxes in the group | `NormalSizes` | [NormalSizes](#normalsizes) | `small` | +| ... | native props | `HTMLAttributes` | `'id', 'className', ...` | - | + +NormalSizes + +```ts +type NormalSizes = 'mini' | 'small' | 'medium' | 'large' +``` CheckboxEvent diff --git a/pages/zh-cn/components/checkbox.mdx b/pages/zh-cn/components/checkbox.mdx index 5773ee7..12bf793 100644 --- a/pages/zh-cn/components/checkbox.mdx +++ b/pages/zh-cn/components/checkbox.mdx @@ -18,6 +18,22 @@ export const meta = { 墨西哥 `} /> + + 最小 + + 较小 + + 中等 + + 最大 + +`} /> + Checkbox.Group.Props @@ -68,7 +85,14 @@ export const meta = { | **value** | 选中的一组值 | `Array` | - | `[]` | | **disabled** | 禁用一个组的复选框 | `boolean` | - | `false` | | **onChange** | 复选框的变化事件 | `(values: string[]) => void` | - | - | -| ... | 原生属性 | `HTMLAttributes` | `'spellCheck', 'style', 'className', ...` | - | +| **size** | 组内所有复选框的大小 | `NormalSizes` | [NormalSizes](#normalsizes) | `small` | +| ... | 原生属性 | `HTMLAttributes` | `'id', 'className', ...` | - | + +NormalSizes + +```ts +type NormalSizes = 'mini' | 'small' | 'medium' | 'large' +``` CheckboxEvent