Skip to content

Commit a1b61a1

Browse files
committed
add switch component
1 parent f37a722 commit a1b61a1

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

lib/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ export { PushButton } from "./normal-components/PushButton"
3939
export { Crystal } from "./normal-components/Crystal"
4040
export { Transistor } from "./normal-components/Transistor"
4141
export { Mosfet } from "./normal-components/Mosfet"
42+
export { Switch } from "./normal-components/Switch"

lib/components/normal-components/Switch.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ import type { BaseSymbolName } from "lib/utils/constants"
44

55
export class Switch extends NormalComponent<typeof switchProps> {
66
get config() {
7-
const baseSymbolName: BaseSymbolName =
8-
this.props.type === "spst"
9-
? "SPST_switch"
10-
: this.props.type === "spdt"
11-
? "SPDT_switch"
12-
: this.props.type === "dpst"
13-
? "dpst_switch"
14-
: "dpdt_switch"
7+
let baseSymbolName: BaseSymbolName
8+
9+
if (this.props.type === "spst") {
10+
baseSymbolName = "SPST_switch"
11+
} else if (this.props.type === "spdt") {
12+
baseSymbolName = "SPDT_switch"
13+
} else if (this.props.type === "dpst") {
14+
baseSymbolName = "dpst_switch"
15+
} else {
16+
baseSymbolName = "dpdt_switch"
17+
}
1518

1619
return {
1720
componentName: "Switch",
@@ -26,12 +29,11 @@ export class Switch extends NormalComponent<typeof switchProps> {
2629
const { _parsedProps: props } = this
2730

2831
const source_component = db.source_component.insert({
29-
ftype: "switch",
32+
ftype: "simple_switch",
3033
name: props.name,
3134
switch_type: props.type,
3235
is_normally_closed: props.isNormallyClosed,
3336
} as any)
34-
3537
this.source_component_id = source_component.source_component_id
3638
}
3739
}

lib/fiber/intrinsic-jsx.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export interface TscircuitElements {
4747
resonator: Props.ResonatorProps
4848
subcircuit: Props.SubcircuitGroupProps
4949
transistor: Props.TransistorProps
50+
switch: Props.SwitchProps
5051
mosfet: Props.MosfetProps
5152
jscad: any
5253
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { it, expect } from "bun:test"
2+
import { getTestFixture } from "tests/fixtures/get-test-fixture"
3+
4+
it("should render an switch", async () => {
5+
const { circuit } = getTestFixture()
6+
circuit.add(
7+
<board width="10mm" height="10mm">
8+
<switch name="switch" type="spst" />
9+
</board>,
10+
)
11+
circuit.render()
12+
expect(circuit).toMatchSchematicSnapshot(import.meta.path)
13+
})

0 commit comments

Comments
 (0)