Skip to content

Commit

Permalink
add switch component
Browse files Browse the repository at this point in the history
  • Loading branch information
techmannih committed Feb 12, 2025
1 parent f37a722 commit a1b61a1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ export { PushButton } from "./normal-components/PushButton"
export { Crystal } from "./normal-components/Crystal"
export { Transistor } from "./normal-components/Transistor"
export { Mosfet } from "./normal-components/Mosfet"
export { Switch } from "./normal-components/Switch"
22 changes: 12 additions & 10 deletions lib/components/normal-components/Switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import type { BaseSymbolName } from "lib/utils/constants"

export class Switch extends NormalComponent<typeof switchProps> {
get config() {
const baseSymbolName: BaseSymbolName =
this.props.type === "spst"
? "SPST_switch"
: this.props.type === "spdt"
? "SPDT_switch"
: this.props.type === "dpst"
? "dpst_switch"
: "dpdt_switch"
let baseSymbolName: BaseSymbolName

if (this.props.type === "spst") {
baseSymbolName = "SPST_switch"
} else if (this.props.type === "spdt") {
baseSymbolName = "SPDT_switch"
} else if (this.props.type === "dpst") {
baseSymbolName = "dpst_switch"
} else {
baseSymbolName = "dpdt_switch"
}

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

const source_component = db.source_component.insert({
ftype: "switch",
ftype: "simple_switch",
name: props.name,
switch_type: props.type,
is_normally_closed: props.isNormallyClosed,
} as any)

this.source_component_id = source_component.source_component_id
}
}
1 change: 1 addition & 0 deletions lib/fiber/intrinsic-jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface TscircuitElements {
resonator: Props.ResonatorProps
subcircuit: Props.SubcircuitGroupProps
transistor: Props.TransistorProps
switch: Props.SwitchProps
mosfet: Props.MosfetProps
jscad: any
}
Expand Down
13 changes: 13 additions & 0 deletions tests/components/normal-components/switch.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { it, expect } from "bun:test"
import { getTestFixture } from "tests/fixtures/get-test-fixture"

it("should render an switch", async () => {
const { circuit } = getTestFixture()
circuit.add(
<board width="10mm" height="10mm">
<switch name="switch" type="spst" />
</board>,
)
circuit.render()
expect(circuit).toMatchSchematicSnapshot(import.meta.path)
})

0 comments on commit a1b61a1

Please sign in to comment.