2022-04-04 11:22:00 +05:30
|
|
|
import { kebabCase } from 'lodash';
|
|
|
|
import Vue from 'vue';
|
|
|
|
import { GlToggle } from '@gitlab/ui';
|
|
|
|
import { parseBoolean } from '~/lib/utils/common_utils';
|
|
|
|
|
|
|
|
export const initToggle = (el) => {
|
|
|
|
if (!el) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
const { name, id, isChecked, disabled, isLoading, label, help, labelPosition, ...dataset } =
|
|
|
|
el.dataset || {};
|
|
|
|
|
|
|
|
const dataAttrs = Object.fromEntries(
|
|
|
|
Object.entries(dataset).map(([key, value]) => [`data-${kebabCase(key)}`, value]),
|
|
|
|
);
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
return new Vue({
|
|
|
|
el,
|
2023-05-27 22:25:52 +05:30
|
|
|
name: 'ToggleFromHtml',
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
data() {
|
|
|
|
return {
|
2023-05-27 22:25:52 +05:30
|
|
|
disabled: parseBoolean(disabled),
|
|
|
|
isLoading: parseBoolean(isLoading),
|
2022-04-04 11:22:00 +05:30
|
|
|
value: parseBoolean(isChecked),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
render(h) {
|
|
|
|
return h(GlToggle, {
|
|
|
|
props: {
|
|
|
|
name,
|
|
|
|
value: this.value,
|
|
|
|
disabled: this.disabled,
|
|
|
|
isLoading: this.isLoading,
|
|
|
|
label,
|
|
|
|
help,
|
|
|
|
labelPosition,
|
|
|
|
},
|
|
|
|
class: el.className,
|
2022-05-07 20:08:51 +05:30
|
|
|
attrs: { id, ...dataAttrs },
|
2022-04-04 11:22:00 +05:30
|
|
|
on: {
|
|
|
|
change: (newValue) => {
|
|
|
|
this.value = newValue;
|
|
|
|
this.$emit('change', newValue);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|