ant-design/components/color-picker/ColorPickerPanel.tsx
红果汁 6bb18b361d
feat: ColorPicker implement panelRender API (#43134)
* feat: implement `panelRender` api and add demo

* test: add test case

* docs: update doc

* fix: styles add overlayInnerStyle

* fix: variable name

* docs: reorder api

* refactor: optimize code

* test: update snapshot

* test: update snapshots

* refactor: remove memo

* refactor: rename  overlayInnerStyle to popupOverlayInner

* fix: props error

* docs: update demo
2023-06-29 11:46:11 +08:00

82 lines
2.1 KiB
TypeScript

import type { HsbaColorType } from '@rc-component/color-picker';
import type { FC } from 'react';
import React from 'react';
import Divider from '../divider';
import type { Color } from './color';
import PanelPicker from './components/PanelPicker';
import PanelPresets from './components/PanelPresets';
import { PanelPickerProvider, PanelPresetsProvider } from './context';
import type { ColorPickerBaseProps } from './interface';
interface ColorPickerPanelProps extends ColorPickerBaseProps {
onChange?: (value?: Color, type?: HsbaColorType, pickColor?: boolean) => void;
onChangeComplete?: (type?: HsbaColorType) => void;
onClear?: () => void;
}
const ColorPickerPanel: FC<ColorPickerPanelProps> = (props) => {
const {
prefixCls,
presets,
panelRender,
color,
onChange,
onClear,
onChangeComplete,
...injectProps
} = props;
const colorPickerPanelPrefixCls = `${prefixCls}-inner-content`;
// ==== Inject props ===
const panelPickerProps = {
prefixCls,
value: color,
onChange,
onClear,
onChangeComplete,
...injectProps,
};
const panelPresetsProps = React.useMemo(
() => ({
prefixCls,
value: color,
presets,
onChange,
}),
[prefixCls, color, presets, onChange],
);
// ====================== Render ======================
const innerPanel = (
<>
<PanelPicker />
{Array.isArray(presets) && <Divider className={`${colorPickerPanelPrefixCls}-divider`} />}
<PanelPresets />
</>
);
return (
<PanelPickerProvider value={panelPickerProps}>
<PanelPresetsProvider value={panelPresetsProps}>
<div className={colorPickerPanelPrefixCls}>
{typeof panelRender === 'function'
? panelRender(innerPanel, {
components: {
Picker: PanelPicker,
Presets: PanelPresets,
},
})
: innerPanel}
</div>
</PanelPresetsProvider>
</PanelPickerProvider>
);
};
if (process.env.NODE_ENV !== 'production') {
ColorPickerPanel.displayName = 'ColorPickerPanel';
}
export default ColorPickerPanel;