Skip to content

Commit 4123d2e

Browse files
committed
new build with vite
1 parent 1a4b04d commit 4123d2e

File tree

8 files changed

+595
-187
lines changed

8 files changed

+595
-187
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ jspm_packages
3838

3939
# Compiled files
4040
lib
41+
dist
42+
stats.html

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ docs
33
yarn.lock
44
README.md
55
LICENSE.md
6+
stats.html

package.json

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
{
22
"name": "orionjs-react-autoform",
33
"version": "2.0.9",
4-
"main": "lib/index.js",
5-
"types": "lib/index.d.ts",
4+
"types": "./dist/index.d.ts",
65
"files": [
7-
"/lib"
6+
"dist"
87
],
8+
"main": "./dist/orionjs-react-autoform.umd.js",
9+
"module": "./dist/orionjs-react-autoform.es.js",
10+
"exports": {
11+
".": {
12+
"import": "./dist/orionjs-react-autoform.es.js",
13+
"require": "./dist/orionjs-react-autoform.umd.js"
14+
}
15+
},
916
"repository": "https://github.com/orionjs/orionjs-react-autoform",
1017
"author": "nicolaslopezj",
1118
"license": "MIT",
1219
"scripts": {
1320
"test": "exit 0",
1421
"prepare": "yarn run build",
15-
"clean": "rm -rf ./lib",
16-
"build": "yarn run clean && tsc",
17-
"watch": "tsc -w"
22+
"clean": "rm -rf ./dist",
23+
"dev": "watch -p './src/**/*.ts' -c 'yarn build'",
24+
"build": "vite build && yarn emit-types",
25+
"emit-types": "tsc"
1826
},
1927
"devDependencies": {
2028
"@apollo/client": "^3.6.4",
@@ -24,8 +32,10 @@
2432
"@types/react": "^18.0.9",
2533
"graphql": "^16.5.0",
2634
"react": "^17.0.0",
35+
"rollup-plugin-visualizer": "^5.8.3",
2736
"simple-react-form": "3.0.10",
28-
"typescript": "^4.6.4"
37+
"typescript": "^4.6.4",
38+
"vite": "^4.0.2"
2939
},
3040
"peerDependencies": {
3141
"@apollo/client": "^3.5.0",

src/AutoForm.tsx

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import React from 'react'
2+
import WithParams from './WithParams'
3+
import Form, {AutoFormFormProps} from './Form'
4+
import WithMutation from './WithMutation'
5+
import getFragment from './getFragment'
6+
import {getValidationErrors, clean, Blackbox} from '@orion-js/schema'
7+
import debounce from 'lodash/debounce'
8+
import {ApolloClient} from '@apollo/client'
9+
import Fields from './Fields'
10+
11+
export interface AutoFormChildrenProps {
12+
params: Blackbox
13+
omit: string[] | string
14+
only: string[] | string
15+
}
16+
17+
export interface AutoFormProps {
18+
mutation: string
19+
doc?: Blackbox
20+
onChange?: (newDoc: any) => any
21+
children?: React.ReactNode | ((props: AutoFormChildrenProps) => React.ReactNode)
22+
fragment?: any
23+
getErrorFieldLabel?: AutoFormFormProps['getErrorFieldLabel']
24+
onSuccess?: AutoFormFormProps['onSuccess']
25+
onValidationError?: AutoFormFormProps['onValidationError']
26+
onError?: AutoFormFormProps['onError']
27+
clean?: AutoFormFormProps['clean']
28+
validate?: AutoFormFormProps['validate']
29+
schema?: AutoFormFormProps['schema']
30+
omit?: string[] | string
31+
only?: string[] | string
32+
getErrorText?: AutoFormFormProps['getErrorText']
33+
getDefaultLabel?: AutoFormFormProps['getDefaultLabel']
34+
refetchQueries?: string[]
35+
buttonRef?: AutoFormFormProps['buttonRef']
36+
autoSave?: boolean
37+
autoSaveDebounceTime?: number
38+
useFormTag?: AutoFormFormProps['useFormTag']
39+
className?: AutoFormFormProps['className']
40+
client?: any
41+
}
42+
43+
export interface CreateAutoFormOptions {
44+
getField: (fieldType: string) => any
45+
onError: (error: any) => any
46+
getErrorText: AutoFormFormProps['getErrorText']
47+
loading: JSX.Element
48+
getDefaultLabel: () => string
49+
getClient?: () => ApolloClient<any>
50+
}
51+
52+
export const options: CreateAutoFormOptions = {
53+
getField: () => {
54+
throw new Error('You must pass a getField function')
55+
},
56+
onError: error => alert(error.message),
57+
getErrorText: (code, field) => code,
58+
loading: null,
59+
getDefaultLabel: () => 'This field'
60+
}
61+
62+
export class AutoForm extends React.Component<AutoFormProps> {
63+
static defaultProps: Partial<AutoFormProps> = {
64+
children: props => <Fields getField={options.getField} {...props} />,
65+
clean: clean,
66+
validate: getValidationErrors,
67+
omit: [],
68+
only: [],
69+
onSuccess: () => {},
70+
onValidationError: () => {},
71+
onError: error => options.onError(error),
72+
getErrorText: (code, field) => options.getErrorText(code, field),
73+
getDefaultLabel: () => options.getDefaultLabel(),
74+
autoSaveDebounceTime: 500
75+
}
76+
77+
form: Form = null
78+
debouncedSubmit: Function = null
79+
80+
constructor(props) {
81+
super(props)
82+
this.debouncedSubmit = debounce(this.submit, props.autoSaveDebounceTime)
83+
}
84+
85+
submit = () => {
86+
return this.form.submit()
87+
}
88+
89+
onChange = newDoc => {
90+
if (this.props.onChange) {
91+
this.props.onChange(newDoc)
92+
}
93+
if (this.props.autoSave) {
94+
this.debouncedSubmit()
95+
}
96+
}
97+
98+
setDoc = newDoc => {
99+
this.onChange(newDoc)
100+
}
101+
102+
renderChildren({params}) {
103+
if (typeof this.props.children === 'function') {
104+
return this.props.children({
105+
params,
106+
omit: this.props.omit,
107+
only: this.props.only
108+
})
109+
} else {
110+
return this.props.children
111+
}
112+
}
113+
114+
getFragment({name, result, basicResultQuery, params}) {
115+
if (this.props.fragment) {
116+
return this.props.fragment
117+
} else {
118+
return getFragment({name, result, basicResultQuery, params})
119+
}
120+
}
121+
122+
render() {
123+
const passedClient = options.getClient ? options.getClient() : undefined
124+
const client = this.props.client ? this.props.client : passedClient
125+
126+
if (!client) {
127+
throw new Error('You must pass a client or pass a getClient function')
128+
}
129+
130+
return (
131+
<WithParams name={this.props.mutation} loading={options.loading} client={client}>
132+
{({name, result, basicResultQuery, params}) => (
133+
<WithMutation
134+
client={client}
135+
refetchQueries={this.props.refetchQueries}
136+
params={params}
137+
fragment={this.getFragment({name, result, basicResultQuery, params})}
138+
mutation={this.props.mutation}>
139+
{(mutate: AutoFormFormProps['mutate']) => (
140+
<Form
141+
setRef={form => (this.form = form)}
142+
buttonRef={this.props.buttonRef}
143+
doc={this.props.doc}
144+
mutate={mutate}
145+
useFormTag={this.props.useFormTag}
146+
className={this.props.className}
147+
onChange={this.onChange}
148+
params={params}
149+
getDefaultLabel={this.props.getDefaultLabel}
150+
schema={this.props.schema || params}
151+
onSuccess={this.props.onSuccess}
152+
onError={this.props.onError}
153+
getErrorFieldLabel={this.props.getErrorFieldLabel}
154+
onValidationError={this.props.onValidationError}
155+
clean={this.props.clean}
156+
getErrorText={this.props.getErrorText}
157+
validate={this.props.validate}>
158+
{this.renderChildren({params: this.props.schema || params})}
159+
</Form>
160+
)}
161+
</WithMutation>
162+
)}
163+
</WithParams>
164+
)
165+
}
166+
}
167+
168+
export function setupAutoForm(passedOptions: CreateAutoFormOptions) {
169+
for (const key in passedOptions) {
170+
options[key] = passedOptions[key]
171+
}
172+
}

0 commit comments

Comments
 (0)