From f66e38291f311851caaa8613659dba1f1ce154f8 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Sun, 29 May 2022 23:27:13 +0800 Subject: [PATCH] feat: use primer checkbox --- src/components/SimpleToggleField.tsx | 92 +++++++++++++--------------- src/utils/config/helper.ts | 2 + 2 files changed, 45 insertions(+), 49 deletions(-) diff --git a/src/components/SimpleToggleField.tsx b/src/components/SimpleToggleField.tsx index 529a584..e7e0773 100644 --- a/src/components/SimpleToggleField.tsx +++ b/src/components/SimpleToggleField.tsx @@ -1,14 +1,13 @@ +import { Checkbox, FormControl } from '@primer/react' import { useConfigs } from 'containers/ConfigsContext' import * as React from 'react' -import { Config } from 'utils/config/helper' -import { Field } from './settings/Field' +import { Config, ConfigKeys } from 'utils/config/helper' -export type SimpleField = { +export type SimpleField = { key: Key label: string wikiLink?: string tooltip?: string - description?: string disabled?: boolean overwrite?: { value: (value: Config[Key]) => boolean @@ -16,59 +15,54 @@ export type SimpleField = { } } -type Props = { +type Props = { field: SimpleField - - onChange?(): void } -export function SimpleToggleField({ field, onChange }: Props) { +export function SimpleToggleField({ field }: Props) { const { overwrite } = field const configContext = useConfigs() const value = configContext.value[field.key] + + const checked = React.useMemo( + () => (overwrite ? overwrite.value(value) : Boolean(value)), + [overwrite, value], + ) + + const handleChange = React.useCallback( + (checked: boolean) => { + const enabled = checked + configContext.onChange({ [field.key]: overwrite ? overwrite.onChange(enabled) : enabled }) + }, + [field.key, overwrite, configContext], + ) + return ( - - {field.label}{' '} - {field.wikiLink ? ( - - (?) - - ) : field.description ? ( -

- {field.description} -

- ) : ( - field.tooltip && ( - - (?) - - ) - )} - - } - className={'field-checkbox'} - checkbox - > - { - const enabled = e.currentTarget.checked - configContext.onChange({ [field.key]: overwrite ? overwrite.onChange(enabled) : enabled }) - if (onChange) onChange() + + handleChange(e.target.checked)} + checked={checked} /> -
+ + {field.label} + {(field.wikiLink || field.tooltip) && ' '} + {field.wikiLink ? ( + + (?) + + ) : ( + field.tooltip && ( + + (?) + + ) + )} + + ) } diff --git a/src/utils/config/helper.ts b/src/utils/config/helper.ts index ac69b04..bd86a7d 100644 --- a/src/utils/config/helper.ts +++ b/src/utils/config/helper.ts @@ -21,6 +21,8 @@ export type Config = { restoreExpandedFolders: boolean } +export type ConfigKeys = keyof Config + enum configKeys { sideBarWidth = 'sideBarWidth', shortcut = 'shortcut',