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

Detect "Next" click to perform call then proceed to next step on success #13

Closed
ianarsenault opened this issue Jul 26, 2018 · 3 comments

Comments

@ianarsenault
Copy link

ianarsenault commented Jul 26, 2018

Following your example, I'm trying to have a user fill out a simple form. I'd like to keep the button disabled until vuelidate does it's thing, and once everything checks out allow the button to be clickable.

When it's "clickable" + clicked I'd like to call a function and upon success, go to the next step.

How do I go about detecting next button click, and how do I prevent from going to next step until I want to?

I'm currently at where I can save to the DB once there are no vuelidate errors. However I would like it so that it only saves once the user clicks "Next"

<script>
    import {validationMixin} from 'vuelidate'
    import {required, email, sameAs, minLength } from 'vuelidate/lib/validators'
    import UserService from '@/services/UserService'

    export default {
      name: "StepOne",
      props: ['clickedNext', 'currentStep'],
      mixins: [validationMixin],
      data() {
        return {
          user: {
            username: '',
            email: '',
            password: '',
            confirmPassword: ''
          }
        }
      },
      validations: {
        user: {
          username: {
            required
          },
          email: {
            required,
            email
          },
          password: {
            required,
            minLength: minLength(8)
          },
          confirmPassword: {
            required,
            minLength: minLength(8),
            sameAsPassword: sameAs('password')
          }
        }
      },
      watch: {
        $v: {
          handler: function (val) {
            if(!val.$invalid) {
              this.addUser()
              this.$emit('can-continue', {value: true});
            } else {
              this.$emit('can-continue', {value: false});
            }
          },
          deep: true
        },
        clickedNext(val) {
          console.log(val)
          if(val === true) {
            this.$v.user.$touch();
          }
        }
      },
      mounted() {
        if(!this.$v.$invalid) {
          this.$emit('can-continue', {value: true});
        } else {
          this.$emit('can-continue', {value: false});
        }
      },
      methods: {
        async addUser() {
          await UserService.addUser(this.user).then(res => {
            if (res.data.errors) {
              this.errors = res.data.errors // display errors
              this.errorAlert() // alert 
            } else {
               console.log('successfully saved information')
              // Now allow the stepper to go to the next step
            }
          })
        }
      }
    }
Edujugon added a commit that referenced this issue Jul 29, 2018
@Edujugon
Copy link
Member

Hi @ianarsenault ,

We've just released a new package version (1.3.0). Here you can find a new event which is emitted before newStep action. So you can easily accomplish your goal.

Below example exposes how you can use it:

<div class="column is-8 is-offset-2">
     <horizontal-stepper @before-next-step="beforeNextStep" :steps="demoSteps" @completed-step="completeStep" :top-buttons="true" @active-step="isStepActive" @stepper-finished="alert">
     </horizontal-stepper>
</div>
.
.
.
methods: {
    beforeNextStep ({ currentStep }, next) {
         // Here your logic
         ........

        // Then next
        next() 
    }
.
.
.

Don't forget calling next() just after your check. If you don't want to continue just call next(false)

You also have access to the current step. so you can check currentStep.name or currentStep.index

Hope this helps

@ianarsenault
Copy link
Author

Thank you @Edujugon !

@MpEliaz
Copy link

MpEliaz commented Jul 5, 2019

Hi! the event is triggered when the next button is disabled, how i can to fix it?

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

No branches or pull requests

3 participants