Skip to content

Commit

Permalink
Updated ChoiceGroup to allow callbacks to define one prop type (#25475)
Browse files Browse the repository at this point in the history
* Updated to ease defining callbacks of one prop type

* yarn change
  • Loading branch information
GeoffCoxMSFT authored Nov 3, 2022
1 parent 6bfd92e commit f5a12b3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Make callbacks able to take one param type or the other rather than requiring both.",
"packageName": "@fluentui/react",
"email": "[email protected]",
"dependentChangeType": "patch"
}
10 changes: 5 additions & 5 deletions packages/react/etc/react.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3348,8 +3348,8 @@ export interface IChoiceGroupOption extends Omit<React_2.InputHTMLAttributes<HTM
imageSrc?: string;
key: string;
labelId?: string;
onRenderField?: IRenderFunction<IChoiceGroupOption | IChoiceGroupOptionProps>;
onRenderLabel?: IRenderFunction<IChoiceGroupOption | IChoiceGroupOptionProps>;
onRenderField?: IRenderFunction<IChoiceGroupOption & IChoiceGroupOptionProps>;
onRenderLabel?: IRenderFunction<IChoiceGroupOption & IChoiceGroupOptionProps>;
selectedImageSrc?: string;
styles?: IStyleFunctionOrObject<IChoiceGroupOptionStyleProps, IChoiceGroupOptionStyles>;
text: string;
Expand All @@ -3364,9 +3364,9 @@ export interface IChoiceGroupOptionProps extends Omit<IChoiceGroupOption, 'key'>
itemKey: string;
key?: string;
name?: string;
onBlur?: (ev?: React_2.FocusEvent<HTMLElement>, props?: IChoiceGroupOption | IChoiceGroupOptionProps) => void;
onChange?: (evt?: React_2.FormEvent<HTMLElement | HTMLInputElement>, props?: IChoiceGroupOption | IChoiceGroupOptionProps) => void;
onFocus?: (ev?: React_2.FocusEvent<HTMLElement | HTMLInputElement>, props?: IChoiceGroupOption | IChoiceGroupOptionProps) => void | undefined;
onBlur?: (ev?: React_2.FocusEvent<HTMLElement>, props?: IChoiceGroupOption & IChoiceGroupOptionProps) => void;
onChange?: (evt?: React_2.FormEvent<HTMLElement | HTMLInputElement>, props?: IChoiceGroupOption & IChoiceGroupOptionProps) => void;
onFocus?: (ev?: React_2.FocusEvent<HTMLElement | HTMLInputElement>, props?: IChoiceGroupOption & IChoiceGroupOptionProps) => void | undefined;
required?: boolean;
theme?: ITheme;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ export interface IChoiceGroupOption extends Omit<React.InputHTMLAttributes<HTMLE
/**
* Used to customize option rendering.
*/
onRenderField?: IRenderFunction<IChoiceGroupOption | IChoiceGroupOptionProps>;
onRenderField?: IRenderFunction<IChoiceGroupOption & IChoiceGroupOptionProps>;

/**
* Used to customize label rendering.
*/
onRenderLabel?: IRenderFunction<IChoiceGroupOption | IChoiceGroupOptionProps>;
onRenderLabel?: IRenderFunction<IChoiceGroupOption & IChoiceGroupOptionProps>;

/**
* Props for an icon to display with this option.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const ChoiceGroupOptionBase: React.FunctionComponent<IChoiceGroupOptionPr
? composeRenderFunction(props.onRenderLabel, defaultOnRenderLabel)
: defaultOnRenderLabel;

const label = onRenderLabel(props);
const label = onRenderLabel({ ...props, key: props.itemKey });

return (
<label htmlFor={id} className={classNames.field}>
Expand Down Expand Up @@ -102,15 +102,15 @@ export const ChoiceGroupOptionBase: React.FunctionComponent<IChoiceGroupOptionPr
const { onRenderField = defaultOnRenderField } = props;

const onChange = (evt: React.FormEvent<HTMLInputElement>): void => {
props.onChange?.(evt, props);
props.onChange?.(evt, { ...props, key: props.itemKey });
};

const onBlur = (evt: React.FocusEvent<HTMLElement>) => {
props.onBlur?.(evt);
};

const onFocus = (evt: React.FocusEvent<HTMLElement>) => {
props.onFocus?.(evt, props);
props.onFocus?.(evt, { ...props, key: props.itemKey });
};

return (
Expand All @@ -130,7 +130,7 @@ export const ChoiceGroupOptionBase: React.FunctionComponent<IChoiceGroupOptionPr
onFocus={onFocus}
onBlur={onBlur}
/>
{onRenderField(props, defaultOnRenderField)}
{onRenderField({ ...props, key: props.itemKey }, defaultOnRenderField)}
</div>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ export interface IChoiceGroupOptionProps extends Omit<IChoiceGroupOption, 'key'>
*/
onChange?: (
evt?: React.FormEvent<HTMLElement | HTMLInputElement>,
props?: IChoiceGroupOption | IChoiceGroupOptionProps,
props?: IChoiceGroupOption & IChoiceGroupOptionProps,
) => void;

/**
* Callback for the ChoiceGroup creating the option to be notified when the choice has received focus.
*/
onFocus?: (
ev?: React.FocusEvent<HTMLElement | HTMLInputElement>,
props?: IChoiceGroupOption | IChoiceGroupOptionProps,
props?: IChoiceGroupOption & IChoiceGroupOptionProps,
) => void | undefined;

/**
* Callback for the ChoiceGroup creating the option to be notified when the choice has lost focus.
*/
onBlur?: (ev?: React.FocusEvent<HTMLElement>, props?: IChoiceGroupOption | IChoiceGroupOptionProps) => void;
onBlur?: (ev?: React.FocusEvent<HTMLElement>, props?: IChoiceGroupOption & IChoiceGroupOptionProps) => void;

/**
* Indicates if the ChoiceGroupOption should appear focused, visually
Expand Down

0 comments on commit f5a12b3

Please sign in to comment.