mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-06-05 21:20:40 +02:00
frontend: updating button component
This commit is contained in:
parent
8c0ad6f334
commit
60ec1569f0
@ -1,41 +1,45 @@
|
|||||||
import React, { MouseEventHandler, useEffect, useRef, useState } from 'react';
|
import React, { useEffect, useRef } from 'react';
|
||||||
import { OverlayTrigger, Badge, Button, Tooltip, ToggleButton } from 'react-bootstrap';
|
import { OverlayTrigger, Badge, Tooltip, ToggleButton } from 'react-bootstrap';
|
||||||
|
import { socket } from '../socket';
|
||||||
|
|
||||||
interface ButtonComponentProps {
|
interface ButtonComponentProps {
|
||||||
name: string;
|
name: string;
|
||||||
fullname?: string;
|
parent_path?: string;
|
||||||
value: boolean;
|
value: boolean;
|
||||||
readOnly: boolean;
|
readOnly: boolean;
|
||||||
docString: string;
|
docString: string;
|
||||||
onToggle?: MouseEventHandler;
|
|
||||||
mapping?: [string, string]; // Enforce a tuple of two strings
|
mapping?: [string, string]; // Enforce a tuple of two strings
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ButtonComponent = React.memo((props: ButtonComponentProps) => {
|
export const ButtonComponent = React.memo((props: ButtonComponentProps) => {
|
||||||
const renderCount = useRef(0);
|
const renderCount = useRef(0);
|
||||||
|
|
||||||
const [checked, setChecked] = useState(false);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
renderCount.current++;
|
renderCount.current++;
|
||||||
});
|
});
|
||||||
const { name, fullname, value, readOnly, docString, onToggle, mapping } = props;
|
const { name, parent_path: fullname, value, readOnly, docString, mapping } = props;
|
||||||
|
|
||||||
const buttonName = mapping ? (value ? mapping[0] : mapping[1]) : name;
|
const buttonName = mapping ? (value ? mapping[0] : mapping[1]) : name;
|
||||||
|
|
||||||
const tooltip = <Tooltip id="tooltip">{docString}</Tooltip>;
|
const tooltip = <Tooltip id="tooltip">{docString}</Tooltip>;
|
||||||
|
|
||||||
|
const setChecked = (checked: boolean) => {
|
||||||
|
socket.emit('frontend_update', {
|
||||||
|
name: name,
|
||||||
|
fullname: fullname,
|
||||||
|
value: checked
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={'component boolean'} id={fullname}>
|
<div className={'component boolean'} id={fullname}>
|
||||||
<p>Render count: {renderCount.current}</p>
|
<p>Render count: {renderCount.current}</p>
|
||||||
<ToggleButton
|
<ToggleButton
|
||||||
id="toggle-check"
|
id="toggle-check"
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
// variant="secondary"
|
variant={value ? 'success' : 'secondary'}
|
||||||
variant={checked ? 'success' : 'secondary'}
|
checked={value}
|
||||||
checked={checked}
|
|
||||||
value={fullname}
|
value={fullname}
|
||||||
onMouseUp={onToggle}
|
|
||||||
disabled={readOnly}
|
disabled={readOnly}
|
||||||
onChange={(e) => setChecked(e.currentTarget.checked)}>
|
onChange={(e) => setChecked(e.currentTarget.checked)}>
|
||||||
<p>{buttonName}</p>
|
<p>{buttonName}</p>
|
Loading…
x
Reference in New Issue
Block a user