Browse Source

refactor: optimize ColorPicker code again (#42334)

pull/42340/head
lijianan 2 years ago
committed by GitHub
parent
commit
0470646a5f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      components/color-picker/__tests__/__snapshots__/demo-extend.test.ts.snap
  2. 6
      components/color-picker/__tests__/__snapshots__/demo.test.ts.snap
  3. 2
      components/color-picker/demo/base.tsx
  4. 8
      components/color-picker/demo/format.tsx
  5. 2
      components/color-picker/demo/pure-panel.tsx
  6. 15
      components/color-picker/demo/trigger.tsx
  7. 6
      components/color-picker/index.en-US.md
  8. 2
      components/color-picker/index.zh-CN.md
  9. 8
      components/color-picker/style/index.ts
  10. 3
      components/color-picker/style/picker.ts

6
components/color-picker/__tests__/__snapshots__/demo-extend.test.ts.snap

@ -2187,7 +2187,7 @@ exports[`renders components/color-picker/demo/format.tsx extend context correctl
> >
HSB: HSB:
<span> <span>
hsb(215,91%,100%) hsb(215, 91%, 100%)
</span> </span>
</div> </div>
</div> </div>
@ -2815,7 +2815,7 @@ exports[`renders components/color-picker/demo/format.tsx extend context correctl
> >
RGB: RGB:
<span> <span>
rgb(22,119,255) rgb(22, 119, 255)
</span> </span>
</div> </div>
</div> </div>
@ -3973,7 +3973,7 @@ Array [
style="margin-right: 8px;" style="margin-right: 8px;"
> >
<div <div
style="width: 20px; height: 20px; border-radius: 4px; background: rgb(22, 119, 255);" style="width: 20px; height: 20px; border-radius: 4px; background-color: rgb(22, 119, 255);"
/> />
</div> </div>
<div <div

6
components/color-picker/__tests__/__snapshots__/demo.test.ts.snap

@ -135,7 +135,7 @@ exports[`renders components/color-picker/demo/format.tsx correctly 1`] = `
> >
HSB: HSB:
<span> <span>
hsb(215,91%,100%) hsb(215, 91%, 100%)
</span> </span>
</div> </div>
</div> </div>
@ -180,7 +180,7 @@ exports[`renders components/color-picker/demo/format.tsx correctly 1`] = `
> >
RGB: RGB:
<span> <span>
rgb(22,119,255) rgb(22, 119, 255)
</span> </span>
</div> </div>
</div> </div>
@ -234,7 +234,7 @@ exports[`renders components/color-picker/demo/trigger.tsx correctly 1`] = `
style="margin-right:8px" style="margin-right:8px"
> >
<div <div
style="width:20px;height:20px;border-radius:4px;background:#1677ff" style="width:20px;height:20px;border-radius:4px;background-color:#1677ff"
/> />
</div> </div>
<div <div

2
components/color-picker/demo/base.tsx

@ -1,5 +1,5 @@
import { ColorPicker, theme } from 'antd'; import { ColorPicker, theme } from 'antd';
import type { Color } from 'antd/lib/color-picker'; import type { Color } from 'antd/es/color-picker';
import React, { useState } from 'react'; import React, { useState } from 'react';
export default () => { export default () => {

8
components/color-picker/demo/format.tsx

@ -1,20 +1,22 @@
import { Col, ColorPicker, Row, Space } from 'antd'; import { Col, ColorPicker, Row, Space } from 'antd';
import type { Color } from 'antd/lib/color-picker'; import type { Color } from 'antd/es/color-picker';
import React, { useMemo, useState } from 'react'; import React, { useMemo, useState } from 'react';
export default () => { export default () => {
const [colorHex, setColorHex] = useState<Color | string>('#1677ff'); const [colorHex, setColorHex] = useState<Color | string>('#1677ff');
const [colorHsb, setColorHsb] = useState<Color | string>('hsb(215,91%,100%)'); const [colorHsb, setColorHsb] = useState<Color | string>('hsb(215, 91%, 100%)');
const [colorRgb, setColorRgb] = useState<Color | string>('rgb(22,119,255)'); const [colorRgb, setColorRgb] = useState<Color | string>('rgb(22, 119, 255)');
const hexString = useMemo( const hexString = useMemo(
() => (typeof colorHex === 'string' ? colorHex : colorHex.toHexString()), () => (typeof colorHex === 'string' ? colorHex : colorHex.toHexString()),
[colorHex], [colorHex],
); );
const hsbString = useMemo( const hsbString = useMemo(
() => (typeof colorHsb === 'string' ? colorHsb : colorHsb.toHsbString()), () => (typeof colorHsb === 'string' ? colorHsb : colorHsb.toHsbString()),
[colorHsb], [colorHsb],
); );
const rgbString = useMemo( const rgbString = useMemo(
() => (typeof colorRgb === 'string' ? colorRgb : colorRgb.toRgbString()), () => (typeof colorRgb === 'string' ? colorRgb : colorRgb.toRgbString()),
[colorRgb], [colorRgb],

2
components/color-picker/demo/pure-panel.tsx

@ -1,5 +1,5 @@
import { ColorPicker, theme } from 'antd'; import { ColorPicker, theme } from 'antd';
import type { Color } from 'antd/lib/color-picker'; import type { Color } from 'antd/es/color-picker';
import React, { useState } from 'react'; import React, { useState } from 'react';
const PureRenderColorPicker = ColorPicker._InternalPanelDoNotUseOrYouWillBeFired; const PureRenderColorPicker = ColorPicker._InternalPanelDoNotUseOrYouWillBeFired;

15
components/color-picker/demo/trigger.tsx

@ -1,11 +1,12 @@
import { ColorPicker, Space, theme } from 'antd'; import { ColorPicker, Space, theme } from 'antd';
import type { Color } from 'antd/lib/color-picker'; import type { Color } from 'antd/es/color-picker';
import React, { useMemo, useState } from 'react'; import React, { useMemo, useState } from 'react';
export default () => { export default () => {
const { token } = theme.useToken(); const { token } = theme.useToken();
const [color, setColor] = useState<Color | string>(token.colorPrimary); const [color, setColor] = useState<Color | string>(token.colorPrimary);
const genColor = useMemo(
const bgColor = useMemo<string>(
() => (typeof color === 'string' ? color : color.toHexString()), () => (typeof color === 'string' ? color : color.toHexString()),
[color], [color],
); );
@ -15,13 +16,13 @@ export default () => {
<Space> <Space>
<div <div
style={{ style={{
width: 20, width: token.sizeMD,
height: 20, height: token.sizeMD,
borderRadius: 4, borderRadius: token.borderRadiusSM,
background: genColor, backgroundColor: bgColor,
}} }}
/> />
<span>{genColor}</span> <span>{bgColor}</span>
</Space> </Space>
</ColorPicker> </ColorPicker>
); );

6
components/color-picker/index.en-US.md

@ -10,11 +10,11 @@ group:
title: Data Entry title: Data Entry
--- ---
Components providing color selection Components providing color selection, Available since `5.5.0`.
## When To Use ## When To Use
Used when the user needs to customize the color selection Used when the user needs to customize the color selection.
## Examples ## Examples
@ -29,7 +29,7 @@ Used when the user needs to customize the color selection
## API ## API
> This component is available since antd@5.5.0 > This component is available since `antd@5.5.0`.
<!-- prettier-ignore --> <!-- prettier-ignore -->
| Property | Description | Type | Default | | Property | Description | Type | Default |

2
components/color-picker/index.zh-CN.md

@ -11,7 +11,7 @@ group:
title: 数据录入 title: 数据录入
--- ---
提供颜色选取的组件 提供颜色选取的组件,自 `5.5.0` 版本开始提供该组件。
## 何时使用 ## 何时使用

8
components/color-picker/style/index.ts

@ -1,10 +1,10 @@
import type { CSSObject } from '@ant-design/cssinjs'; import type { CSSObject } from '@ant-design/cssinjs';
import type { FullToken, GenerateStyle } from '../../theme/internal'; import type { FullToken, GenerateStyle } from '../../theme/internal';
import { genComponentStyleHook, mergeToken } from '../../theme/internal'; import { genComponentStyleHook, mergeToken } from '../../theme/internal';
import genPickerStyle from './picker'; import genColorBlockStyle from './color-block';
import genInputStyle from './input'; import genInputStyle from './input';
import genPickerStyle from './picker';
import genPresetsStyle from './presets'; import genPresetsStyle from './presets';
import genColorBlockStyle from './color-block';
export interface ComponentToken {} export interface ComponentToken {}
@ -137,7 +137,7 @@ export default genComponentStyleHook('ColorPicker', (token) => {
const colorPickerSliderHeight = 8; const colorPickerSliderHeight = 8;
const ColorPickerToken = mergeToken<ColorPickerToken>(token, { const colorPickerToken = mergeToken<ColorPickerToken>(token, {
colorPickerWidth: 234, colorPickerWidth: 234,
colorPickerHandlerSize: 16, colorPickerHandlerSize: 16,
colorPickerHandlerSizeSM: 12, colorPickerHandlerSizeSM: 12,
@ -149,5 +149,5 @@ export default genComponentStyleHook('ColorPicker', (token) => {
colorPickerPreviewSize: colorPickerSliderHeight * 2 + marginSM, colorPickerPreviewSize: colorPickerSliderHeight * 2 + marginSM,
}); });
return [genColorPickerStyle(ColorPickerToken)]; return [genColorPickerStyle(colorPickerToken)];
}); });

3
components/color-picker/style/picker.ts

@ -1,7 +1,7 @@
import type { CSSObject } from '@ant-design/cssinjs'; import type { CSSObject } from '@ant-design/cssinjs';
import type { GenerateStyle } from '../../theme/internal'; import type { GenerateStyle } from '../../theme/internal';
import type { ColorPickerToken } from './index';
import genColorBlockStyle from './color-block'; import genColorBlockStyle from './color-block';
import type { ColorPickerToken } from './index';
const genPickerStyle: GenerateStyle<ColorPickerToken, CSSObject> = (token) => { const genPickerStyle: GenerateStyle<ColorPickerToken, CSSObject> = (token) => {
const { const {
@ -41,6 +41,7 @@ const genPickerStyle: GenerateStyle<ColorPickerToken, CSSObject> = (token) => {
border: `${lineWidthBold}px solid ${colorBgElevated}`, border: `${lineWidthBold}px solid ${colorBgElevated}`,
position: 'relative', position: 'relative',
borderRadius: '50%', borderRadius: '50%',
cursor: 'pointer',
boxShadow: `${colorPickerInsetShadow}, 0 0 0 1px ${colorFillSecondary}`, boxShadow: `${colorPickerInsetShadow}, 0 0 0 1px ${colorFillSecondary}`,
'&-sm': { '&-sm': {
width: colorPickerHandlerSizeSM, width: colorPickerHandlerSizeSM,

Loading…
Cancel
Save