Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(FormGroup): simplify bindings between input and form group p… #704

Merged
merged 12 commits into from
Sep 21, 2023
Merged

chore(FormGroup): simplify bindings between input and form group p… #704

merged 12 commits into from
Sep 21, 2023

Conversation

romhml
Copy link
Collaborator

@romhml romhml commented Sep 20, 2023

πŸ”— Linked issue

#680

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

This PR refactors the way the FormGroup component injects props into input components. It also introduces a util function to generate unique unique html identifiers (fixes #680).

In short:

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@vercel
Copy link

vercel bot commented Sep 20, 2023

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Updated (UTC)
ui βœ… Ready (Inspect) Visit Preview Sep 21, 2023 4:16pm

@romhml
Copy link
Collaborator Author

romhml commented Sep 20, 2023

FYI @benjamincanac @aditio-eka

const color = computed(() => formGroup?.error?.value ? 'red' : props.color)
const size = computed(() => formGroup?.size?.value ?? props.size)
const id = formGroup?.labelFor
const { emitFormBlur, emitFormInput, size, color, inputId, name } = useFormGroup(props)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const { emitFormBlur, emitFormInput, size, color, inputId, name } = useFormGroup(props)
const { emitFormBlur, emitFormInput, size, color, inputId } = useFormGroup(props)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the name not used to bind on the <input>?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went too fast, fixed it here fb2442b

@@ -38,9 +46,12 @@ export const useFormGroup = (inputAttrs?: InputAttrs) => {
}, 300)

return {
inputId: formGroup?.inputId,
name: computed(() => inputProps?.name ?? formGroup?.name),
size: computed(() => inputProps?.size ?? formGroup?.size?.value ?? appConfig.ui.input.default.size),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately appConfig.ui.input.default.size won't work here as the default config no longer lives in the appConfig, it's only the one from the user. You can take a look at the FormGroup: https://github.com/nuxt/ui/pull/704/files#diff-82ffaa8fe9c377bdfd6259594303b0111befc57be8db4b95e1e1d4cd0d3eb56dR30

import { formGroup } from '#ui/ui.config'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I looked into it, looks like it's going to be a bit more complicated than I imagined, the default value needs to be set to the input's default (not the form group), it might make more sense move this back into each components

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you can take the default from the input as most of the other form elements inherit from its config. This could lead to an edge-case if the user changes the config entirely but I think its good enough.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it by providing the configuration object in the composable to avoid edge-cases, what do you think?

pnpm-lock.yaml Outdated Show resolved Hide resolved
@@ -80,7 +80,7 @@ async function submit (event: FormSubmitEvent<Schema>) {
</UFormGroup>

<UFormGroup name="checkbox" label="Checkbox">
<UCheckbox v-model="state.checkbox" />
<UCheckbox v-model="state.checkbox" label="Check me" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add a label here? This should with UFormGroup right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checkbox components and Radio are a bit different from the others because a label is included in the component:

https://github.com/nuxt/ui/pull/704/files#diff-eeb89ce166ecb860614d9cb79ad8b64476eaf9a919c31dfccb89c50052c2c728R21

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but both behaviour should work, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@benjamincanac benjamincanac Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does clicking on the Checkbox label (one from FormGroup) works for you? it doesn't for me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sorry I could not see straight, just fixed it!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The label of the Radio components doesn't seem to work while the one from the FormGroup does.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, note that clicking on the FormGroup's label for Radio groups does not work properly, I'll fix this in another PR introducing a wrapper component (will also fix #684).

@benjamincanac benjamincanac changed the title refactor(FormGroup): simplify bindings between input and form group p… chore(FormGroup): simplify bindings between input and form group p… Sep 21, 2023
@benjamincanac benjamincanac merged commit 46879dc into nuxt:dev Sep 21, 2023
1 check passed
@@ -119,7 +119,7 @@ export default defineComponent({
},
size: {
type: String as PropType<keyof typeof config.size>,
default: () => config.default.size,
default: null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@romhml Why did you move the default to the useFormGroup composable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

prop id (optional) for radio component
2 participants