In the following code, I have a TextField that I want to force all UpperCase letters for the user when they are typing, as well as store the text that the user inputted. I can make the TextField all UpperCase but I wont be able to submit to Excel. I can make the TextField submit to Excel but then I can't have it change to all UpperCase. I have the UpperCase working and commented the submit to Excel codes out. How do I merge these codes together or value and onChange? I already have a function for two calls for onChange and tried to include the third line and it did not work.
import * as React from "react";
import { TextField, PrimaryButton } from 'office-ui-fabric-react/lib/';
export interface ParentState {
[key: string]: ParentState[keyof ParentState];
//dataGoToExcel?;
multiline: boolean;
descriptionVal: string;
};
export default class ParentComponent extends React.Component<{}, ParentState> {
constructor(props, context) {
super(props, context);
this.state = {
//dataGoToExcel: '',
multiline: false,
descriptionVal: '',
};
}
handleChange2 = (event) => {
this.setState({dataGoToExcel: event.target.value})
};
addToBOM = async () => {
try {
await Excel.run(async context => {
const range = context.workbook.getSelectedRange();
// const range1 = context.workbook.getActiveCell();
range.load("address");
// let newRange = (this.state.dataGoToExcel);
// range1.values = newRange;
range.format.fill.color = "yellow";
await context.sync();
console.log(`The range address was ${range.address}.`);
});
} catch (error) {
console.error(error);
}
this.setState({
//dataGoToExcel: '',
descriptionVal: '',
})
};
render(){
this.handleChange = this.handleChange.bind(this);
return(
<div>
<TextField
label="Data to Go to Excel"
type="text"
styles={{ root: { width: 225 } }}
onChange={this.twoCalls}
//onChange={this.handleChange2}
value={this.state["descriptionVal"]}
//value={this.state.dataGoToExcel}
multiline={this.state.multiline}
/>
<PrimaryButton
text="Enter"
onClick={this.addToBOM}
/>
</div>
);
}
twoCalls2 = () => { //this currently isnt running
//this.state.dataGoToExcel();
this.state["descriptionVal"];
}
twoCalls = (e: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, newText: string): void => {
this.handleChange("descriptionVal")(e);
this._onChange(e, newText);
}
private _onChange = (e: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, newText: string): void => {
const newMultiline = newText.length > 25;
console.log(e)
if (newMultiline !== this.state.multiline) {
this.setState({ multiline: newMultiline });
}
};
handleChange = (field: string) => (event: any) => {
const fieldVal = event.target.value.toUpperCase();
this.setState({ [field]: fieldVal });
};
}
After Zohaib's answer, I added className and css but it is not working. It is only adding uppercase to the label. Here is the link for TextField properties. https://developer.microsoft.com/en-us/fluentui#/controls/web/textfield
<TextField
className="uppercase"
label="Data to Go to Excel"
type="text"
styles={{ root: { width: 225 } }}
onChange={this.handleChange2}
value={this.state.dataGoToExcel}
multiline={this.state.multiline}
/>
In taskpane.css
.uppercase {
text-transform: uppercase;
}
Figured it out. I was using declaring descriptionVal as a string when I should have put a "?" in the interface ParentState.
import * as React from "react";
import { TextField, PrimaryButton } from 'office-ui-fabric-react/lib/';
export interface ParentState {
[key: string]: ParentState[keyof ParentState];
multiline: boolean;
descriptionVal?;
};
export default class ParentComponent extends React.Component<{}, ParentState> {
constructor(props, context) {
super(props, context);
this.state = {
multiline: false,
descriptionVal: '',
};
}
addToBOM = async () => {
try {
await Excel.run(async context => {
const range = context.workbook.getSelectedRange();
const range1 = context.workbook.getActiveCell();
range.load("address");
let newRange = (this.state.descriptionVal);
range1.values = newRange;
range.format.fill.color = "yellow";
await context.sync();
console.log(`The range address was ${range.address}.`);
});
} catch (error) {
console.error(error);
}
this.setState({
descriptionVal: '',
})
};
render(){
this.handleChange = this.handleChange.bind(this);
return(
<div >
<TextField
label="Data to Go to Excel"
type="text"
styles={{ root: { width: 225 } }}
onChange={this.twoCalls}
value={this.state["descriptionVal"]}
multiline={this.state.multiline}
/>
<PrimaryButton
text="Enter"
onClick={this.addToBOM}
/>
</div>
);
}
twoCalls2 = () => {
this.state.dataGoToExcel();
this.state["descriptionVal"];
}
twoCalls = (e: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, newText: string): void => {
this.handleChange("descriptionVal")(e);
this._onChange(e, newText);
}
private _onChange = (e: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, newText: string): void => {
const newMultiline = newText.length > 25;
console.log(e)
if (newMultiline !== this.state.multiline) {
this.setState({ multiline: newMultiline });
}
};
handleChange = (field: string) => (event: any) => {
const fieldVal = event.target.value.toUpperCase();
this.setState({ [field]: fieldVal });
};
}