Skip to content

Commit

Permalink
feat: added namespaced id generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Swiftwork committed Jan 28, 2020
1 parent 825be83 commit 82e3129
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 20 deletions.
3 changes: 1 addition & 2 deletions packages/ui-core/src/components/Button/Button.tsx
Expand Up @@ -16,9 +16,8 @@ interface State {
export default class Button extends Component<Props, State> {
constructor(props: Props) {
super(props);
window.eid = window.eid || 0;
this.state = {
id: `id-${window.eid++}`,
id: window.uikit.nextId(),
};
}

Expand Down
3 changes: 1 addition & 2 deletions packages/ui-core/src/components/CheckBox/CheckBox.tsx
Expand Up @@ -28,10 +28,9 @@ export default class CheckBox extends Component<Props, State> {

constructor(props: Props) {
super(props);
window.eid = window.eid || 0;
this.validator = new Validator<boolean>(this.props.validators);
this.state = {
id: `id-${window.eid++}`,
id: window.uikit.nextId(),
invalid: false,
focused: false,
errors: [],
Expand Down
3 changes: 1 addition & 2 deletions packages/ui-core/src/components/RadioButton/RadioButton.tsx
Expand Up @@ -28,10 +28,9 @@ export default class RadioButton extends Component<Props, State> {

constructor(props: Props) {
super(props);
window.eid = window.eid || 0;
this.validator = new Validator<string>(this.props.validators);
this.state = {
id: `id-${window.eid++}`,
id: window.uikit.nextId(),
invalid: false,
focused: false,
errors: [],
Expand Down
3 changes: 1 addition & 2 deletions packages/ui-core/src/components/Select/Select.tsx
Expand Up @@ -41,9 +41,8 @@ export default class Select extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.validator = new Validator(this.props.validators);
window.eid = window.eid || 0;
this.state = {
id: `id-${window.eid++}`,
id: window.uikit.nextId(),
invalid: false,
focused: false,
errors: [],
Expand Down
6 changes: 2 additions & 4 deletions packages/ui-core/src/components/TextArea/TextArea.tsx
Expand Up @@ -27,10 +27,9 @@ export default class TextArea extends Component<Props, State> {

constructor(props: Props) {
super(props);
window.eid = window.eid || 0;
this.validator = new Validator(this.props.validators);
this.state = {
id: `id-${window.eid++}`,
id: window.uikit.nextId(),
invalid: false,
focused: false,
errors: [],
Expand Down Expand Up @@ -79,8 +78,7 @@ export default class TextArea extends Component<Props, State> {
(this.state.focused ? ' focused' : '') +
(this.props.disabled ? ' disabled' : '') +
(this.state.invalid ? ' invalid ' : '')
}
>
}>
{this.props.label && (
<label className={'tu-textarea--label' + (this.hasValueOrFocus() ? ' floating' : '')} htmlFor={this.state.id}>
{this.props.label}
Expand Down
3 changes: 1 addition & 2 deletions packages/ui-core/src/components/TextField/TextField.tsx
Expand Up @@ -32,10 +32,9 @@ export default class TextField extends Component<Props, State> {

constructor(props: Props) {
super(props);
window.eid = window.eid || 0;
this.validator = new Validator(this.props.validators);
this.state = {
id: `id-${window.eid++}`,
id: window.uikit.nextId(),
invalid: false,
focused: false,
errors: [],
Expand Down
17 changes: 15 additions & 2 deletions packages/ui-core/src/framework/core.ts
Expand Up @@ -5,11 +5,24 @@ export {};
declare global {
interface Window {
/** Counter for unique identifiers */
eid: number;
uikit: {
ids: { [namespace: string]: number; default: number };
nextId: (namespace?: string) => string;
};
}
}

(function() {
// Make sure there is an incremental ID each component can use
window.eid || (window.eid = 0);
if (window.uikit) {
window.uikit = {
ids: { default: 0 },
nextId(namespace?: string): string {
if (!namespace || namespace == 'default') return `id-${this.ids.default++}`;
if (this.ids[namespace]) return `id-${namespace}-${this.ids[namespace]++}`;
this.ids[namespace] = 0;
return `id-${namespace}-${this.ids[namespace]}`;
},
};
}
})();
3 changes: 1 addition & 2 deletions packages/ui-datepicker/src/Datepicker.tsx
Expand Up @@ -55,7 +55,6 @@ export default class Datepicker extends Component<Props, State> {

constructor(props: Props) {
super(props);
window.eid = window.eid || 0;
this.todaysDate = new Date();
this.todaysDate.setHours(0, 0, 0, 0);
this.validator = new Validator(props.validators);
Expand All @@ -66,7 +65,7 @@ export default class Datepicker extends Component<Props, State> {
focusedDate.setHours(0, 0, 0, 0);

this.state = {
id: `id-${window.eid++}`,
id: window.uikit.nextId(),
invalid: false,
focused: false,
days: this.genDays(this.props.weekday),
Expand Down
3 changes: 1 addition & 2 deletions packages/ui-typeahead/src/Typeahead.tsx
Expand Up @@ -59,10 +59,9 @@ export default class Typeahead<T extends TypeaheadResult = TypeaheadResult> exte

constructor(props: Props<T>) {
super(props);
window.eid = window.eid || 0;
this.validator = new Validator(props.validators);
this.state = {
id: `id-${window.eid++}`,
id: window.uikit.nextId(),
invalid: false,
focused: false,
active: -1,
Expand Down

0 comments on commit 82e3129

Please sign in to comment.