|
| 1 | +import { AppProvider, BlockStack, Card, FormLayout, InlineStack, Page, Text } from "@shopify/polaris"; |
| 2 | +import translations from "@shopify/polaris/locales/en.json"; |
| 3 | +import React from "react"; |
| 4 | +import { Provider } from "../../../src/GadgetProvider.tsx"; |
| 5 | +import { PolarisAutoForm } from "../../../src/auto/polaris/PolarisAutoForm.tsx"; |
| 6 | +import { PolarisAutoInput } from "../../../src/auto/polaris/inputs/PolarisAutoInput.tsx"; |
| 7 | +import { PolarisAutoBelongsToInput } from "../../../src/auto/polaris/inputs/relationships/PolarisAutoBelongsToInput.tsx"; |
| 8 | +import { PolarisAutoRelationshipForm } from "../../../src/auto/polaris/inputs/relationships/PolarisAutoRelationshipForm.tsx"; |
| 9 | +import { PolarisAutoSubmit } from "../../../src/auto/polaris/submit/PolarisAutoSubmit.tsx"; |
| 10 | +import { FormProvider, useForm } from "../../../src/useActionForm.ts"; |
| 11 | +import { testApi as api } from "../../apis.ts"; |
| 12 | + |
| 13 | +const ExampleWidgetAutoRelatedForm = (props) => { |
| 14 | + return ( |
| 15 | + <Page> |
| 16 | + <PolarisAutoForm {...props}> |
| 17 | + <Card> |
| 18 | + <Text as="h2" variant="headingSm"> |
| 19 | + Top Level Form -- Widget |
| 20 | + </Text> |
| 21 | + <PolarisAutoInput field="name" /> |
| 22 | + <PolarisAutoInput field="description" /> |
| 23 | + <PolarisAutoInput field="inventoryCount" /> |
| 24 | + </Card> |
| 25 | + |
| 26 | + <Card> |
| 27 | + <Text as="h2" variant="headingSm"> |
| 28 | + Belongs To Form -- Section |
| 29 | + </Text> |
| 30 | + <PolarisAutoBelongsToInput field="section" optionLabel="id" /> |
| 31 | + </Card> |
| 32 | + |
| 33 | + <Card> |
| 34 | + <PolarisAutoInput field="gizmos" /> |
| 35 | + </Card> |
| 36 | + |
| 37 | + <Card> |
| 38 | + <Text as="h2" variant="headingSm"> |
| 39 | + Has Many Form -- Gizmos |
| 40 | + </Text> |
| 41 | + <PolarisAutoRelationshipForm |
| 42 | + field="gizmos" |
| 43 | + selectPaths={["name", "orientation"]} |
| 44 | + primaryLabel="name" |
| 45 | + secondaryLabel="orientation" |
| 46 | + > |
| 47 | + <PolarisAutoInput field="name" /> |
| 48 | + <PolarisAutoInput field="orientation" /> |
| 49 | + <PolarisAutoInput field="attachment" /> |
| 50 | + </PolarisAutoRelationshipForm> |
| 51 | + </Card> |
| 52 | + <PolarisAutoSubmit /> |
| 53 | + </PolarisAutoForm> |
| 54 | + </Page> |
| 55 | + ); |
| 56 | +}; |
| 57 | + |
| 58 | +const ExampleSectionAutoRelatedForm = (props) => { |
| 59 | + return ( |
| 60 | + <Page> |
| 61 | + <PolarisAutoForm {...props}> |
| 62 | + <Card> |
| 63 | + <Text as="h2" variant="headingSm"> |
| 64 | + Top Level Form -- Section |
| 65 | + </Text> |
| 66 | + <PolarisAutoInput field="name" /> |
| 67 | + <PolarisAutoInput field="label" /> |
| 68 | + </Card> |
| 69 | + |
| 70 | + <Card> |
| 71 | + <Text as="h2" variant="headingSm"> |
| 72 | + Has Many Form -- Widgets |
| 73 | + </Text> |
| 74 | + <PolarisAutoRelationshipForm field="widgets"> |
| 75 | + <PolarisAutoInput field="name" /> |
| 76 | + <PolarisAutoInput field="inventoryCount" /> |
| 77 | + </PolarisAutoRelationshipForm> |
| 78 | + </Card> |
| 79 | + <PolarisAutoSubmit /> |
| 80 | + </PolarisAutoForm> |
| 81 | + </Page> |
| 82 | + ); |
| 83 | +}; |
| 84 | + |
| 85 | +const ExampleCourseCreateRelatedForm = (props) => { |
| 86 | + return ( |
| 87 | + <Page> |
| 88 | + <PolarisAutoForm {...props}> |
| 89 | + <Card> |
| 90 | + <BlockStack gap="300"> |
| 91 | + <Text as="h2" variant="headingSm"> |
| 92 | + Top Level Form -- Course |
| 93 | + </Text> |
| 94 | + <FormLayout> |
| 95 | + <PolarisAutoInput field="title" /> |
| 96 | + <PolarisAutoInput field="description" /> |
| 97 | + </FormLayout> |
| 98 | + </BlockStack> |
| 99 | + </Card> |
| 100 | + |
| 101 | + <Card> |
| 102 | + <PolarisAutoRelationshipForm |
| 103 | + field="students" |
| 104 | + selectPaths={["firstName", "lastName", "year", "department"]} |
| 105 | + primaryLabel={["firstName", "lastName"]} |
| 106 | + secondaryLabel={(record) => { |
| 107 | + if (record.year <= 1) { |
| 108 | + return "Freshman"; |
| 109 | + } else if (record.year <= 2) { |
| 110 | + return "Sophomore"; |
| 111 | + } else if (record.year <= 3) { |
| 112 | + return "Junior"; |
| 113 | + } else if (record.year <= 4) { |
| 114 | + return "Senior"; |
| 115 | + } else { |
| 116 | + return "Mature"; |
| 117 | + } |
| 118 | + }} |
| 119 | + tertiaryLabel="department" |
| 120 | + > |
| 121 | + <InlineStack> |
| 122 | + <PolarisAutoInput field="registration.effectiveFrom" /> |
| 123 | + <PolarisAutoInput field="registration.effectiveTo" /> |
| 124 | + </InlineStack> |
| 125 | + </PolarisAutoRelationshipForm> |
| 126 | + </Card> |
| 127 | + |
| 128 | + <Card> |
| 129 | + <PolarisAutoRelationshipForm |
| 130 | + field="professors" |
| 131 | + selectPaths={["title", "firstName", "lastName"]} |
| 132 | + primaryLabel={["title", "firstName", "lastName"]} |
| 133 | + /> |
| 134 | + </Card> |
| 135 | + <PolarisAutoSubmit /> |
| 136 | + </PolarisAutoForm> |
| 137 | + </Page> |
| 138 | + ); |
| 139 | +}; |
| 140 | + |
| 141 | +export default { |
| 142 | + title: "Polaris/AutoForm/AutoRelationshipForm", |
| 143 | + component: ExampleWidgetAutoRelatedForm, |
| 144 | + decorators: [ |
| 145 | + (Story) => { |
| 146 | + return ( |
| 147 | + <Provider api={api}> |
| 148 | + <AppProvider i18n={translations}> |
| 149 | + <FormProvider {...useForm()}> |
| 150 | + <Story /> |
| 151 | + </FormProvider> |
| 152 | + </AppProvider> |
| 153 | + </Provider> |
| 154 | + ); |
| 155 | + }, |
| 156 | + ], |
| 157 | +}; |
| 158 | + |
| 159 | +export const Create = { |
| 160 | + args: { |
| 161 | + action: api.widget.create, |
| 162 | + }, |
| 163 | +}; |
| 164 | + |
| 165 | +export const Update = { |
| 166 | + args: { |
| 167 | + action: api.widget.update, |
| 168 | + findBy: "1", |
| 169 | + }, |
| 170 | +}; |
| 171 | + |
| 172 | +export const CreateWithLargeHasMany = { |
| 173 | + render: (args) => <ExampleSectionAutoRelatedForm {...args} />, |
| 174 | + args: { |
| 175 | + action: api.section.create, |
| 176 | + }, |
| 177 | +}; |
| 178 | + |
| 179 | +export const CreateWithHasManyThrough = { |
| 180 | + render: (args) => <ExampleCourseCreateRelatedForm {...args} />, |
| 181 | + args: { |
| 182 | + action: api.university.course.create, |
| 183 | + }, |
| 184 | +}; |
| 185 | + |
| 186 | +export const UpdateWithHasManyThrough = { |
| 187 | + render: (args) => <ExampleCourseCreateRelatedForm {...args} />, |
| 188 | + args: { |
| 189 | + action: api.university.course.update, |
| 190 | + findBy: "3", |
| 191 | + }, |
| 192 | +}; |
0 commit comments