From 5bcaedb556414fea15bc8bc07214d6b8ecc261e3 Mon Sep 17 00:00:00 2001 From: Michael Schramm Date: Mon, 3 Jan 2022 09:00:31 +0100 Subject: [PATCH] add checkbox field type fixes https://github.com/ohmyform/ohmyform/issues/138 --- CHANGELOG.md | 1 + components/form/admin/submission.values.tsx | 11 +++ components/form/admin/types/checkbox.type.tsx | 77 +++++++++++++++++++ components/form/admin/types/index.ts | 2 + components/form/types/checkbox.type.tsx | 41 ++++++++++ components/form/types/index.ts | 2 + components/styled/checkbox.tsx | 38 +++++++++ 7 files changed, 172 insertions(+) create mode 100644 components/form/admin/types/checkbox.type.tsx create mode 100644 components/form/types/checkbox.type.tsx create mode 100644 components/styled/checkbox.tsx diff --git a/CHANGELOG.md b/CHANGELOG.md index f99ecf0..1381b50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - field logic - add enviroment config - anonymous form submissions (fixes https://github.com/ohmyform/ohmyform/issues/108) +- checkbox field type (fixed https://github.com/ohmyform/ohmyform/issues/138) ### Changed diff --git a/components/form/admin/submission.values.tsx b/components/form/admin/submission.values.tsx index 45928f0..10d21fc 100644 --- a/components/form/admin/submission.values.tsx +++ b/components/form/admin/submission.values.tsx @@ -30,9 +30,20 @@ export const SubmissionValues: React.FC = (props) => { { title: t('submission:value'), render(_, row) { + console.log('row.value', row.value) + try { const data = JSON.parse(row.value) as { value: string } + if (Array.isArray(data.value)) { + return ( + + ) + } return data.value } catch (e) { return row.value diff --git a/components/form/admin/types/checkbox.type.tsx b/components/form/admin/types/checkbox.type.tsx new file mode 100644 index 0000000..1a07e8f --- /dev/null +++ b/components/form/admin/types/checkbox.type.tsx @@ -0,0 +1,77 @@ +import { Button, Col, Form, Input, Row } from 'antd' +import React from 'react' +import { useTranslation } from 'react-i18next' +import { AdminFieldTypeProps } from './type.props' + +export const CheckboxType: React.FC = (props) => { + const { t } = useTranslation() + + return ( +
+ + + + + + {(fields, { add, remove }) => { + return ( +
+ {fields.map((field, index) => ( + + + + + + + + + + + + + + + + + + ))} + + + + +
+ ) + }} +
+
+ ) +} diff --git a/components/form/admin/types/index.ts b/components/form/admin/types/index.ts index f66ec12..9b36664 100644 --- a/components/form/admin/types/index.ts +++ b/components/form/admin/types/index.ts @@ -1,4 +1,5 @@ import React from 'react' +import { CheckboxType } from './checkbox.type' import { DateType } from './date.type' import { DropdownType } from './dropdown.type' import { EmailType } from './email.type' @@ -18,6 +19,7 @@ export const adminTypes: { } = { date: DateType, dropdown: DropdownType, + checkbox: CheckboxType, email: EmailType, hidden: HiddenType, link: LinkType, diff --git a/components/form/types/checkbox.type.tsx b/components/form/types/checkbox.type.tsx new file mode 100644 index 0000000..6b68513 --- /dev/null +++ b/components/form/types/checkbox.type.tsx @@ -0,0 +1,41 @@ +import { Checkbox, Form } from 'antd' +import React from 'react' +import { useTranslation } from 'react-i18next' +import { StyledCheckbox } from '../../styled/checkbox' +import { FieldTypeProps } from './type.props' + +export const CheckboxType: React.FC = ({ field, design, urlValue }) => { + const { t } = useTranslation() + + let initialValue: string = undefined + + if (field.value) { + initialValue = field.value + } + + if (urlValue) { + initialValue = urlValue + } + + return ( +
+ option.value) + .find((value) => value === initialValue)} + > + + {field.options + .filter((option) => option.key === null) + .map((option) => ( + + {option.title || option.value} + + ))} + + +
+ ) +} diff --git a/components/form/types/index.ts b/components/form/types/index.ts index 8fb46fc..f61ce80 100644 --- a/components/form/types/index.ts +++ b/components/form/types/index.ts @@ -1,4 +1,5 @@ import React from 'react' +import { CheckboxType } from './checkbox.type' import { DateType } from './date.type' import { DropdownType } from './dropdown.type' import { EmailType } from './email.type' @@ -17,6 +18,7 @@ export const fieldTypes: { } = { date: DateType, dropdown: DropdownType, + checkbox: CheckboxType, email: EmailType, link: LinkType, number: NumberType, diff --git a/components/styled/checkbox.tsx b/components/styled/checkbox.tsx new file mode 100644 index 0000000..a313be0 --- /dev/null +++ b/components/styled/checkbox.tsx @@ -0,0 +1,38 @@ +import { Checkbox } from 'antd' +import { RadioProps } from 'antd/lib/radio/interface' +import React from 'react' +import styled from 'styled-components' +import { FormPublicDesignFragment } from '../../graphql/fragment/form.public.fragment' + +interface Props extends RadioProps { + design: FormPublicDesignFragment +} + +// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-assignment +const Field = styled(Checkbox)` + color: ${(props: Props) => props.design.colors.answer}; + border-color: ${(props: Props) => props.design.colors.answer}; + background: none; + + .ant-radio { + .ant-radio-inner { + border-color: ${(props: Props) => props.design.colors.answer}; + + &::after { + background: ${(props: Props) => props.design.colors.answer}; + } + } + + &::after { + border-color: ${(props: Props) => props.design.colors.answer}; + } + } + + .anticon { + color: ${(props: Props) => props.design.colors.answer}; + } +` + +export const StyledCheckbox: React.FC = ({ children, ...props }) => { + return {children} +}