mirror of
https://github.com/pezkuwichain/pezkuwi-apps.git
synced 2026-10-29 05:39:49 +00:00
- dev ^0.86.4 at the root and in packages/apps; directives renamed by pezkuwi-dev-migrate-eslint10. - eslint --fix with the 0.86.4 config; one fix broke syntax (typescript-eslint prefer-optional-chain turned `(isStarted && onStart) && onStart()` into `(isStarted && onStart?.();` in TxButton) and is corrected by hand. - The remaining findings fixed rather than disabled: optional chains (each suggestion reviewed), `cause` on rethrown errors, `if` instead of `a && promise` in effects, parallel instead of sequential awaits in Promise.all, Error rejections, no `delete` on arrays (the storage query cache is a Map; an upload's file list no longer keeps a hole), no `[object Object]` stringification (TFunction's second argument is text or options), dead code removed (referenda's never-shown expand button, react-query's unused CoreDescriptor).
106 lines
2.2 KiB
TypeScript
106 lines
2.2 KiB
TypeScript
// Copyright 2017-2026 @pezkuwi/react-components authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import React, { useCallback } from 'react';
|
|
|
|
import { styled } from './styled.js';
|
|
|
|
interface Props {
|
|
className?: string;
|
|
isDisabled?: boolean;
|
|
isRadio?: boolean;
|
|
label: React.ReactNode;
|
|
onChange?: (isChecked: boolean) => void;
|
|
preventDefault?: boolean;
|
|
value?: boolean;
|
|
}
|
|
|
|
function Toggle ({ className = '', isDisabled, isRadio, label, onChange, preventDefault, value }: Props): React.ReactElement<Props> {
|
|
const _onClick = useCallback(
|
|
(event: React.MouseEvent<HTMLDivElement, MouseEvent>): void => {
|
|
if (!isDisabled) {
|
|
if (preventDefault) {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
}
|
|
|
|
onChange?.(!value);
|
|
}
|
|
},
|
|
[isDisabled, onChange, preventDefault, value]
|
|
);
|
|
|
|
return (
|
|
<StyledDiv
|
|
className={`${className} ui--Toggle ${value ? 'isChecked' : ''} ${isDisabled ? 'isDisabled' : ''} ${isRadio ? 'isRadio' : ''}`}
|
|
onClick={_onClick}
|
|
>
|
|
{label && <label>{label}</label>}
|
|
<div className={`ui--Toggle-Slider ${isRadio ? 'highlight--before-border' : ''}`} />
|
|
</StyledDiv>
|
|
);
|
|
}
|
|
|
|
const StyledDiv = styled.div`
|
|
> label {
|
|
display: inline-block;
|
|
margin: 0 0.5rem !important;
|
|
}
|
|
|
|
> label,
|
|
> div {
|
|
vertical-align: middle;
|
|
}
|
|
|
|
.ui--Toggle-Slider {
|
|
background: var(--bg-toggle);
|
|
border-radius: 1.5rem;
|
|
display: inline-block;
|
|
height: 1.5rem;
|
|
position: relative;
|
|
width: 3rem;
|
|
|
|
&::before {
|
|
background: var(--bg-table);
|
|
border: 0.125rem solid var(--bg-toggle);
|
|
border-radius: 50%;
|
|
content: "";
|
|
height: 1.5rem;
|
|
left: 0;
|
|
position: absolute;
|
|
top: 0;
|
|
width: 1.5rem;
|
|
}
|
|
}
|
|
|
|
&:not(.isDisabled) {
|
|
cursor: pointer;
|
|
|
|
> label {
|
|
cursor: pointer;
|
|
}
|
|
}
|
|
|
|
&.isChecked {
|
|
&:not(.isRadio) {
|
|
.ui--Toggle-Slider:before {
|
|
transform: translateX(1.5rem);
|
|
}
|
|
}
|
|
|
|
&.isRadio {
|
|
.ui--Toggle-Slider:before {
|
|
border-width: 0.5rem;
|
|
}
|
|
}
|
|
}
|
|
|
|
&.isRadio {
|
|
.ui--Toggle-Slider {
|
|
width: 1.5rem;
|
|
}
|
|
}
|
|
`;
|
|
|
|
export default React.memo(Toggle);
|