Skip to content

Commit

Permalink
test: add test case of form
Browse files Browse the repository at this point in the history
  • Loading branch information
pointhalo committed Jun 20, 2024
1 parent 9339ff8 commit 8524885
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
55 changes: 55 additions & 0 deletions packages/semi-ui/form/_story/ArrayField/remountInit.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React, { useState } from 'react';
import { ArrayField, TextArea, Form, Button, useFormState } from '@douyinfe/semi-ui';
import { IconPlusCircle, IconMinusCircle } from '@douyinfe/semi-icons';

// toggle form input visible
// visible -> invisible -> visible
// initValue will not use in final round now
// The behavior in ArrayField is inconsistent with that in non-ArrayField, which is undefined behavior. Clearer guidance is needed.

const RemountInit = () => {
const [data, setData] = useState([
{ name: 'arrayfield defualtValue' },
]);
const [visible, setVisible] = useState(true);
const ComponentUsingFormState = () => {
const formState = useFormState();
return (
<TextArea style={{ marginTop: 10 }} value={JSON.stringify(formState)} />
);
};
return (
<Form style={{ width: 800 }} labelPosition='left' labelWidth='100px' allowEmpty>
<Button onClick={() => { setVisible(!visible); }}>toggle</Button>
<ArrayField field='rules' initValue={data}>
{({ add, arrayFields }) => (
<React.Fragment>
<Button onClick={add} icon={<IconPlusCircle />} theme='light'>Add new line</Button>
{
arrayFields.map(({ field, key, remove }, i) => (
<div key={key} style={{ width: 1000, display: 'flex' }}>
{visible && <Form.Input
field={`${field}[name]`}
label={`${field}.name`}
style={{ width: 200, marginRight: 16 }}
initValue={'field init value'}
>
</Form.Input>}
<Button
type='danger'
theme='borderless'
icon={<IconMinusCircle />}
onClick={remove}
style={{ margin: 12 }}
/>
</div>
))
}
</React.Fragment>
)}
</ArrayField>
<ComponentUsingFormState />
</Form>
);
};
export default RemountInit;
1 change: 1 addition & 0 deletions packages/semi-ui/form/_story/form.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import ChildDidMount from './Debug/childDidMount';

export { default as FormSubmit } from './FormSubmit';
export { default as TabelForm } from './TableDemo';
export { default as RemountInit } from './ArrayField/remountInit'

const {
Input,
Expand Down

0 comments on commit 8524885

Please sign in to comment.