Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SwatchColorPicker] add a renderfunction as props to override cell content #28064

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "add onRenderCellContent to override cell internals",
"packageName": "@fluentui/react",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { useId } from '@fluentui/react-hooks';
import { SwatchColorPicker } from '@fluentui/react/lib/SwatchColorPicker';
import { IColorCellProps, SwatchColorPicker } from '@fluentui/react/lib/SwatchColorPicker';

const colorCellsExample1 = [
{ id: 'a', label: 'orange', color: '#ca5010' },
Expand All @@ -24,6 +24,17 @@ const colorCellsExample2 = [
{ id: 'l', label: 'gray20', color: '#69797e' },
];

const colorCellsExample3 = [
{ id: 'a', label: 'redBlueGradient', color: 'linear-gradient(0, red, blue)' },
{ id: 'b', label: 'greenGradient', color: 'linear-gradient(0, grey, green)' },
{ id: 'c', label: 'yellowGradient', color: 'linear-gradient(0, grey, yellow)' },
{ id: 'd', label: 'magentaGradient', color: 'linear-gradient(0, grey, magenta)' },
{ id: 'e', label: 'cyanGradient', color: 'linear-gradient(0, #038387, #ca5010)' },
{ id: 'f', label: 'ygGradient', color: 'linear-gradient(0, #8cbd18, #69797e)' },
{ id: 'g', label: 'grayGreen', color: 'linear-gradient(0, #0b6a0b, #69797e)' },
{ id: 'h', label: 'gray', color: '#7a7574' },
];

export const SwatchColorPickerBasicExample: React.FunctionComponent = () => {
const [previewColor, setPreviewColor] = React.useState<string>();
const baseId = useId('colorpicker');
Expand All @@ -32,6 +43,19 @@ export const SwatchColorPickerBasicExample: React.FunctionComponent = () => {
setPreviewColor(color!);
};

const renderCustomCellContent = (cellProps: IColorCellProps) => {
return (
<div
style={{
width: '100%',
height: '100%',
borderRadius: '50%',
background: cellProps.color,
}}
/>
);
};

return (
<div>
<div id={`${baseId}-circle`}>Simple circle swatch color picker:</div>
Expand Down Expand Up @@ -82,6 +106,17 @@ export const SwatchColorPickerBasicExample: React.FunctionComponent = () => {
colorCells={colorCellsExample2}
aria-labelledby={`${baseId}-grid`}
/>
<div id={`${baseId}-custom-content`}>Swatch color picker with gradient colors:</div>
<SwatchColorPicker
columnCount={4}
cellHeight={40}
cellWidth={40}
cellShape={'circle'}
colorCells={colorCellsExample3}
aria-labelledby={`${baseId}-custom-content`}
// eslint-disable-next-line react/jsx-no-bind
onRenderColorCellContent={renderCustomCellContent}
/>
<div id={`${baseId}-disabled`}>Simple disabled circle swatch color picker:</div>
<SwatchColorPicker
disabled
Expand Down
2 changes: 2 additions & 0 deletions packages/react/etc/react.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3582,6 +3582,7 @@ export interface IColorPickerGridCellProps {
// (undocumented)
onMouseLeave?: (ev: React_2.MouseEvent<HTMLButtonElement>) => void;
onMouseMove?: (ev: React_2.MouseEvent<HTMLButtonElement>) => boolean;
onRenderColorCellContent?: IRenderFunction<IColorCellProps>;
// (undocumented)
onWheel?: (ev: React_2.MouseEvent<HTMLButtonElement>) => void;
selected: boolean;
Expand Down Expand Up @@ -9080,6 +9081,7 @@ export interface ISwatchColorPickerProps extends React_2.RefAttributes<HTMLEleme
// @deprecated (undocumented)
onColorChanged?: (id?: string, color?: string) => void;
onRenderColorCell?: IRenderFunction<IColorCellProps>;
onRenderColorCellContent?: IRenderFunction<IColorCellProps>;
selectedId?: string;
shouldFocusCircularNavigate?: boolean;
styles?: IStyleFunctionOrObject<ISwatchColorPickerStyleProps, ISwatchColorPickerStyles>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const ColorPickerGridCellBase: React.FunctionComponent<IColorPickerGridCe
});

// Render the core of a color cell
const onRenderColorOption = (colorOption: IColorCellProps): JSX.Element => {
const renderColorOption = (colorOption: IColorCellProps): JSX.Element => {
const svgClassName = classNames.svg;

// Build an SVG for the cell with the given shape and color properties
Expand All @@ -119,6 +119,11 @@ export const ColorPickerGridCellBase: React.FunctionComponent<IColorPickerGridCe
);
};

const onRenderItem = (option: IColorCellProps): JSX.Element => {
const { onRenderColorCellContent = renderColorOption } = props;
return onRenderColorCellContent(option, renderColorOption) as JSX.Element;
};

const cellSemantics = isRadio
? {
role: 'radio',
Expand All @@ -138,7 +143,7 @@ export const ColorPickerGridCellBase: React.FunctionComponent<IColorPickerGridCe
disabled={disabled}
{...cellSemantics}
// eslint-disable-next-line react/jsx-no-bind
onRenderItem={onRenderColorOption}
onRenderItem={onRenderItem}
onClick={onClick}
onHover={onHover}
onFocus={onFocus}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import type { IStyle, ITheme } from '../../Styling';
import type { IStyleFunctionOrObject } from '../../Utilities';
import type { IRenderFunction, IStyleFunctionOrObject } from '../../Utilities';

/**
* {@docCategory SwatchColorPicker}
Expand Down Expand Up @@ -113,6 +113,11 @@ export interface IColorPickerGridCellProps {
onWheel?: (ev: React.MouseEvent<HTMLButtonElement>) => void;

onKeyDown?: (ev: React.KeyboardEvent<HTMLButtonElement>) => void;

/**
* Custom render function for rendering internal content of the color cell.
*/
onRenderColorCellContent?: IRenderFunction<IColorCellProps>;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const SwatchColorPickerBase: React.FunctionComponent<ISwatchColorPickerPr
cellHeight,
cellWidth,
cellBorderWidth,
onRenderColorCellContent,
} = props;

/**
Expand Down Expand Up @@ -321,6 +322,7 @@ export const SwatchColorPickerBase: React.FunctionComponent<ISwatchColorPickerPr
onMouseLeave={onMouseLeave}
onWheel={setNavigationTimeout}
onKeyDown={onKeyDown}
onRenderColorCellContent={onRenderColorCellContent}
height={cellHeight}
width={cellWidth}
borderWidth={cellBorderWidth}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,17 @@ export interface ISwatchColorPickerProps extends React.RefAttributes<HTMLElement
onCellFocused?: (id?: string, color?: string, event?: React.FormEvent<HTMLButtonElement>) => void;

/**
* Custom render function for the color cell
* Custom render function for the color cell.
* This can replace the entire button element, including the default focus and hover states.
*/
onRenderColorCell?: IRenderFunction<IColorCellProps>;

/**
* Custom render function for inner content of the color cell.
* This will retain the cell's default button behavior and overrides just the inner content.
*/
onRenderColorCellContent?: IRenderFunction<IColorCellProps>;

/**
* Whether the control is disabled.
*/
Expand Down