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

fix(FormGroup): ensure size exists in config #695

Merged
merged 9 commits into from
Oct 28, 2023

Conversation

Levy-from-Odessa
Copy link
Contributor

Closes #673.

@vercel
Copy link

vercel bot commented Sep 17, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
ui ✅ Ready (Inspect) Visit Preview Oct 2, 2023 5:53pm

const size = computed(() => formGroup?.size?.value ?? props.size)
const size = computed(() => {
const size = formGroup?.size?.value ?? props.size
if (!ui.value.size[size.value] ||
Copy link
Contributor

@Sma11X Sma11X Sep 19, 2023

Choose a reason for hiding this comment

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

I think we don't need to check too many, that issue happens because the range component do not support the xl size from formGroup.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agree, is it better now?

Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer return to props.size, that might be better for users want to define range size themselves, and it has default value already.
Return to default will overwrite the size passed by range if range not support formGroup size.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -89,7 +89,13 @@ export default defineComponent({

const { emitFormChange, formGroup } = useFormGroup()
const color = computed(() => formGroup?.error?.value ? 'red' : props.color)
const size = computed(() => formGroup?.size?.value ?? props.size)
const size = computed(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

#704 had conflicts with it, maybe this PR needs to take another look ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have pulled the dev and I returned the size.default as a fallback according to the #704 MR

@@ -92,7 +92,8 @@ export default defineComponent({
setup (props, { emit }) {
const { ui, attrs, attrsClass } = useUI('range', props.ui, config)

const { emitFormChange, inputId, color, size, name } = useFormGroup(props, config)
const { emitFormChange, inputId, color, size: formGroupSize, name } = useFormGroup(props, config)
const size = computed(() => ui.value.size[formGroupSize] ? formGroupSize : config.default?.size)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const size = computed(() => ui.value.size[formGroupSize] ? formGroupSize : config.default?.size)
const size = computed(() => ui.value.size[formGroupSize] ? formGroupSize : (props.size ?? config.default?.size))

How about this ? I'm not sure if range component still need to custom in formGroup thou.

Comment on lines 95 to 96
const { emitFormChange, inputId, color, size, name } = useFormGroup(props, config)
const { emitFormChange, inputId, color, size: formGroupSize, name } = useFormGroup(props, config)
const size = computed(() => ui.value.size[formGroupSize] ? formGroupSize : (props.size ?? config.default?.size))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I recently refactored how FormGroup props are injected (#704), it might be better to do this check for all inputs by doing here: https://github.com/nuxt/ui/blob/dev/src/runtime/composables/useFormGroup.ts#L47

Also, props.size should have priority over formGroupSize

I guess something like this should work?

# useFormGroup.ts
export const useFormGroup = (...) => {
    return {
      // ...
      size: computed(() => {
        const formGroupSize = config.sizes[formGroup?.size.value] ? formGroup?.size.value : undefined
        return inputProps?.size ?? formGroupSize ?? config?.default?.size
      }),
      // ...
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@romhml , could you 2-check the last commit, pls?
And I think we really should add types to the ui.config.ts - file..... It would be great to use types inside useUI or useFormGroup instead of any. Am I wrong, or i can create an issue for it?

@romhml
Copy link
Collaborator

romhml commented Sep 22, 2023

Also, wouldn't it make sense to have the same size variants for all components, or at least inputs to avoid that kind of edge cases? @benjamincanac

@benjamincanac
Copy link
Member

Also, wouldn't it make sense to have the same size variants for all components, or at least inputs to avoid that kind of edge cases? @benjamincanac

Input, Textarea, Select and SelectMenu have the same sizes, it's only for Range and in a near future Toggle (#224) that have only 3 sizes. I don't think it matters that all components have the same sizes as a user could potentially define a new size on a specific component in their app.config.ts.

@Sma11X
Copy link
Contributor

Sma11X commented Sep 23, 2023

The latest commit cannot deploy

@Levy-from-Odessa
Copy link
Contributor Author

The latest commit cannot deploy
could you describe what is the issue here, pls?

@Sma11X
Copy link
Contributor

Sma11X commented Sep 25, 2023

Try the same code as issue at playground and will return 500. It can't read xl now. No need to use ui instead of config.

Co-authored-by: Sma11X <540351143@qq.com>
@@ -92,7 +92,7 @@ export default defineComponent({
setup (props, { emit }) {
const { ui, attrs, attrsClass } = useUI('range', props.ui, config)

const { emitFormChange, inputId, color, size, name } = useFormGroup(props, config)
const { emitFormChange, inputId, color, size, name } = useFormGroup(props, ui)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const { emitFormChange, inputId, color, size, name } = useFormGroup(props, ui)
const { emitFormChange, inputId, color, size, name } = useFormGroup(props, config)

src/runtime/composables/useFormGroup.ts Show resolved Hide resolved
Copy link
Member

@romhml I've followed your advice in the end and added sizes to the Range component in 3cb3914 so it fixes #673.

Does this PR is still relevant in your opinion? It might prevent bugs in the future I guess.

@romhml
Copy link
Collaborator

romhml commented Oct 28, 2023

@benjamincanac I agree, it's still good idea to fallback to the default input's size if the one provided by the FormGroup is not defined, to handle cases where a user defines custom sizes.

@benjamincanac benjamincanac changed the title fix(Range): add default size fix(FormGroup): ensure size exists in config Oct 28, 2023
@benjamincanac benjamincanac merged commit f5f3388 into nuxt:dev Oct 28, 2023
2 checks passed
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.

Range is broken in UFormGroup
4 participants