rulesUpdateDemo.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* eslint-disable */
  2. import React, { useLayoutEffect, useRef, useContext, useState, Component } from 'react';
  3. import { Button, Form, Select } from '@douyinfe/semi-ui';
  4. const ApiContext = React.createContext({});
  5. const HOC = Component => props => {
  6. let { text } = props;
  7. let { field } = props;
  8. const parentApi = useContext(ApiContext);
  9. const ref = useRef(props);
  10. const logProps = () => {
  11. // 通过props取,只能是当次render时的值
  12. console.log(`props: ${props.text}`);
  13. // 通过ref取,始终是最新的值
  14. // console.log(`ref.current: ${ref.current.text}`);
  15. };
  16. let content = (() => (
  17. <>
  18. {Component}
  19. <span>Props.text:</span> {text}
  20. <Button onClick={logProps}>子级自己内部触发log</Button>
  21. </>
  22. ))();
  23. useLayoutEffect(() => {
  24. ref.current = props;
  25. }, [props]);
  26. useLayoutEffect(() => {
  27. parentApi.register(field, {
  28. log: logProps,
  29. });
  30. return () => {};
  31. }, [field]);
  32. return content;
  33. };
  34. let D = HOC(<div />);
  35. class UpdateDemo extends React.Component {
  36. constructor() {
  37. super();
  38. this.state = {
  39. text: 'semi',
  40. };
  41. this.change = this.change.bind(this);
  42. this.triggerChildLog = this.triggerChildLog.bind(this);
  43. this.fields = {};
  44. }
  45. change() {
  46. this.setState({ text: Math.random() });
  47. }
  48. register(field, fieldApi) {
  49. this.fields.name = {
  50. func: fieldApi,
  51. };
  52. }
  53. triggerChildLog() {
  54. this.fields.name.func.log();
  55. }
  56. render() {
  57. const { text } = this.state;
  58. const api = {
  59. register: this.register.bind(this),
  60. };
  61. return (
  62. <div>
  63. <ApiContext.Provider value={api}>
  64. <div>
  65. <D text={text} field="name" />
  66. </div>
  67. <Button onClick={this.change}>父级动态改变子级props</Button>
  68. <Button onClick={this.triggerChildLog}>由父级触发子级的log函数</Button>
  69. </ApiContext.Provider>
  70. </div>
  71. );
  72. }
  73. }
  74. const RuleupdateDemo = () => {
  75. const [val, setVal] = useState(['fefe']);
  76. const change = () => {
  77. let arr = [];
  78. arr[0] = Math.random();
  79. setVal(arr);
  80. return false;
  81. };
  82. return (
  83. <Form layout="horizontal">
  84. {({ formState, values, formApi }) => (
  85. <>
  86. <Form.Input field="UserName" label="用户名" test={val} rules={ values.required ? [{ required: values.required }]: [] } />
  87. {/* <Form.Input field="UserName" label="用户名" test={val} rules={ [{ required: values.required }] } /> */}
  88. <Form.Switch field="required" />
  89. <Button onClick={change}>change</Button>
  90. <Button htmlType="submit">submit</Button>
  91. <code style={{ marginTop: 30 }}>{JSON.stringify(formState)}</code>
  92. </>
  93. )}
  94. </Form>
  95. );
  96. };
  97. export { UpdateDemo, RuleupdateDemo };