Skip to content

Commit

Permalink
field_types
Browse files Browse the repository at this point in the history
  • Loading branch information
epompeii committed Jun 3, 2023
1 parent bb1cc5a commit 62e3d8a
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 19 deletions.
1 change: 0 additions & 1 deletion services/ui/src/components/auth/AuthConfirmPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ const AuthConfirmPage = (props: {
<Field
kind={FieldKind.INPUT}
fieldKey="token"
label={true}
value={form()?.token?.value}
valid={form()?.token?.valid}
config={AUTH_FIELDS.token}
Expand Down
5 changes: 2 additions & 3 deletions services/ui/src/components/auth/AuthForm.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createSignal, createEffect, createMemo } from "solid-js";
import axios from "axios";

import Field from "../field/Field";
import Field, { FieldHandler } from "../field/Field";
import AUTH_FIELDS from "./config/fields";
import {
BENCHER_API_URL,
Expand Down Expand Up @@ -43,7 +43,7 @@ export const AuthForm = (props: Props) => {
);
const [form, setForm] = createSignal(initForm());

const handleField = (key, value, valid) => {
const handleField: FieldHandler = (key, value, valid) => {
setForm({
...form(),
[key]: {
Expand Down Expand Up @@ -187,7 +187,6 @@ export const AuthForm = (props: Props) => {
<Field
kind={FieldKind.CHECKBOX}
fieldKey="consent"
label=""
value={form()?.consent?.value}
valid={form()?.consent?.valid}
config={AUTH_FIELDS.consent}
Expand Down
37 changes: 29 additions & 8 deletions services/ui/src/components/field/Field.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
import Input from "./kinds/Input";
import Checkbox from "./kinds/Checkbox";
import Input, { InputConfig, InputValue } from "./kinds/Input";
import Checkbox, { CheckboxConfig, CheckboxValue } from "./kinds/Checkbox";
import Switch from "./kinds/Switch";
import Select from "./kinds/Select";
import FieldKind from "./kind";
import Radio from "./kinds/Radio";

const Field = (props) => {
type FieldValue = CheckboxValue | InputValue;

type FieldConfig = CheckboxConfig | InputConfig;

export type FieldHandler = (
key: string,
value: FieldValue,
valid: boolean,
) => void;

export type FieldValueHandler = (value: FieldValue) => void;

const Field = (props: {
kind: FieldKind;
fieldKey: string;
label?: string;
value: FieldValue;
valid: boolean;
config: FieldConfig;
handleField: FieldHandler;
}) => {
function handleField(value) {
switch (props.kind) {
case FieldKind.CHECKBOX:
Expand All @@ -24,10 +44,11 @@ const Field = (props) => {
break;
case FieldKind.INPUT:
case FieldKind.NUMBER:
const config = props.config as InputConfig;
props.handleField(
props.fieldKey,
value,
props.config.validate ? props.config.validate(value) : true,
config.validate ? config.validate(value) : true,
);
break;
}
Expand All @@ -38,8 +59,8 @@ const Field = (props) => {
case FieldKind.CHECKBOX:
return (
<Checkbox
value={props.value}
config={props.config}
value={props.value as CheckboxValue}
config={props.config as CheckboxConfig}
handleField={handleField}
/>
);
Expand Down Expand Up @@ -73,9 +94,9 @@ const Field = (props) => {
case FieldKind.NUMBER:
return (
<Input
value={props.value}
value={props.value as InputValue}
valid={props.valid}
config={props.config}
config={props.config as InputConfig}
handleField={handleField}
/>
);
Expand Down
16 changes: 15 additions & 1 deletion services/ui/src/components/field/kinds/Checkbox.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
const Checkbox = (props) => {
import { JSX } from "solid-js";
import { FieldValueHandler } from "../Field";

export type CheckboxValue = boolean;

export interface CheckboxConfig {
label: string;
placeholder: JSX.Element;
}

const Checkbox = (props: {
value: CheckboxValue;
config: CheckboxConfig;
handleField: FieldValueHandler;
}) => {
return (
<div class="field" id={props.config.label}>
<input
Expand Down
24 changes: 21 additions & 3 deletions services/ui/src/components/field/kinds/Input.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
const Input = (props) => {
import { FieldValueHandler } from "../Field";

export type InputValue = string | number | null | undefined;

export interface InputConfig {
icon: string;
type: string;
placeholder?: string;
value: InputValue;
disabled?: boolean;
validate: (value: InputValue) => boolean;
}

const Input = (props: {
value: InputValue;
valid: boolean;
config: InputConfig;
handleField: FieldValueHandler;
}) => {
return (
<div class="control has-icons-left has-icons-right">
<span class="icon is-small is-left">
<i class={props.config.icon}></i>
<i class={props.config.icon} />
</span>
<input
class="input"
Expand All @@ -14,7 +32,7 @@ const Input = (props) => {
/>
{props.valid && (
<span class="icon is-small is-right">
<i class="fas fa-check"></i>
<i class="fas fa-check" />
</span>
)}
</div>
Expand Down
2 changes: 1 addition & 1 deletion services/ui/src/components/field/kinds/Radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const Radio = (props) => {
<div class="level-left">
<div class="level-item">
<div class="icon is-small is-left">
<i class={props.config.icon}></i>
<i class={props.config.icon} />
</div>
</div>
<div class="level-item">
Expand Down
2 changes: 1 addition & 1 deletion services/ui/src/components/field/kinds/Select.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Select = (props) => {
function getStatus() {
let selected = props.value.options.find((option) => {
const selected = props.value.options.find((option) => {
return props.value.selected === option.value;
});
if (selected?.status) {
Expand Down
3 changes: 2 additions & 1 deletion services/ui/src/components/field/kinds/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Switch = (props) => {
checked={props.value}
disabled={props.config?.disabled}
/>
{/* rome-ignore lint/a11y/useKeyWithClickEvents: TODO */}
<label
for={props.config?.label}
onClick={(_event) => {
Expand All @@ -17,7 +18,7 @@ const Switch = (props) => {
}
props.handleField(!props.value);
}}
></label>
/>
</div>
);
};
Expand Down

0 comments on commit 62e3d8a

Please sign in to comment.