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

feat(forms): Auto ID generation client side if no id prop provided #882

Merged
merged 24 commits into from
Aug 19, 2017

Conversation

tmorehouse
Copy link
Member

@tmorehouse tmorehouse commented Aug 18, 2017

This is a client side only ID generator, which supplies an ID attribute on form control when no ID prop is provided.

ID generation only runs on client side, during the mounted() hook (after SSR rehydration, if applicable).

The presence if IDs, even if not in the initial SSR rendered HTML will greatly improve accessibility.

Other components that can take advantage of IDs (i.e. dropdowns, carousel, etc) can use the new id.js mixin

lib/mixins/id.js Outdated
safeIdPlus(str) {
str = String(str || '').replace(/\s+/g, '_');
return (this.safeId && Boolean(str)) ? `${this.safeId}_${str}` : null;
}
}
};
Copy link
Member

@mosinve mosinve Aug 18, 2017

Choose a reason for hiding this comment

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

IMHO will be more flexible to have one universal method at this API, which accepts two optional params: generateId(String suffix = '', Boolean isPrefix=false)

It could be something like that:

generateId(str='', isPrefix=false) {
            str = String(str).replace(/\s+/g, '_');
            if(!!str && this.safeId){
                     return isPrefix? `${str}_${this.safeId}`: `${this.safeId}_${str}`
            }else if(this.safeId){   
            return this.safeId
            }
      return null
}

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

True, but in most cases, this is an internal ID's will have suffix only.

Originally I had planned on a single method, which appended the suffix if provided (similar to what you have above), but I wanted wanted the suffixed ID to return null if the suffix was invalid (i.e. a boolean, undefined, etc).

Copy link
Member Author

Choose a reason for hiding this comment

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

but I could convert safeId to a method, and optionally append the first argument

@bootstrap-vue bootstrap-vue deleted a comment from codecov-io Aug 18, 2017
lib/mixins/id.js Outdated
if (!id) {
return null;
}
suffix = String(suffix || '').replace(/\s+/g, '_');
Copy link
Member

Choose a reason for hiding this comment

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

With default value at function definition you do not need to do suffix || ''

Copy link
Member

Choose a reason for hiding this comment

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

on the whole, this PR now looks better with one flexible method 👍

@codecov-io
Copy link

Codecov Report

Merging #882 into 1.x will increase coverage by 0.38%.
The diff coverage is 82.35%.

Impacted file tree graph

@@            Coverage Diff            @@
##             1.x     #882      +/-   ##
=========================================
+ Coverage   40.5%   40.89%   +0.38%     
=========================================
  Files         88       88              
  Lines       2244     2252       +8     
  Branches     645      647       +2     
=========================================
+ Hits         909      921      +12     
+ Misses      1177     1175       -2     
+ Partials     158      156       -2
Impacted Files Coverage Δ
lib/components/form-select.vue 64.7% <0%> (ø) ⬆️
lib/components/form-file.vue 14.1% <0%> (ø) ⬆️
lib/components/form-textarea.vue 51.85% <0%> (ø) ⬆️
lib/mixins/id.js 100% <100%> (+100%) ⬆️
lib/components/form-input.vue 50% <100%> (ø) ⬆️
lib/components/form-radio.vue 60.71% <100%> (ø) ⬆️
lib/components/form-group.vue 75.92% <100%> (+1.92%) ⬆️
lib/components/form-checkbox.vue 42.1% <100%> (ø) ⬆️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d7e4f8a...1a9fdd0. Read the comment docs.

@tmorehouse tmorehouse merged commit da39b86 into 1.x Aug 19, 2017
@tmorehouse tmorehouse deleted the tmorehouse-formids branch August 19, 2017 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants