mirror of
https://github.com/outline/outline.git
synced 2026-11-21 05:24:03 +03:00
* Update Switch component onChange handler to use boolean callback - Remove synthetic event handling from Switch component - Update onChange signature to (checked: boolean) => void - Update all call points across the codebase: - PublicAccess.tsx: Updated handlers for indexing, showLastModified, and published switches - Security.tsx: Created individual handlers for all security preferences - Preferences.tsx: Created individual handlers for user preferences - CollectionForm.tsx: Added createSwitchRegister helper for react-hook-form compatibility - TemplatizeDialog: Updated publish handler - DocumentMenu.tsx: Added handlers for embeds and full-width toggles - Search.tsx: Updated SearchTitlesFilter handler - Application.tsx: Added createSwitchRegister helper - Details.tsx: Updated public branding handler - Features.tsx: Created individual handlers for seamless edit and commenting - OAuthClientForm.tsx: Added createSwitchRegister helper - Maintained backward compatibility with react-hook-form - Improved type safety and code clarity * Fix ESLint floating promise errors - Add void operator to onChange call in CollectionForm.tsx - Add void operator to document.save call in DocumentMenu.tsx These changes fix the @typescript-eslint/no-floating-promises errors while maintaining the existing functionality. * Fix Switch component onChange handlers in remaining files - Updated DocumentCopy.tsx handlers to use boolean parameter - Updated Notifications.tsx to use closure pattern for event types - Updated SlackListItem.tsx to use closure pattern for event names - All TypeScript errors resolved * Refactor createSwitchRegister into utils/forms - Created shared utility function in app/utils/forms.ts - Removed duplicate implementations from CollectionForm, Application, and OAuthClientForm - Updated all usage points to use the shared utility - Improved TypeScript typing with Record<string, unknown> - Fixed imports and removed unused variables - Maintained existing functionality while reducing code duplication * Fix TypeScript errors in createSwitchRegister utility - Updated generic constraint from Record<string, unknown> to FieldValues - Added Path import from react-hook-form for proper type safety - Fixed parameter type from keyof TFormData to Path<TFormData> - Improved type compatibility with react-hook-form's UseFormRegister Resolves TypeScript compilation errors in CI pipeline. * Remove unnecessary handlePublishChange callbacks - Removed handlePublishChange wrapper in DocumentCopy.tsx - Removed handlePublishChange wrapper in TemplatizeDialog/index.tsx - Updated Switch components to use setPublish directly - Simplified code by leveraging boolean callback from Switch component Since Switch now passes boolean directly, no need for intermediate callbacks. * Address review feedback: simplify callbacks and fix fullWidth behavior 1. DocumentCopy.tsx: - Remove handleRecursiveChange callback wrapper - Use setRecursive directly with Switch component 2. DocumentMenu.tsx: - Add void user.save() to persist user preference - Add document.fullWidth = checked for optimistic update behavior Both changes leverage the boolean callback from Switch component properly. * Update Security.tsx * i18n --------- Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com> Co-authored-by: Tom Moor <tom@getoutline.com>
177 lines
4.0 KiB
TypeScript
177 lines
4.0 KiB
TypeScript
import * as RadixSwitch from "@radix-ui/react-switch";
|
|
import * as React from "react";
|
|
import styled from "styled-components";
|
|
import { s } from "@shared/styles";
|
|
import { LabelText } from "~/components/Input";
|
|
import Text from "~/components/Text";
|
|
import { undraggableOnDesktop } from "~/styles";
|
|
|
|
interface Props
|
|
extends Omit<
|
|
React.ComponentProps<typeof RadixSwitch.Root>,
|
|
"checked" | "onCheckedChange" | "onChange"
|
|
> {
|
|
/** Width of the switch. Defaults to 32. */
|
|
width?: number;
|
|
/** Height of the switch. Defaults to 18 */
|
|
height?: number;
|
|
/** An optional label for the switch */
|
|
label?: string;
|
|
/** Whether the label should be positioned on left or right. Defaults to right */
|
|
labelPosition?: "left" | "right";
|
|
/** An optional note to display below the switch */
|
|
note?: React.ReactNode;
|
|
/** Name of the input */
|
|
name?: string;
|
|
/** Whether the switch is checked */
|
|
checked?: boolean;
|
|
/** Whether the switch is disabled */
|
|
disabled?: boolean;
|
|
/** Callback when the switch state changes */
|
|
onChange?: (checked: boolean) => void;
|
|
}
|
|
|
|
function Switch(
|
|
{
|
|
width = 32,
|
|
height = 18,
|
|
labelPosition = "left",
|
|
label,
|
|
disabled,
|
|
className,
|
|
note,
|
|
checked,
|
|
onChange,
|
|
...props
|
|
}: Props,
|
|
ref: React.Ref<React.ElementRef<typeof RadixSwitch.Root>>
|
|
) {
|
|
const handleCheckedChange = React.useCallback(
|
|
(checkedState: boolean) => {
|
|
if (onChange) {
|
|
onChange(checkedState);
|
|
}
|
|
},
|
|
[onChange]
|
|
);
|
|
|
|
const component = (
|
|
<StyledSwitchRoot
|
|
ref={ref}
|
|
checked={checked}
|
|
onCheckedChange={handleCheckedChange}
|
|
disabled={disabled}
|
|
width={width}
|
|
height={height}
|
|
className={label ? undefined : className}
|
|
{...props}
|
|
>
|
|
<StyledSwitchThumb width={width} height={height} />
|
|
</StyledSwitchRoot>
|
|
);
|
|
|
|
if (label) {
|
|
return (
|
|
<Wrapper>
|
|
<Label
|
|
disabled={disabled}
|
|
htmlFor={props.id}
|
|
className={className}
|
|
$labelPosition={labelPosition}
|
|
>
|
|
{component}
|
|
<InlineLabelText>{label}</InlineLabelText>
|
|
</Label>
|
|
{note && (
|
|
<Text
|
|
type="secondary"
|
|
size="small"
|
|
style={{
|
|
paddingRight: labelPosition === "left" ? width : 0,
|
|
paddingLeft: labelPosition === "right" ? width : 0,
|
|
}}
|
|
>
|
|
{note}
|
|
</Text>
|
|
)}
|
|
</Wrapper>
|
|
);
|
|
}
|
|
|
|
return component;
|
|
}
|
|
|
|
const Wrapper = styled.div`
|
|
padding-bottom: 8px;
|
|
${undraggableOnDesktop()}
|
|
`;
|
|
|
|
const InlineLabelText = styled(LabelText)`
|
|
padding-bottom: 0;
|
|
width: 100%;
|
|
`;
|
|
|
|
const Label = styled.label<{
|
|
disabled?: boolean;
|
|
$labelPosition: "left" | "right";
|
|
}>`
|
|
display: flex;
|
|
align-items: center;
|
|
user-select: none;
|
|
gap: 8px;
|
|
|
|
${(props) =>
|
|
props.$labelPosition === "left" ? `flex-direction: row-reverse;` : ""}
|
|
${(props) => (props.disabled ? `opacity: 0.75;` : "")}
|
|
`;
|
|
|
|
const StyledSwitchRoot = styled(RadixSwitch.Root)<{
|
|
width: number;
|
|
height: number;
|
|
}>`
|
|
position: relative;
|
|
width: ${(props) => props.width}px;
|
|
height: ${(props) => props.height}px;
|
|
background-color: ${(props) => props.theme.slate};
|
|
border-radius: ${(props) => props.height}px;
|
|
border: none;
|
|
cursor: var(--pointer);
|
|
transition: background-color 0.4s;
|
|
padding: 0 4px;
|
|
flex-shrink: 0;
|
|
|
|
&:focus {
|
|
box-shadow: 0 0 1px ${s("accent")};
|
|
outline: none;
|
|
}
|
|
|
|
&[data-state="checked"] {
|
|
background-color: ${s("accent")};
|
|
}
|
|
|
|
&:disabled {
|
|
opacity: 0.75;
|
|
cursor: default;
|
|
}
|
|
`;
|
|
|
|
const StyledSwitchThumb = styled(RadixSwitch.Thumb)<{
|
|
width: number;
|
|
height: number;
|
|
}>`
|
|
display: block;
|
|
width: ${(props) => props.height - 8}px;
|
|
height: ${(props) => props.height - 8}px;
|
|
background-color: white;
|
|
border-radius: 50%;
|
|
transition: transform 0.4s;
|
|
transform: translateX(0);
|
|
will-change: transform;
|
|
|
|
&[data-state="checked"] {
|
|
transform: translateX(${(props) => props.width - props.height}px);
|
|
}
|
|
`;
|
|
|
|
export default React.forwardRef(Switch);
|