ui(admin): add egg variable elements

This commit is contained in:
Matthew Penner
2021-10-01 11:25:06 -06:00
parent e2de673488
commit 7239f0e336
6 changed files with 185 additions and 44 deletions
@@ -1,8 +1,48 @@
import tw, { styled } from 'twin.macro';
import Checkbox from '@/components/elements/Checkbox';
import React from 'react';
import { useStoreState } from 'easy-peasy';
import Label from '@/components/elements/Label';
import { Field, FieldProps } from 'formik';
import Input from '@/components/elements/Input';
interface CheckboxProps {
name: string;
value: string;
className?: string;
}
type CheckboxOmitFields = 'ref' | 'name' | 'value' | 'type' | 'checked' | 'onClick' | 'onChange';
type InputProps = Omit<JSX.IntrinsicElements['input'], CheckboxOmitFields> & CheckboxProps;
const Checkbox = ({ name, value, className, ...props }: InputProps) => (
<Field name={name}>
{({ field, form }: FieldProps) => {
if (!Array.isArray(field.value)) {
console.error('Attempting to mount a checkbox using a field value that is not an array.');
return null;
}
return (
<Input
{...field}
{...props}
className={className}
type={'checkbox'}
checked={(field.value || []).includes(value)}
onClick={() => form.setFieldTouched(field.name, true)}
onChange={e => {
const set = new Set(field.value);
set.has(value) ? set.delete(value) : set.add(value);
field.onChange(e);
form.setFieldValue(field.name, Array.from(set));
}}
/>
);
}}
</Field>
);
const Container = styled.label`
${tw`flex items-center border border-transparent rounded md:p-2 transition-colors duration-75`};