Skip to content

Commit 5c24975

Browse files
0aveRyanCopilotsaeedvaziry
authored
Add componentProps to DynamicField, Database defaults for WordPress (#936)
* Add componentProps to DynamicField and set WordPress database charset and collation defaults * Update resources/js/pages/databases/components/create-database.tsx Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Saeed Vaziry <61919774+saeedvaziry@users.noreply.github.com>
1 parent 6f754a3 commit 5c24975

File tree

6 files changed

+46
-7
lines changed

6 files changed

+46
-7
lines changed

app/DTOs/DynamicField.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public function __construct(
1414
private ?array $options = null,
1515
private ?array $link = null,
1616
private ?string $className = null,
17+
private ?array $componentProps = null,
1718
) {}
1819

1920
public static function make(string $name): self
@@ -122,6 +123,16 @@ public function className(?string $className): self
122123
return $this;
123124
}
124125

126+
/**
127+
* @param array<string, mixed> $props
128+
*/
129+
public function componentProps(array $props): self
130+
{
131+
$this->componentProps = $props;
132+
133+
return $this;
134+
}
135+
125136
/**
126137
* @return array<string, mixed>
127138
*/
@@ -137,6 +148,7 @@ public function toArray(): array
137148
'options' => $this->options,
138149
'link' => $this->link,
139150
'className' => $this->className,
151+
'componentProps' => $this->componentProps,
140152
];
141153
}
142154
}

app/Providers/SiteTypeServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ public function wordpress(): void
216216
DynamicField::make('database')
217217
->text()
218218
->label('Database Name')
219-
->placeholder('wordpress'),
219+
->placeholder('wordpress')
220+
->componentProps(['defaultCharset' => 'utf8mb4', 'defaultCollation' => 'utf8mb4_0900_ai_ci']),
220221
DynamicField::make('database_user')
221222
->text()
222223
->label('Database User')

resources/js/pages/databases/components/create-database.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FormEvent, ReactNode, useState } from 'react';
1+
import { FormEvent, ReactNode, useEffect, useState } from 'react';
22
import {
33
Dialog,
44
DialogClose,
@@ -32,11 +32,15 @@ type CreateForm = {
3232
export default function CreateDatabase({
3333
server,
3434
withUser = false,
35+
defaultCharset,
36+
defaultCollation,
3537
onDatabaseCreated,
3638
children,
3739
}: {
3840
server: number;
3941
withUser?: boolean;
42+
defaultCharset?: string;
43+
defaultCollation?: string;
4044
onDatabaseCreated?: () => void;
4145
children: ReactNode;
4246
}) {
@@ -52,12 +56,21 @@ export default function CreateDatabase({
5256

5357
const form = useForm<CreateForm>({
5458
name: '',
55-
charset: '',
56-
collation: '',
59+
charset: defaultCharset || '',
60+
collation: defaultCollation || '',
5761
user: withUser,
5862
existing_user_id: '',
5963
});
6064

65+
// Auto-load collations when modal opens with a default charset
66+
useEffect(() => {
67+
if (open && defaultCharset && charsets.includes(defaultCharset) && collations.length === 0) {
68+
axios.get(route('databases.collations', { server: server, charset: defaultCharset })).then((response) => {
69+
setCollations(response.data);
70+
});
71+
}
72+
}, [open, charsets, defaultCharset, server, collations]);
73+
6174
const submit = (e: FormEvent) => {
6275
e.preventDefault();
6376
form.post(route('databases.store', server), {
@@ -106,7 +119,7 @@ export default function CreateDatabase({
106119
</FormField>
107120
<FormField>
108121
<Label htmlFor="charset">Charset</Label>
109-
<Select onValueChange={handleCharsetChange} defaultValue={form.data.charset}>
122+
<Select onValueChange={handleCharsetChange} value={form.data.charset}>
110123
<SelectTrigger id="charset">
111124
<SelectValue placeholder="Select charset" />
112125
</SelectTrigger>
@@ -122,7 +135,7 @@ export default function CreateDatabase({
122135
</FormField>
123136
<FormField>
124137
<Label htmlFor="collation">Collation</Label>
125-
<Select onValueChange={(value) => form.setData('collation', value)} defaultValue={form.data.collation}>
138+
<Select onValueChange={(value) => form.setData('collation', value)} value={form.data.collation}>
126139
<SelectTrigger id="collation">
127140
<SelectValue placeholder="Select collation" />
128141
</SelectTrigger>

resources/js/pages/databases/components/database-select.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ export default function DatabaseSelect({
1111
serverId,
1212
value,
1313
createWithUser,
14+
defaultCharset,
15+
defaultCollation,
1416
onValueChange,
1517
...props
1618
}: {
1719
serverId: number;
1820
value: string;
1921
createWithUser?: boolean;
22+
defaultCharset?: string;
23+
defaultCollation?: string;
2024
onValueChange: (value: string) => void;
2125
} & SelectTriggerProps) {
2226
const query = useQuery<Database[]>({
@@ -43,7 +47,13 @@ export default function DatabaseSelect({
4347
</SelectGroup>
4448
</SelectContent>
4549
</Select>
46-
<CreateDatabase server={serverId} withUser={createWithUser} onDatabaseCreated={() => query.refetch()}>
50+
<CreateDatabase
51+
server={serverId}
52+
withUser={createWithUser}
53+
defaultCharset={defaultCharset}
54+
defaultCollation={defaultCollation}
55+
onDatabaseCreated={() => query.refetch()}
56+
>
4757
<Button variant="outline">
4858
<PlusIcon />
4959
</Button>

resources/js/pages/sites/components/create-site.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ export default function CreateSite({
169169
/*@ts-expect-error dynamic types*/
170170
onValueChange={(value) => form.setData('database', value)}
171171
createWithUser={true}
172+
defaultCharset={field.componentProps?.defaultCharset as string | undefined}
173+
defaultCollation={field.componentProps?.defaultCollation as string | undefined}
172174
/>
173175
{/*@ts-expect-error dynamic types*/}
174176
<InputError message={form.errors.database} />

resources/js/types/dynamic-field-config.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ export interface DynamicFieldConfig {
1212
url: string;
1313
};
1414
className?: string;
15+
componentProps?: Record<string, unknown>;
1516
}

0 commit comments

Comments
 (0)