Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/violet-humans-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@example/erp": patch
"@genseki/react": patch
---

[Feat]: add limit to type 'create' and remove button
1 change: 1 addition & 0 deletions examples/erp/genseki/collections/posts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export const fields = builder.fields('post', (fb) => ({
postTags: fb.relations('postTags', (fb) => ({
type: 'create' as const,
label: 'Tags',
limit: 4,
fields: fb.fields('postTag', (fb) => ({
remark: fb.columns('remark', {
type: 'text',
Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/core/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ export interface FieldRelationConnectOptions extends FieldOptionsShapeBase {
type: 'connect'
fields: Fields
options: string
limit?: number
}
export interface FieldRelationConnectShapeClient
extends Omit<FieldRelationConnectOptions, 'fields'>,
Expand All @@ -387,6 +388,7 @@ export interface FieldRelationConnectShape
export interface FieldRelationCreateOptions extends FieldOptionsShapeBase {
type: 'create'
fields: Fields
limit?: number
}
export interface FieldRelationCreateShapeClient
extends Omit<FieldRelationCreateOptions, 'fields'>,
Expand All @@ -401,6 +403,7 @@ export interface FieldRelationConnectOrCreateOptions extends FieldOptionsShapeBa
type: 'connectOrCreate'
fields: Fields
options: string
limit?: number
}
export interface FieldRelationConnectOrCreateShapeClient
extends Omit<FieldRelationConnectOrCreateOptions, 'fields'>,
Expand Down
52 changes: 49 additions & 3 deletions packages/react/src/react/components/compound/auto-field/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ export function AutoField(props: AutoFieldProps) {
disabled={disabled}
optionsFetchPath={props.optionsFetchPath}
deselectable={field.deselectable}
limit={field.limit}
/>
)
}
Expand All @@ -667,6 +668,8 @@ interface AutoRelationshipFieldProps {
allowConnect?: boolean
disabled?: boolean
deselectable?: boolean

limit?: number
}

export function AutoRelationshipField(props: AutoRelationshipFieldProps) {
Expand All @@ -685,6 +688,7 @@ export function AutoRelationshipField(props: AutoRelationshipFieldProps) {
disabled={props.disabled}
optionsFetchPath={props.optionsFetchPath}
deselectable={props.deselectable}
limit={props.limit}
/>
)
case true:
Expand All @@ -698,6 +702,7 @@ export function AutoRelationshipField(props: AutoRelationshipFieldProps) {
disabled={props.disabled}
optionsFetchPath={props.optionsFetchPath}
deselectable={props.deselectable}
limit={props.limit}
/>
)
default:
Expand Down Expand Up @@ -803,6 +808,8 @@ interface AutoManyRelationshipFieldProps {
allowConnect?: boolean
disabled?: boolean
deselectable?: boolean

limit?: number
}

export function AutoManyRelationshipField(props: AutoManyRelationshipFieldProps) {
Expand All @@ -814,8 +821,9 @@ export function AutoManyRelationshipField(props: AutoManyRelationshipFieldProps)

const fieldShape = props.fieldShape

const isLimitReached = props.limit ? fieldArray.fields.length >= props.limit : false
if (fieldShape.hidden) return null
const disabled = fieldShape.disabled || props.disabled
const disabled = fieldShape.disabled || props.disabled || isLimitReached

const connectComponent = (name: string, options: string) => (
<FormField
Expand Down Expand Up @@ -871,6 +879,16 @@ export function AutoManyRelationshipField(props: AutoManyRelationshipFieldProps)
{fieldShape.label} #{index + 1}
</div>
{connectComponent(`${props.name}.${index}.connect`, fieldShape.options)}
<div className="flex justify-end">
<Button
type="button"
variant="primary"
size="sm"
onClick={() => fieldArray.remove(index)}
>
Remove
</Button>
</div>
</div>
))}
</div>
Expand All @@ -889,22 +907,40 @@ export function AutoManyRelationshipField(props: AutoManyRelationshipFieldProps)
case 'create':
return (
<div className="rounded-md border border-input p-6 grid grid-cols-1 gap-y-6">
{!!fieldArray.fields.length && (
{fieldArray.fields.length ? (
<div className="flex flex-col space-y-6">
{fieldArray.fields.map((fieldValue, index) => (
<div
key={fieldValue.id}
className="p-6 bg-surface-tertiary rounded-sm flex flex-col gap-y-4"
>
<Typography type="caption" weight="normal">
{fieldShape.label} #{index + 1}
{fieldShape.label} #{index + 1}{' '}
{fieldShape.required && <span className="ml-1 text-text-brand">*</span>}
</Typography>
<div className="flex flex-col">
{createComponent(`${props.name}.${index}.create`)}
</div>
<div className="flex justify-end">
<Button
type="button"
variant="primary"
size="sm"
onClick={() => fieldArray.remove(index)}
>
Remove
</Button>
</div>
</div>
))}
</div>
) : (
<div>
<Typography type="body" weight="bold">
{fieldShape.label}
</Typography>
{fieldShape.required && <span className="ml-1 text-text-brand">*</span>}
</div>
)}
<Button
type="button"
Expand All @@ -929,6 +965,16 @@ export function AutoManyRelationshipField(props: AutoManyRelationshipFieldProps)
</div>
{connectComponent(`${props.name}.${index}.connect`, fieldShape.options)}
{createComponent(`${props.name}.${index}.create`)}
<div className="flex justify-end">
<Button
type="button"
variant="primary"
size="sm"
onClick={() => fieldArray.remove(index)}
>
Remove
</Button>
</div>
</div>
))}
<Button
Expand Down
Loading