Hi I'm quite new to Typescript/FluentUI/PCF components.
I'm creating a Toggle Button and I'm struggling to understand how to implement it using an export class pattern.
See code below. The first implementation is using code lifted from the fluentui/controls documentation and works fine.
But my other pcf controls use a Class pattern and I'd like to keep this consistent. But, due to my lack of experience, I don't know how to implement the onChange properly so it toggles.
Can anyone help me refactor the class so it works ?
Thanks
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable prettier/prettier */
import * as React from "react";
import { DefaultButton, IBaseButtonState } from "@fluentui/react/lib/Button";
import { IIconProps, Toggle } from "@fluentui/react";
import { IUseBooleanCallbacks, useBoolean } from "@fluentui/react-hooks";
export interface IButtonElementProps {
name?: string;
disabled?: boolean;
checked?: boolean;
}
//Implementation exporting a function - taken from FluidUI example
export const ButtonElement: React.FunctionComponent<IButtonElementProps> = (props) => {
const { disabled, checked } = props;
const icon: IIconProps = { iconName: "Accept" };
const [muted, { toggle: setMuted }] = useBoolean(false);
return (
<DefaultButton
toggle
iconProps={icon}
text={muted ? "Volume muted" : "Volume unmuted"}
checked={muted || checked}
onClick={setMuted}
></DefaultButton>
);
};
//Implementation exporting class - this is the bit I can't get to work
export class ButtonControl extends React.Component<IButtonElementProps, IBaseButtonState> {
constructor(props: IButtonElementProps) {
super(props);
}
icon: IIconProps = { iconName: "AddIn" };
//muted: [boolean, IUseBooleanCallbacks] = useBoolean(false); // This breaks it
tmuted = [useBoolean(false)]; //so does this
muted: IUseBooleanCallbacks = { // this runs but I don't know if its right or understand how to use it
toggle() {
}, setFalse() {
}, setTrue() {
},
};
public render(): React.ReactNode {
// eslint-disable-next-line prettier/prettier
return <DefaultButton
toggle
iconProps={this.icon}
text={this.props.name}
checked={true} //how do I make this so it takes the value of muted
onClick={this.clicked}
></DefaultButton>;
}
private clicked(args: React.MouseEvent<HTMLAnchorElement>) {
console.log("clicked");
console.log(args);
// is this the right place and what do I put here to set the property ?
}
}