From 7babd0967963bf2440e350d7e456ee77371704d4 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Tue, 21 Nov 2023 11:28:15 +0000 Subject: [PATCH] more work on forms --- packages/fastui-bootstrap/src/index.tsx | 18 +++++++++++---- packages/fastui/src/components/button.tsx | 4 ++-- packages/fastui/src/components/form.tsx | 16 ++++++++------ packages/fastui/src/components/link.tsx | 6 ++--- packages/fastui/src/components/table.tsx | 6 ++--- packages/fastui/src/hooks/className.ts | 2 +- packages/fastui/src/hooks/event.ts | 13 +++++++++-- packages/fastui/src/hooks/locationContext.tsx | 5 +++++ packages/fastui/src/index.tsx | 12 ++++++++-- python/demo/main.py | 22 +++++++++++++------ python/fastui/components/__init__.py | 9 +++++++- python/fastui/events.py | 6 ++++- 12 files changed, 86 insertions(+), 33 deletions(-) diff --git a/packages/fastui-bootstrap/src/index.tsx b/packages/fastui-bootstrap/src/index.tsx index 01e7e721..67df6346 100644 --- a/packages/fastui-bootstrap/src/index.tsx +++ b/packages/fastui-bootstrap/src/index.tsx @@ -1,6 +1,6 @@ import { ClassNameGenerator, CustomRender } from 'fastui' -import type { FormFieldProps } from 'fastui' +import type { FormFieldProps, ClassName } from 'fastui' export const customRender: CustomRender = (props) => { const { type } = props @@ -25,25 +25,35 @@ export const classNameGenerator: ClassNameGenerator = (props, subElement) => { return 'btn btn-primary' case 'Table': return 'table table-striped' + case 'Form': + case 'ModelForm': + return formClassName(subElement) case 'FormFieldInput': case 'FormFieldCheckbox': case 'FormFieldSelect': case 'FormFieldFile': - return formClassName(props, subElement) + return formFieldClassName(props, subElement) } } -function formClassName(props: FormFieldProps, subElement?: string) { +function formFieldClassName(props: FormFieldProps, subElement?: string): ClassName { switch (subElement) { case 'input': return props.error ? 'is-invalid form-control' : 'form-control' case 'select': return 'form-select' case 'label': - return 'form-label' + return { 'form-label': true, 'fw-bold': props.required } case 'error': return 'invalid-feedback' default: return 'mb-3' } } + +function formClassName(subElement?: string): ClassName { + switch (subElement) { + case 'form-container': + return 'd-flex justify-content-center' + } +} diff --git a/packages/fastui/src/components/button.tsx b/packages/fastui/src/components/button.tsx index b10db838..dafb92ac 100644 --- a/packages/fastui/src/components/button.tsx +++ b/packages/fastui/src/components/button.tsx @@ -1,12 +1,12 @@ import { FC } from 'react' import { ClassName, useClassName } from '../hooks/className' -import { useFireEvent, PageEvent, GoToEvent } from '../hooks/event' +import { useFireEvent, Event } from '../hooks/event' export interface ButtonProps { type: 'Button' text: string - onClick?: PageEvent | GoToEvent + onClick?: Event htmlType?: 'button' | 'submit' | 'reset' className?: ClassName } diff --git a/packages/fastui/src/components/form.tsx b/packages/fastui/src/components/form.tsx index da6fc0de..099655e1 100644 --- a/packages/fastui/src/components/form.tsx +++ b/packages/fastui/src/components/form.tsx @@ -1,7 +1,7 @@ import { FC, FormEvent, useState } from 'react' import { ClassName, useClassName } from '../hooks/className' -import { useFireEvent, PageEvent, GoToEvent } from '../hooks/event' +import { useFireEvent, Event } from '../hooks/event' import { request } from '../tools' import { FastProps, RenderChildren } from './index' @@ -26,7 +26,7 @@ export interface ModelFormProps extends BaseFormProps { interface FormResponse { type: 'FormResponse' - event: PageEvent | GoToEvent + event: Event } export const FormComp: FC = (props) => { @@ -71,11 +71,13 @@ export const FormComp: FC = (props) => { ) return ( -
- - {error ?
Error: {error}
: null} -