Skip to content

Commit

Permalink
feat: allow required form field with default value hidden
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Aug 21, 2024
1 parent 952badb commit 2fee785
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
type MaybeConditionGroup,
toMaybeConditionGroup,
parseValidViewFilter,
formOption,
} from "@undb/table"
import { Switch } from "$lib/components/ui/switch"
import { Label } from "$lib/components/ui/label"
Expand Down Expand Up @@ -85,18 +86,24 @@
<div class="flex items-center gap-3">
<Label class="flex items-center gap-2 text-xs">
{#if !previousFields.length}
<Switch disabled class="text-sm" checked={false} />
<Switch size="sm" disabled class="text-sm" checked={false} />
{:else}
<Switch class="text-sm" bind:checked={formField.conditionEnabled} on:click={setForm} />
<Switch size="sm" class="text-sm" bind:checked={formField.conditionEnabled} on:click={setForm} />
{/if}
<span>enable condition</span>
<span>Enable condition</span>
</Label>
<Label class="flex items-center gap-2 text-xs">
<Switch
class="text-sm"
bind:checked={formField.required}
size="sm"
checked={formField.getRequired(field)}
disabled={field.required}
on:click={() => setForm()}
onCheckedChange={(checked) => {
formField.setRequired(field, checked)
form.option = formOption

setForm()
}}
/>
<span>required</span>
</Label>
Expand All @@ -106,7 +113,7 @@
type="checkbox"
class="hidden"
bind:checked={formField.hidden}
disabled={formField.required}
disabled={formField.getRequired(field) && !formField.defaultValue}
on:click={setForm}
/>
{#if formField.hidden}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@
{#each filteredFields as formField (formField.fieldId)}
{@const field = schema.get(formField.fieldId)}
{#if field}
{@const disabled = formField.getRequired(field)}
{@const required = formField.getRequired(field)}
{@const hiddenDisabled = required && !formField.defaultValue}
<button
class={cn("flex w-full items-center justify-between text-pretty p-2 text-sm transition-all", {
"bg-gray-50 shadow-inner": formField.fieldId === $selectedFieldId,
Expand Down Expand Up @@ -153,9 +154,9 @@
<Tooltip.Trigger>
<Switch
size="sm"
bind:checked={formField.required}
on:click={(e) => {
e.stopPropagation()
checked={formField.getRequired(field)}
onCheckedChange={(checked) => {
formField.setRequired(field, checked)
setForm()
}}
disabled={field.required}
Expand All @@ -165,8 +166,14 @@
<p>Required</p>
</Tooltip.Content>
</Tooltip.Root>
<label class={cn(disabled ? "cursor-not-allowed" : "cursor-pointer")}>
<input type="checkbox" class="hidden" bind:checked={formField.hidden} {disabled} on:change={setForm} />
<label class={cn(hiddenDisabled ? "cursor-not-allowed" : "cursor-pointer")}>
<input
type="checkbox"
class="hidden"
bind:checked={formField.hidden}
disabled={hiddenDisabled}
on:change={setForm}
/>
{#if formField.hidden}
<EyeClosed class="h-4 w-4" />
{:else}
Expand Down
11 changes: 11 additions & 0 deletions packages/table/src/modules/forms/form/form-field.vo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export class FormFieldVO extends ValueObject<IFormField> {

public set defaultValue(value: any) {
this.props.defaultValue = value
if ((value === null || value === undefined) && this.required) {
this.hidden = false
}
}

public set hidden(value: boolean) {
Expand All @@ -59,6 +62,14 @@ export class FormFieldVO extends ValueObject<IFormField> {
return this.props.required
}

public setRequired(field: Field, value: boolean) {
if (field.required) {
this.props.required = true
} else {
this.props.required = value
}
}

public get conditionEnabled() {
return this.props.conditionEnabled ?? false
}
Expand Down

0 comments on commit 2fee785

Please sign in to comment.