diff --git a/src/admin/components/forms/field-types/Checkbox/Input.tsx b/src/admin/components/forms/field-types/Checkbox/Input.tsx new file mode 100644 index 0000000000..8532a8c609 --- /dev/null +++ b/src/admin/components/forms/field-types/Checkbox/Input.tsx @@ -0,0 +1,62 @@ +import React from 'react'; +import Check from '../../../icons/Check'; +import Label from '../../Label'; + +import './index.scss'; + +const baseClass = 'custom-checkbox'; + +type CheckboxInputProps = { + onToggle: React.MouseEventHandler + inputRef?: React.MutableRefObject + readOnly?: boolean + checked?: boolean + name?: string + id?: string + label?: string + required?: boolean +} +export const CheckboxInput: React.FC = (props) => { + const { + onToggle, + checked, + inputRef, + name, + id, + label, + readOnly, + required, + } = props; + + return ( + + + + + ); +}; diff --git a/src/admin/components/forms/field-types/Checkbox/index.scss b/src/admin/components/forms/field-types/Checkbox/index.scss index 094383099b..2daf65b4f2 100644 --- a/src/admin/components/forms/field-types/Checkbox/index.scss +++ b/src/admin/components/forms/field-types/Checkbox/index.scss @@ -14,6 +14,46 @@ &__error-wrap { position: relative; } +} + + +.custom-checkbox { + label { + padding-bottom: 0; + } + + input { + // hidden HTML checkbox + position: absolute; + top: 0; + left: 0; + opacity: 0; + } + + &__input { + // visible checkbox + @include formInput; + padding: 0; + line-height: 0; + position: relative; + width: $baseline; + height: $baseline; + margin-right: base(.5); + + svg { + opacity: 0; + } + } + + &--read-only { + .custom-checkbox__input { + background-color: var(--theme-elevation-100); + } + + label { + color: var(--theme-elevation-400); + } + } button { @extend %btn-reset; @@ -33,45 +73,13 @@ } } - &__input { - @include formInput; - padding: 0; - line-height: 0; - position: relative; - width: $baseline; - height: $baseline; - margin-right: base(.5); - - svg { - opacity: 0; - } - } - - input { - position: absolute; - top: 0; - left: 0; - opacity: 0; - } - &--checked { button { - .checkbox__input { + .custom-checkbox__input { svg { opacity: 1; } } } } - - &--read-only { - - .checkbox__label { - color: var(--theme-elevation-400); - } - - .checkbox__input { - background-color: var(--theme-elevation-100); - } - } } diff --git a/src/admin/components/forms/field-types/Checkbox/index.tsx b/src/admin/components/forms/field-types/Checkbox/index.tsx index 1751ebfcbd..1fdb7785d0 100644 --- a/src/admin/components/forms/field-types/Checkbox/index.tsx +++ b/src/admin/components/forms/field-types/Checkbox/index.tsx @@ -4,10 +4,10 @@ import useField from '../../useField'; import withCondition from '../../withCondition'; import Error from '../../Error'; import { checkbox } from '../../../../../fields/validations'; -import Check from '../../../icons/Check'; import FieldDescription from '../../FieldDescription'; import { Props } from './types'; import { getTranslation } from '../../../../../utilities/getTranslation'; +import { CheckboxInput } from './Input'; import './index.scss'; @@ -52,6 +52,15 @@ const Checkbox: React.FC = (props) => { condition, }); + const onToggle = useCallback(() => { + if (!readOnly) { + setValue(!value); + if (typeof onChange === 'function') onChange(!value); + } + }, [onChange, readOnly, setValue, value]); + + const fieldID = `field-${path.replace(/\./gi, '__')}`; + return (
= (props) => { message={errorMessage} />
- -