Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add form.onChange #819

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rare-meals-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ensembleui/react-runtime": patch
---

Added support of form.onChange
3 changes: 3 additions & 0 deletions apps/kitchen-sink/src/ensemble/screens/forms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ View:
styles:
gap: 8px
labelPosition: top
onChange:
executeCode: |
console.log({fields})
onSubmit:
executeCode:
body: |
Expand Down
22 changes: 22 additions & 0 deletions packages/runtime/src/widgets/Form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import {
type EnsembleWidget,
} from "@ensembleui/react-framework";
import { Form as AntForm } from "antd";
import type { FormProps as AntFormProps } from "antd";
import { useCallback, useState } from "react";
import type { FormLayout } from "antd/es/form/Form";
import { set } from "lodash-es";
import { WidgetRegistry } from "../../registry";
import { EnsembleRuntime } from "../../runtime";
import { useEnsembleAction } from "../../runtime/hooks/useEnsembleAction";
Expand All @@ -20,18 +22,24 @@ export type FormProps = {
children: EnsembleWidget[];
enabled?: boolean;
onSubmit?: EnsembleAction;
onChange?: EnsembleAction;
styles?: {
labelPosition: "top" | "start" | "none";
labelOverflow: "wrap" | "visible" | "clip" | "ellipsis";
} & EnsembleWidgetStyles;
} & EnsembleWidgetProps;

type AntFieldData = NonNullable<AntFormProps["fields"]>;

export const Form: React.FC<FormProps> = (props) => {
const { children, ...rest } = props;

const [form] = AntForm.useForm<unknown>();
const getValues = form.getFieldsValue;

const action = useEnsembleAction(props.onSubmit);
const onChangeAction = useEnsembleAction(props.onChange);

const onFinishCallback = useCallback(
(vals: unknown) => {
if (!action) {
Expand All @@ -43,6 +51,19 @@ export const Form: React.FC<FormProps> = (props) => {
[action],
);

const onChangeActionCallback = useCallback(
(changedFields: AntFieldData) => {
const fields = {};

changedFields.map(({ name, value }) =>
set(fields, name as string[], value),
);

onChangeAction?.callback({ fields });
},
[onChangeAction],
);

// reset form
const handleResetForm = useCallback(() => {
form.resetFields();
Expand Down Expand Up @@ -90,6 +111,7 @@ export const Form: React.FC<FormProps> = (props) => {
colon={false}
form={form}
layout={getLayout(values?.styles?.labelPosition)}
onFieldsChange={onChangeActionCallback}
onFinish={onFinishCallback}
style={{
display: "flex",
Expand Down
Loading