|
| 1 | +Basic Form |
| 2 | + |
| 3 | +```react|span-4 |
| 4 | +const onSubmit = (values, actions) => { |
| 5 | + alert(JSON.stringify(values, null, 2)); |
| 6 | + actions.setSubmitting(false) |
| 7 | +}; |
| 8 | +
|
| 9 | +const onCancel = () => alert("Everyting is Erased!!!!"); |
| 10 | +
|
| 11 | +const yup = Form.yup; |
| 12 | +
|
| 13 | +class Model { |
| 14 | + static schemata = { |
| 15 | + normal: yup.object().shape({ |
| 16 | + testField: yup.string().min(5).max(8).required() |
| 17 | + }) |
| 18 | + }; |
| 19 | +
|
| 20 | +
|
| 21 | + constructor({ testField = ""}){ |
| 22 | + this.testField = testField; |
| 23 | + } |
| 24 | +} |
| 25 | +
|
| 26 | +const values = { testField: "abc"} |
| 27 | +
|
| 28 | +const Field = Form.Field; |
| 29 | +<Form |
| 30 | + values={values} |
| 31 | + Model={Model} |
| 32 | + title="Test Form" |
| 33 | + onSubmit={onSubmit} |
| 34 | + onCancel={onCancel} |
| 35 | +> |
| 36 | + <Field title="Test Field" name="testField" placeholder="Test" /> |
| 37 | +</Form> |
| 38 | +``` |
| 39 | + |
| 40 | +Pimped Out Form |
| 41 | + |
| 42 | +```react|span-4 |
| 43 | +state: { errors: {}} |
| 44 | +--- |
| 45 | +
|
| 46 | +const errors = {general: "you seem to have misspelled something..."}; |
| 47 | +const testItems1 = [{task: "aaaa", resourceUrl: "bbb"}, {task: "", resourceUrl: ""}]; |
| 48 | +const testItems2 = ["aaaaa", "bbbb"]; |
| 49 | +
|
| 50 | +const onSubmit = (values, actions) => { |
| 51 | + alert(JSON.stringify(values, null, 2)); |
| 52 | + setState({errors}), |
| 53 | + actions.setSubmitting(true) |
| 54 | +}; |
| 55 | +
|
| 56 | +
|
| 57 | +const onCancel = () => alert("Everyting is Erased!!!!"); |
| 58 | +const values = { |
| 59 | + testField: "ab", |
| 60 | + testItems: testItems2 |
| 61 | +}; |
| 62 | +
|
| 63 | +const buttons = { |
| 64 | + warning: { |
| 65 | + title: "Maybe...", |
| 66 | + variant: "warning", |
| 67 | + onClick: () => confirm("are you sure"), |
| 68 | + size: "small" |
| 69 | + }, |
| 70 | + cancel: { |
| 71 | + title: "HELL NO", |
| 72 | + size: "medium" |
| 73 | + }, |
| 74 | + submit: { |
| 75 | + title: "Okay", |
| 76 | + size: "small" |
| 77 | + } |
| 78 | +}; |
| 79 | +
|
| 80 | +const linkData = [ |
| 81 | + { onClick: onCancel, title: "Go Somewhere Else" }, |
| 82 | + { onClick: onCancel, title: "Go Someplace Other" } |
| 83 | +]; |
| 84 | +
|
| 85 | +const yup = Form.yup; |
| 86 | +class Model { |
| 87 | + static schemata = { |
| 88 | + normal: yup.object().shape({ |
| 89 | + testField: yup.string().min(5).max(8).required(), |
| 90 | + testItems: yup.array().of(yup.string().min(3)).min(2).max(3).required() |
| 91 | + }) |
| 92 | + }; |
| 93 | +
|
| 94 | + constructor({ testField = "", testItems = [""]}){ |
| 95 | + this.testField = testField; |
| 96 | + this.testItems = testItems; |
| 97 | + } |
| 98 | +} |
| 99 | +
|
| 100 | +const Field = Form.Field; |
| 101 | +const FieldList = Form.FieldList; |
| 102 | +<Form |
| 103 | + values={values} |
| 104 | + Model={Model} |
| 105 | + errors={state.errors} |
| 106 | + title="Test Form" |
| 107 | + links={linkData} |
| 108 | + buttons={buttons} |
| 109 | + onSubmit={onSubmit} |
| 110 | + onCancel={onCancel} |
| 111 | +> |
| 112 | + <Field title="Test Field" name="testField" placeholder="Test" /> |
| 113 | + <FieldList title="Test Items" name="testItems"/> |
| 114 | +</Form> |
| 115 | +``` |
0 commit comments