Skip to content

Commit

Permalink
feat: Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
nuintun committed May 23, 2024
1 parent 86905d7 commit e3539ee
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions app/js/components/FormDrawer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import useSubmit, { Options, Values } from '/js/hooks/useSubmit';
import { Button, Form, FormInstance, FormProps, Space } from 'antd';
import FlexDrawer, { FlexDrawerProps } from '/js/components/FlexDrawer';
import React, { cloneElement, memo, useCallback, useEffect, useMemo, useState } from 'react';
import React, { cloneElement, memo, useCallback, useEffect, useId, useMemo, useState } from 'react';

const { useForm } = Form;

Expand All @@ -21,6 +21,10 @@ type DrawerPicked =
type FormOmitted = 'title' | 'onError';
type SubmitPicked = 'query' | 'method' | 'notify' | 'normalize' | 'onError' | 'onSuccess' | 'onComplete';

function createFormId(id: string): string {
return `form_${id.replace(/[^a-z_\d]/gi, '')}`;
}

export interface FormDrawerProps<V extends Values, R>
extends Omit<FormProps<V>, FormOmitted>,
Pick<Options<V, R>, SubmitPicked>,
Expand Down Expand Up @@ -50,6 +54,7 @@ function defaultExtra<V>(submitting: boolean, form: FormInstance<V>, onClose: ()
}

function FormDrawer<V extends Values, R>({
name,
form,
query,
title,
Expand Down Expand Up @@ -77,6 +82,7 @@ function FormDrawer<V extends Values, R>({
extra = defaultExtra,
...restProps
}: FormDrawerProps<V, R>): React.ReactElement {
const id = useId();
const [wrapForm] = useForm<V>(form);
const [open, setOpen] = useState(false);

Expand Down Expand Up @@ -138,7 +144,7 @@ function FormDrawer<V extends Values, R>({
extra={extra(submitting, wrapForm, onCloseHandler)}
footer={footer?.(submitting, wrapForm, onCloseHandler)}
>
<Form {...restProps} layout={layout} form={wrapForm} onFinish={onSubmit}>
<Form {...restProps} layout={layout} form={wrapForm} onFinish={onSubmit} name={name || createFormId(id)}>
{children}
</Form>
</FlexDrawer>
Expand Down
2 changes: 1 addition & 1 deletion app/js/hooks/createStyles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface Styles<C extends Components = Components> {
export type Token<C extends Components = Components> = AliasToken & Pick<OverrideToken, C>;

function createScopeId(id: string): string {
return `css-var-scope-${id.replace(/[^a-z\d]/gi, '')}`;
return `css-var-scope-${id.replace(/[^a-z_\d]/gi, '')}`;
}

function prefixToken(component: string, key: string): string {
Expand Down

0 comments on commit e3539ee

Please sign in to comment.