inform.test.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import React from 'react';
  2. // import {
  3. // storiesOf
  4. // } from '@storybook/react';
  5. import { Form, Text } from 'informed';
  6. import { TextArea } from '@douyinfe/semi-ui';
  7. // import { Form, asField } from '../src/index';
  8. // import Input from '../../input/index';
  9. // import Select from '../../select/index';
  10. // const stories = storiesOf('informed', module);
  11. // // // stories.addDecorator(withKnobs);;
  12. // const validate = value => {
  13. // return !value || value.length < 5
  14. // ? 'Field must be at least five characters'
  15. // : undefined;
  16. // };
  17. // const ErrorText = asField(({
  18. // fieldState,
  19. // fieldApi,
  20. // ...props
  21. // }) => {
  22. // const {value} = fieldState;
  23. // const {setValue, setTouched} = fieldApi;
  24. // const {
  25. // onChange,
  26. // onBlur,
  27. // initialValue,
  28. // forwardedRef,
  29. // ...rest
  30. // } = props;
  31. // return (
  32. // <React.Fragment>
  33. // <Input
  34. // {...rest}
  35. // ref={forwardedRef}
  36. // value={!value && value !== 0 ? '' : value}
  37. // onChange={(value, e) => {
  38. // setValue(e.target.value);
  39. // if (onChange) {
  40. // onChange(e);
  41. // }
  42. // }}
  43. // onBlur={e => {
  44. // setTouched();
  45. // if (onBlur) {
  46. // onBlur(e);
  47. // }
  48. // }}
  49. // />
  50. // </React.Fragment>
  51. // );
  52. // });
  53. // const FromScratch = () => (
  54. // <div>
  55. // <Form id='custom-form-2'>
  56. // {({formApi, formState}) => (
  57. // <React.Fragment>
  58. // <label>
  59. // First name:
  60. // <ErrorText
  61. // field='name'
  62. // validate={validate}
  63. // validateOnChange
  64. // validateOnBlur
  65. // />
  66. // </label>
  67. // <button type='submit'>Submit</button>
  68. // <code>{JSON.stringify(formState)}</code>
  69. // </React.Fragment>
  70. // )}
  71. // </Form>
  72. // </div>
  73. // );
  74. // stories.add('Form施工现场', () => (<FromScratch />));
  75. const submit = values => window.alert(`Form successfully submitted with ${JSON.stringify(values)}`);
  76. const validate = value => {
  77. if (!value) {
  78. return '不能为空';
  79. }
  80. };
  81. class InformDemo extends React.Component {
  82. render() {
  83. return (
  84. <Form onSubmit={submit}>
  85. {({ formState, values }) => (
  86. <>
  87. <Text field="panels[11].name" validateOnBlur />
  88. <Text field="panels[11].type" validateOnBlur />
  89. <button type="submit">Submit</button>
  90. <TextArea value={JSON.stringify(formState)} />
  91. </>
  92. )}
  93. </Form>
  94. );
  95. }
  96. }
  97. export { InformDemo };