<template>
  <el-select
    v-model="selected"
    class="font-family-select"
    placeholder="请选择"
    @change="select"
  >
    <el-option
      v-for="item in optionList"
      :key="item"
      :label="item"
      :value="item"
    >
      <span :style="{ fontFamily: item }">{{ item }}</span>
    </el-option>
  </el-select>
</template>

<script>
const PREDEFINE_OPTIONS = ["宋体", "黑体"];

export default {
  name: "FontFamilySelect",
  props: {
    value: { type: String, default: null },
    predefine: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    return {
      optionList: [],
      selected: "",
    };
  },
  watch: {
    value: {
      immediate: true,
      handler(val) {
        this.selected = val;
      },
    },
  },
  created() {
    this.optionList =
      this.predefine && this.predefine.length
        ? this.predefine
        : PREDEFINE_OPTIONS;
  },
  methods: {
    select() {
      this.$emit("input", this.selected);
      this.$emit("change", this.selected);
    },
  },
};
</script>
