index.tsx 4.6 KB
Newer Older
1 2 3 4
import {
  defineComponent,
  inject,
  onBeforeUnmount,
d-u-a's avatar
d-u-a 已提交
5
  Ref,
6 7 8 9 10 11 12
  ref,
  computed,
  watch,
} from 'vue'
import { useListeners } from '../../helpers/useListeners'
import { NVueComponentStyles, createNVueTextVNode } from '../utils'
import { checkboxProps } from '../../components/checkbox'
d-u-a's avatar
d-u-a 已提交
13 14 15 16 17 18
import {
  uniCheckGroupKey,
  UniCheckGroupCtx,
} from '../../components/checkbox-group'
import { UniFormCtx, uniFormKey } from '../../components/form'
import { uniLabelKey, UniLabelCtx } from '../label'
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

const checkboxStyles: NVueComponentStyles = [
  {
    'uni-checkbox': {
      '': {
        flexDirection: 'row',
        alignItems: 'center',
      },
    },
    'uni-checkbox-input': {
      '': {
        justifyContent: 'center',
        alignItems: 'center',
        position: 'relative',
        borderWidth: '1',
        borderColor: '#d1d1d1',
        borderStyle: 'solid',
        backgroundColor: '#ffffff',
        borderRadius: '3',
        width: '22',
        height: '22',
      },
    },
    'uni-icon': {
      '': {
        fontFamily: 'unincomponents',
        fontSize: '16',
        marginLeft: '2',
        marginTop: '2',
        color: '#007aff',
      },
    },
    'uni-checkbox-input-disabled': {
      '': {
        backgroundColor: '#e1e1e1',
      },
    },
    'uni-checkbox-input-disabled-before': {
      '': {
        color: '#adadad',
      },
    },
    'uni-checkbox-slot': {
      '': {
        fontSize: '16',
        marginLeft: '5',
      },
    },
  },
]

export default defineComponent({
  name: 'Checkbox',
  props: checkboxProps,
  styles: checkboxStyles,
  setup(props, { slots }) {
    const rootRef = ref<HTMLElement | null>(null)
    const checkboxChecked = ref(props.checked)
    const checkboxValue = ref(props.value)
d-u-a's avatar
d-u-a 已提交
78 79 80 81 82 83 84 85 86 87

    const checkboxColor = computed(() =>
      props.disabled ? '#adadad' : props.color
    )

    const reset = () => {
      checkboxChecked.value = false
    }

    const _onClick = ($event: Event, isLabelClick?: boolean) => {
88 89 90 91 92
      if (props.disabled) {
        return
      }
      if (isLabelClick) {
        rootRef.value!.click()
93
        return
94 95
      }
      checkboxChecked.value = !checkboxChecked.value
d-u-a's avatar
d-u-a 已提交
96
      uniCheckGroup && uniCheckGroup.checkboxChange($event)
97 98
    }

d-u-a's avatar
d-u-a 已提交
99 100 101 102
    const { uniCheckGroup, uniLabel } = useCheckboxInject(
      checkboxChecked,
      checkboxValue,
      reset
103 104
    )

d-u-a's avatar
d-u-a 已提交
105 106 107 108 109 110 111 112
    if (uniLabel) {
      uniLabel.addHandler(_onClick)
      onBeforeUnmount(() => {
        uniLabel.removeHandler(_onClick)
      })
    }
    useListeners(props, { 'label-click': _onClick })

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    watch(
      [() => props.checked, () => props.value],
      ([newChecked, newModelValue]) => {
        checkboxChecked.value = newChecked
        checkboxValue.value = newModelValue
      }
    )

    const wrapSlots = () => {
      if (!slots.default) return []
      const vnodes = slots.default()
      if (vnodes.length === 1 && vnodes[0].type === Text) {
        return [
          createNVueTextVNode(vnodes[0].children as string, {
            class: 'uni-checkbox-slot',
          }),
        ]
      }
      return vnodes
    }

    return () => {
      return (
        <div
          ref={rootRef}
          {...{ dataUncType: 'uni-checkbox' }}
d-u-a's avatar
d-u-a 已提交
139
          onClick={_onClick}
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
          class="uni-checkbox"
        >
          <div
            class="uni-checkbox-input"
            // @ts-expect-error
            class={{ 'uni-checkbox-input-disabled': props.disabled }}
          >
            {checkboxChecked.value
              ? createNVueTextVNode('\uEA08', {
                  class: 'uni-icon',
                  style: {
                    color: checkboxColor.value,
                  },
                })
              : null}
          </div>
          {...wrapSlots()}
        </div>
      )
    }
  },
})
d-u-a's avatar
d-u-a 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

function useCheckboxInject(
  checkboxChecked: Ref<string | boolean>,
  checkboxValue: Ref<string>,
  reset: () => void
) {
  const field = computed(() => ({
    checkboxChecked: Boolean(checkboxChecked.value),
    value: checkboxValue.value,
  }))
  const formField = { reset }

  const uniCheckGroup = inject<UniCheckGroupCtx>(
    uniCheckGroupKey,
    false as unknown as UniCheckGroupCtx
  )
  if (!!uniCheckGroup) {
    uniCheckGroup.addField(field)
  }

  const uniForm = inject<UniFormCtx>(uniFormKey, false as unknown as UniFormCtx)
  if (!!uniForm) {
    uniForm.addField(formField)
  }

  const uniLabel = inject<UniLabelCtx>(
    uniLabelKey,
    false as unknown as UniLabelCtx
  )

  onBeforeUnmount(() => {
    uniCheckGroup && uniCheckGroup.removeField(field)
    uniForm && uniForm.removeField(formField)
  })

  return {
    uniCheckGroup,
    uniForm,
    uniLabel,
  }
}