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

[Start Coords] Fix the radius input in circle graph start coords UI #1509

Merged
merged 1 commit into from
Aug 12, 2024
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
5 changes: 5 additions & 0 deletions .changeset/light-islands-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus-editor": patch
---

[Start Coords] Fix the radius input in circle graph start coords UI
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,79 @@ describe("StartCoordSettings", () => {
expect(onChangeMock).toHaveBeenLastCalledWith(expectedCoords);
},
);

test("does not call onChange when the radius is not a number", async () => {
// Arrange
const onChangeMock = jest.fn();

render(
<StartCoordsSettings
{...defaultProps}
type="circle"
onChange={onChangeMock}
/>,
{wrapper: RenderStateRoot},
);

// Act
const input = screen.getByRole("spinbutton", {
name: "Radius:",
});
await userEvent.type(input, "-");

// Assert
expect(onChangeMock).not.toHaveBeenCalled();
});

test("does not call onChange when the radius is empty", async () => {
// Arrange
const onChangeMock = jest.fn();

render(
<StartCoordsSettings
{...defaultProps}
type="circle"
onChange={onChangeMock}
/>,
{wrapper: RenderStateRoot},
);

// Act
const input = screen.getByRole("spinbutton", {
name: "Radius:",
});
await userEvent.clear(input);

// Assert
expect(onChangeMock).not.toHaveBeenCalled();
});

test("allows the user to type a decimal radius", async () => {
// Arrange
const onChangeMock = jest.fn();

render(
<StartCoordsSettings
{...defaultProps}
type="circle"
onChange={onChangeMock}
/>,
{wrapper: RenderStateRoot},
);

// Act
const input = screen.getByRole("spinbutton", {
name: "Radius:",
});
await userEvent.clear(input);
await userEvent.type(input, "0.5");

// Assert
expect(onChangeMock).toHaveBeenCalledWith({
center: [0, 0],
radius: 0.5,
});
});
});

describe("sinusoid graph", () => {
Expand Down
43 changes: 33 additions & 10 deletions packages/perseus-editor/src/components/start-coords-circle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,33 @@ type Props = {
const StartCoordsCircle = (props: Props) => {
const {startCoords, onChange} = props;

const [radiusState, setRadiusState] = React.useState(
startCoords.radius.toString(),
);

// Update the local state when the props change. (Specifically, when
// the radius is reset from the "Use default start coordinates" button.)
React.useEffect(() => {
setRadiusState(startCoords.radius.toString());
}, [startCoords.radius]);

function handleRadiuschange(newValue: string) {
// Update the local state to update the input field.
setRadiusState(newValue);

// Assume the user is in the middle of typing. Don't update
// the props until they've finished typing a valid number.
if (isNaN(+newValue) || newValue === "" || +newValue === 0) {
return;
}

// Update the props (update the graph).
onChange({
center: startCoords.center,
radius: parseFloat(newValue),
});
}

return (
<View style={styles.tile}>
{/* Center */}
Expand All @@ -38,20 +65,15 @@ const StartCoordsCircle = (props: Props) => {
<Strut size={spacing.small_12} />

{/* Radius */}
<View style={styles.row}>
<LabelLarge>Radius:</LabelLarge>
<LabelLarge tag="label" style={styles.row}>
Radius:
<Strut size={spacing.small_12} />
<ScrolllessNumberTextField
value={startCoords.radius.toString()}
onChange={(value) => {
onChange({
center: startCoords.center,
radius: parseFloat(value),
});
}}
value={radiusState}
onChange={handleRadiuschange}
style={styles.textField}
/>
</View>
</LabelLarge>
</View>
);
};
Expand All @@ -64,6 +86,7 @@ const styles = StyleSheet.create({
borderRadius: spacing.xSmall_8,
},
row: {
display: "flex",
flexDirection: "row",
alignItems: "center",
},
Expand Down
Loading