Skip to content

Commit 131aa70

Browse files
committed
fix(Form): internal logic fixes
1 parent c3e1ae2 commit 131aa70

File tree

5 files changed

+41
-38
lines changed

5 files changed

+41
-38
lines changed

.changeset/wild-parents-suffer.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cube-dev/ui-kit': minor
3+
---
4+
5+
Form logic internal fixes.

src/components/content/Tag/Tag.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const TagElement = tasty({
3434
closable: '0 (2.5x - 1bw) 0 (1x - 1bw)',
3535
},
3636
fill: {
37-
'': '#dark.04',
37+
'': '#light',
3838
...Object.keys(THEMES).reduce((map, type) => {
3939
map[`[data-type="${type}"]`] = THEMES[type].fill;
4040

src/components/fields/ComboBox/ComboBox.stories.tsx

+23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { userEvent, within } from '@storybook/test';
44

55
import { SELECTED_KEY_ARG } from '../../../stories/FormFieldArgs';
66
import { baseProps } from '../../../stories/lists/baseProps';
7+
import { Form, useForm } from '../../form/index';
78

89
import { ComboBox, CubeComboBoxProps } from './ComboBox';
910

@@ -32,6 +33,26 @@ const Template: StoryFn<CubeComboBoxProps<any>> = (
3233
</>
3334
);
3435

36+
const FormTemplate: StoryFn<CubeComboBoxProps<any>> = (
37+
args: CubeComboBoxProps<any>,
38+
) => {
39+
const [form] = useForm();
40+
41+
return (
42+
<Form form={form}>
43+
<ComboBox name="combobox" {...args}>
44+
<ComboBox.Item key="red">Red</ComboBox.Item>
45+
<ComboBox.Item key="orange">Orange</ComboBox.Item>
46+
<ComboBox.Item key="yellow">Yellow</ComboBox.Item>
47+
<ComboBox.Item key="green">Green</ComboBox.Item>
48+
<ComboBox.Item key="blue">Blue</ComboBox.Item>
49+
<ComboBox.Item key="purple">Purple</ComboBox.Item>
50+
<ComboBox.Item key="violet">Violet</ComboBox.Item>
51+
</ComboBox>
52+
</Form>
53+
);
54+
};
55+
3556
export const Default = Template.bind({});
3657
Default.args = {};
3758

@@ -101,3 +122,5 @@ With1LongOptionFiltered.play = async ({ canvasElement }) => {
101122

102123
await userEvent.type(combobox, 'Red');
103124
};
125+
126+
export const WithinForm = FormTemplate.bind({});

src/components/form/Form/use-field/use-field.ts

+11-37
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useEffect, useMemo, useState } from 'react';
22

33
import { ValidateTrigger } from '../../../../shared/index';
4-
import { useEvent, useIsFirstRender } from '../../../../_internal/index';
4+
import { useEvent } from '../../../../_internal/index';
55
import { useFormProps } from '../Form';
66
import { FieldTypes } from '../types';
77
import { delayValidationRule } from '../validation';
@@ -47,7 +47,6 @@ export function useField<T extends FieldTypes, Props extends CubeFieldProps<T>>(
4747
props = useFormProps(props);
4848

4949
let {
50-
defaultValue,
5150
id,
5251
idPrefix,
5352
name,
@@ -60,15 +59,9 @@ export function useField<T extends FieldTypes, Props extends CubeFieldProps<T>>(
6059
showValid,
6160
shouldUpdate,
6261
} = props;
63-
64-
if (rules && rules.length && validationDelay) {
65-
rules.unshift(delayValidationRule(validationDelay));
66-
}
67-
6862
const nonInput = !name;
6963
const fieldName: string = name != null ? name : '';
7064

71-
const isFirstRender = useIsFirstRender();
7265
let [fieldId, setFieldId] = useState(
7366
id || (idPrefix ? `${idPrefix}_${fieldName}` : fieldName),
7467
);
@@ -95,41 +88,22 @@ export function useField<T extends FieldTypes, Props extends CubeFieldProps<T>>(
9588

9689
let field = form?.getFieldInstance(fieldName);
9790

98-
if (field) {
99-
field.rules = rules;
100-
}
101-
102-
let isRequired = rules && !!rules.find((rule) => rule.required);
103-
104-
useEffect(() => {
105-
if (!form) return;
106-
107-
if (field) {
108-
form.forceReRender();
109-
} else {
110-
form.createField(fieldName);
111-
}
112-
}, [field]);
113-
11491
if (form) {
115-
if (isFirstRender) {
116-
if (!field) {
117-
field = form.createField(fieldName, true);
118-
}
119-
120-
if (field?.value == null && defaultValue != null) {
121-
form.setFieldValue(fieldName, defaultValue, false, true);
122-
form.updateInitialFieldsValue({ [fieldName]: defaultValue });
123-
124-
field = form?.getFieldInstance(fieldName);
125-
}
92+
if (!field) {
93+
field = form.createField(fieldName, true);
12694
}
95+
}
96+
97+
if (field && field.rules !== rules) {
98+
field.rules = rules;
12799

128-
if (!field?.touched && defaultValue != null) {
129-
form.setFieldValue(fieldName, defaultValue, false, true);
100+
if (field.rules && field.rules.length && validationDelay) {
101+
field.rules.unshift(delayValidationRule(validationDelay));
130102
}
131103
}
132104

105+
let isRequired = rules && !!rules.find((rule) => rule.required);
106+
133107
const onChangeHandler = useEvent((val: any, dontTouch: boolean) => {
134108
if (!form) return;
135109

src/tokens.ts

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const TOKENS = {
4848
transition: '80ms',
4949
'clear-color': 'transparent',
5050
'border-color': color('dark', 0.1),
51+
'border-opaque-color': 'rgb(227, 227, 233)',
5152
'shadow-color': color('dark-03', 0.1),
5253
'draft-color': color('dark', 0.2),
5354
'minor-color': color('dark', 0.65),

0 commit comments

Comments
 (0)