Files
PyUIBuilder/src/frameworks/tkinter/widgets/optionMenu.js

141 lines
4.6 KiB
JavaScript
Raw Normal View History

2024-09-23 19:31:30 +05:30
import Tools from "../../../canvas/constants/tools"
import { DownOutlined } from "@ant-design/icons"
import { TkinterWidgetBase} from "./base"
import { convertObjectToKeyValueString } from "../../../utils/common"
2024-09-23 19:31:30 +05:30
class OptionMenu extends TkinterWidgetBase{
2024-09-23 19:31:30 +05:30
static widgetType = "option_menu"
2025-03-11 07:23:17 +05:30
static displayName = "Option Menu"
2024-09-23 19:31:30 +05:30
constructor(props) {
super(props)
// const {layout, ...newAttrs} = this.state.attrs // Removes the layout attribute
this.minSize = {width: 50, height: 30}
2024-09-23 19:31:30 +05:30
this.state = {
...this.state,
isDropDownOpen: false,
2024-09-27 16:04:03 +05:30
widgetName: "Option menu",
size: { width: 120, height: 30 },
fitContent: { width: true, height: true },
2024-09-23 19:31:30 +05:30
attrs: {
...this.state.attrs,
2024-09-23 19:31:30 +05:30
defaultValue: {
label: "Default Value",
tool: Tools.INPUT,
value: "Select option",
onChange: ({inputs, selectedRadio}) => {
this.setAttrValue("options", {inputs, selectedRadio})
}
},
widgetOptions: {
2024-09-23 19:31:30 +05:30
label: "Options",
tool: Tools.INPUT_RADIO_LIST,
value: {inputs: ["option 1"], selectedRadio: -1},
onChange: ({inputs, selectedRadio}) => {
this.setAttrValue("widgetOptions", {inputs, selectedRadio})
2024-09-23 19:31:30 +05:30
}
}
}
}
2024-09-23 19:31:30 +05:30
}
2024-10-01 11:22:45 +05:30
2024-09-23 19:31:30 +05:30
componentDidMount(){
super.componentDidMount()
2024-10-01 11:22:45 +05:30
this.setWidgetInnerStyle("backgroundColor", "#E4E2E2")
2024-09-23 19:31:30 +05:30
}
generateCode(variableName, parent){
const config = convertObjectToKeyValueString(this.getConfigCode())
const defaultValue = this.getAttrValue("defaultValue")
const options = this.getAttrValue("widgetOptions").inputs
2024-09-30 15:30:46 +05:30
const code = [
`${variableName}_options = ${JSON.stringify(options)}`,
`${variableName}_var = tk.StringVar(value="${options.at(1) || defaultValue || ''}")`,
2024-09-30 15:30:46 +05:30
`${variableName} = tk.OptionMenu(${parent}, ${variableName}_var, *${variableName}_options)`
]
return [
2024-09-30 15:30:46 +05:30
...code,
`${variableName}.config(${config})`,
`${variableName}.${this.getLayoutCode()}`
]
}
2024-09-23 19:31:30 +05:30
getToolbarAttrs(){
const toolBarAttrs = super.getToolbarAttrs()
const attrs = this.state.attrs
2024-09-29 20:57:10 +05:30
2024-09-23 19:31:30 +05:30
return ({
id: this.__id,
widgetName: toolBarAttrs.widgetName,
checkLabel: attrs.checkLabel,
size: toolBarAttrs.size,
...attrs,
})
}
toggleDropDownOpen = () => {
this.setState((prev) => ({
isDropDownOpen: !prev.isDropDownOpen
}))
}
renderContent(){
const {inputs, selectedRadio} = this.getAttrValue("widgetOptions")
2024-09-23 19:31:30 +05:30
return (
<div className="tw-flex tw-p-1 tw-w-full tw-h-full tw-rounded-md tw-overflow-hidden"
style={this.getInnerRenderStyling()}
2024-09-23 19:31:30 +05:30
onClick={this.toggleDropDownOpen}
>
<div className="tw-flex tw-justify-between tw-gap-1">
{this.getAttrValue("defaultValue")}
<div className="tw-text-sm">
<DownOutlined />
</div>
</div>
{this.state.isDropDownOpen &&
<div className="tw-absolute tw-p-1 tw-bg-white tw-rounded-md tw-shadow-md tw-left-0
tw-w-full tw-h-fit "
style={{top: "calc(100% + 5px)"}}
>
{
inputs.map((value, index) => {
return (
<div key={index} className="tw-flex tw-gap-2 tw-w-full tw-h-full tw-place-items-center
hover:tw-bg-[#c5c5c573] tw-p-1">
2024-09-25 17:27:12 +05:30
<span className="tw-text-base" style={{color: this.state.widgetInnerStyling.foregroundColor}}>
2024-09-23 19:31:30 +05:30
{value}
</span>
</div>
)
})
}
</div>
}
</div>
)
}
}
export default OptionMenu