Skip to content

Commit 08a1643

Browse files
committed
Define shared types in types.ts
1 parent c286411 commit 08a1643

34 files changed

+212
-112
lines changed

lib/generators/inertia_templates/scaffold/templates/react/Edit.tsx.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Link, Head } from '@inertiajs/react'
22
import Form from './Form'
3-
import { <%= inertia_model_type %> } from './<%= inertia_component_name %>'
3+
import { <%= inertia_model_type %> } from './types'
44

55
interface EditProps {
66
<%= singular_table_name %>: <%= inertia_model_type %>

lib/generators/inertia_templates/scaffold/templates/react/Form.tsx.tt

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
11
import { FormEvent } from 'react'
22
import { useForm, InertiaFormProps } from '@inertiajs/react'
3-
import { <%= inertia_model_type %> } from './<%= inertia_component_name %>'
3+
import { <%= inertia_model_type %>, <%= inertia_model_form_type %> } from './types'
44

55
interface FormProps {
66
<%= singular_table_name %>: <%= inertia_model_type %>
7-
onSubmit: (form: InertiaFormProps<<%= inertia_component_name %>FormType>) => void
7+
onSubmit: (form: InertiaFormProps<<%= inertia_model_form_type %>>) => void
88
submitText: string
99
}
1010

11-
type <%= inertia_component_name %>FormType = Omit<<%= inertia_model_type %>, <%= omit_input_attributes.map { |a| "'#{a}'" }.join(' | ') %>><% if custom_form_attributes.any? -%> & {
12-
<% custom_form_attributes.map do |attribute| -%>
13-
<% if attribute.password_digest? -%>
14-
password: string
15-
password_confirmation: string
16-
<% elsif attribute.attachment? -%>
17-
<%= attribute.column_name %>?: File
18-
<% elsif attribute.attachments? -%>
19-
<%= attribute.column_name %>?: File[]
20-
<% end -%>
21-
<% end -%>
22-
}<% end -%>
23-
2411
export default function Form({ <%= singular_table_name %>, onSubmit, submitText }: FormProps) {
25-
const form = useForm<<%= inertia_component_name %>FormType>({
12+
const form = useForm<<%= inertia_model_form_type %>>({
2613
<% attributes.reject { |a| a.attachment? || a.attachments? }.each do |attribute| -%>
2714
<% if attribute.password_digest? -%>
2815
password: '',

lib/generators/inertia_templates/scaffold/templates/react/Index.tsx.tt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Link, Head } from '@inertiajs/react'
2-
import <%= inertia_component_name %>, { <%= inertia_model_type %> } from './<%= inertia_component_name %>'
2+
import { <%= inertia_model_type %> } from './types'
3+
import <%= inertia_component_name %> from './<%= inertia_component_name %>'
34

45
interface IndexProps {
56
<%= plural_table_name %>: <%= inertia_model_type %>[]

lib/generators/inertia_templates/scaffold/templates/react/New.tsx.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Link, Head } from '@inertiajs/react'
22
import Form from './Form'
3-
import { <%= inertia_model_type %> } from './<%= inertia_component_name %>'
3+
import { <%= inertia_model_type %> } from './types'
44

55
interface NewProps {
66
<%= singular_table_name %>: <%= inertia_model_type %>

lib/generators/inertia_templates/scaffold/templates/react/One.tsx.tt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
export interface <%= inertia_model_type %> {
2-
id: number
3-
<% attributes.reject(&:password_digest?).each do |attribute| -%>
4-
<%= attribute.column_name %>: <%= ts_type(attribute) %>
5-
<% end -%>
6-
}
1+
import { <%= inertia_model_type %> } from './types'
72

83
interface <%= inertia_component_name %>Props {
94
<%= singular_table_name %>: <%= inertia_model_type %>

lib/generators/inertia_templates/scaffold/templates/react/Show.tsx.tt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { MouseEvent } from 'react'
22
import { Link, Head } from '@inertiajs/react'
3-
import <%= inertia_component_name %>, { <%= inertia_model_type %> } from './<%= inertia_component_name %>'
3+
import { <%= inertia_model_type %> } from './types'
4+
import <%= inertia_component_name %> from './<%= inertia_component_name %>'
45

56
interface ShowProps {
67
<%= singular_table_name %>: <%= inertia_model_type %>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export interface <%= inertia_model_type %> {
2+
id: number
3+
<% attributes.reject(&:password_digest?).each do |attribute| -%>
4+
<%= attribute.column_name %>: <%= ts_type(attribute) %>
5+
<% end -%>
6+
}
7+
8+
export type <%= inertia_model_form_type %> = Omit<<%= inertia_model_type %>, <%= omit_input_attributes.map { |a| "'#{a}'" }.join(' | ') %>><% if custom_form_attributes.any? -%> & {
9+
<% custom_form_attributes.map do |attribute| -%>
10+
<% if attribute.password_digest? -%>
11+
password: string
12+
password_confirmation: string
13+
<% elsif attribute.attachment? -%>
14+
<%= attribute.column_name %>?: File
15+
<% elsif attribute.attachments? -%>
16+
<%= attribute.column_name %>?: File[]
17+
<% end -%>
18+
<% end -%>
19+
}<% end -%>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export interface <%= inertia_model_type %> {
2+
id: number
3+
<% attributes.reject(&:password_digest?).each do |attribute| -%>
4+
<%= attribute.column_name %>: <%= ts_type(attribute) %>
5+
<% end -%>
6+
}
7+
8+
export type <%= inertia_model_form_type %> = Omit<<%= inertia_model_type %>, <%= omit_input_attributes.map { |a| "'#{a}'" }.join(' | ') %>><% if custom_form_attributes.any? -%> & {
9+
<% custom_form_attributes.map do |attribute| -%>
10+
<% if attribute.password_digest? -%>
11+
password: string
12+
password_confirmation: string
13+
<% elsif attribute.attachment? -%>
14+
<%= attribute.column_name %>?: File
15+
<% elsif attribute.attachments? -%>
16+
<%= attribute.column_name %>?: File[]
17+
<% end -%>
18+
<% end -%>
19+
}<% end -%>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export interface <%= inertia_model_type %> {
2+
id: number
3+
<% attributes.reject(&:password_digest?).each do |attribute| -%>
4+
<%= attribute.column_name %>: <%= ts_type(attribute) %>
5+
<% end -%>
6+
}
7+
8+
export type <%= inertia_model_form_type %> = Omit<<%= inertia_model_type %>, <%= omit_input_attributes.map { |a| "'#{a}'" }.join(' | ') %>><% if custom_form_attributes.any? -%> & {
9+
<% custom_form_attributes.map do |attribute| -%>
10+
<% if attribute.password_digest? -%>
11+
password: string
12+
password_confirmation: string
13+
<% elsif attribute.attachment? -%>
14+
<%= attribute.column_name %>?: File
15+
<% elsif attribute.attachments? -%>
16+
<%= attribute.column_name %>?: File[]
17+
<% end -%>
18+
<% end -%>
19+
}<% end -%>

lib/generators/inertia_templates/scaffold/templates/vue/Edit.ts.vue.tt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
<script setup lang="ts">
2121
import { Link, Head, InertiaForm } from '@inertiajs/vue3'
22-
import Form, { <%= inertia_component_name %>FormType } from './Form.vue'
23-
import { <%= inertia_model_type %> } from './<%= inertia_component_name %>.vue'
22+
import { <%= inertia_model_type %>, <%= inertia_model_form_type %> } from './types'
23+
import Form from './Form.vue'
2424

2525
const { <%= singular_table_name %> } = defineProps<{ <%= singular_table_name %>: <%= inertia_model_type %> }>()
2626

27-
const handleSubmit = (form: InertiaForm<<%= inertia_component_name %>FormType>) => {
27+
const handleSubmit = (form: InertiaForm<<%= inertia_model_form_type %>>) => {
2828
form.transform((data) => ({ <%= singular_table_name %>: data }))
2929
<% if attributes.any?(&:attachments?) -%>
3030
form.post(`<%= js_resource_path %>`, {

0 commit comments

Comments
 (0)