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

Fix ExpressionInput undo #922

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
89 changes: 30 additions & 59 deletions src/components/input/ExpressionInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type Props = {

type State = {
focused: boolean;
editing: boolean;
editedValue: string;
valid: boolean;
resetCounter: number;
};

class Input extends Component<Props, State> {
Expand All @@ -35,58 +35,32 @@ class Input extends Component<Props, State> {
super(props);
this.state = {
focused: false,
editing: false,
editedValue: this.props.number.expr.toString()
valid: true,
resetCounter: 0
};
this.inputElemRef = React.createRef<HTMLInputElement>();
}

unfocusedMode() {
this.setState({
focused: false,
editing: false,
editedValue: this.props.number.expr.toString()
focused: false
});
}

focusedMode() {
this.setState({
focused: true,
editing: false,
editedValue: this.props.number.expr.toString()
});
this.inputElemRef.current!.value = this.props.number.expr.toString();
this.inputElemRef.current!.select();
}

editingMode() {
this.setState({
focused: true,
editing: true
focused: true
});
}

getDisplayStr(): string {
if (this.state.editing) {
return this.state.editedValue;
} else {
if (this.state.focused) {
return this.props.number.expr.toString();
} else {
return this.getRoundedStr();
}
}
}

getRoundedStr(): string {
return this.props.number.expr.toString();
}

getValid(): boolean {
try {
if (this.state.editing) {
if (this.state.focused) {
const newNode = this.props.number.validate(
math.parse(this.state.editedValue)
math.parse(this.inputElemRef.current!.value)
);
return newNode !== undefined;
} else {
Expand All @@ -96,6 +70,13 @@ class Input extends Component<Props, State> {
return false;
}
}
updateValid(): void {
this.setState({ valid: this.getValid() });
}
componentDidMount(): void {
this.inputElemRef.current!.defaultValue = this.getRoundedStr();
this.updateValid();
}
componentDidUpdate(
prevProps: Readonly<Props>,
_prevState: Readonly<State>,
Expand All @@ -106,9 +87,13 @@ class Input extends Component<Props, State> {
}
if (!prevProps.number.expr.equals(this.props.number.expr)) {
// if the value has changed from the outside, make sure it is no longer
// focused so concise precision is shown.
// focused.
this.unfocusedMode();
}
if (_prevState.resetCounter !== this.state.resetCounter) {
this.inputElemRef.current!.defaultValue = this.getRoundedStr();
this.updateValid();
}
}

render() {
Expand Down Expand Up @@ -146,12 +131,15 @@ class Input extends Component<Props, State> {
)}
{this.props.title instanceof Function && this.props.title()}
<input
// Remove and re-mount the input component to clear its undo history
// every time this updates
key={this.state.resetCounter}
ref={this.inputElemRef}
type="text"
className={
styles.Number +
(showNumberWhenDisabled ? " " + styles.ShowWhenDisabled : "") +
(this.getValid() ? " " : " " + styles.Invalid)
(this.state.valid ? " " : " " + styles.Invalid)
}
style={{
minWidth: `${characters}ch`,
Expand All @@ -165,37 +153,20 @@ class Input extends Component<Props, State> {
}}
onBlur={(_e) => {
const newNode = this.props.number.validate(
math.parse(this.state.editedValue)
math.parse(this.inputElemRef.current!.value)
);
if (newNode !== undefined) {
this.props.number.set(newNode);
}
this.unfocusedMode();
}}
onChange={(e) => {
if (!this.state.editing) {
this.editingMode();
}
this.setState({
editedValue: e.target.value
});
e.preventDefault();
this.setState((prev) => ({
resetCounter: prev.resetCounter + 1,
focused: false
}));
}}
onChange={() => this.updateValid()}
onKeyDown={(e) => {
if (e.key == "Enter") {
this.inputElemRef.current?.blur();
// let newNumber = parseFloat(this.state.editedValue);
// if (!Number.isNaN(newNumber)) {
// this.props.setNumber(newNumber);
// }
// this.unfocusedMode();
}
}}
value={this.getDisplayStr()}
onMouseDown={(e) => {
if (!this.state.focused) {
this.focusedMode();
e.preventDefault();
}
}}
autoComplete="off"
Expand Down
Loading