0.8.0 • Published 11 months ago

quark-ui-rate v0.8.0

Weekly downloads
-
License
-
Repository
-
Last release
11 months ago

Introduction

Give rate to something.

It's a cross-framework component enpowered by quarkc.

Installation

npm i quark-ui-rate

Use whatever package manager you like.

Usage

Since it's cross-framework, you can use it in popular javascript frameworks like Vue, React, Angular and jQuery. Rate component is fully controlled, it only reacts to passed-down attribute changes.

First import it in your main JS entry:

import "quark-ui-rate";

Vue

First create an attribute(vue2)in your .vue SFC script:

export default {
  data() {
    return {
      // create attribute to bind
      rateValue: 4.7,
    };
  },
  methods: {
    handleInput(event) {
      this.rateValue = event.detail;
    },
    handleChange(event) {
      console.log("value changed", event.detail);
    },
  },
};

or writable ref(vue3):

import { ref } from "vue";
const rateValue = ref(4.7);
const handleInput = (event) => {
  // set ref's value
  rateValue.value = event.detail;
};
const handleChange = (event) => {
  console.log("value changed", event.detail);
};

Then in your .vue SFC template, bind it to value attribute:

<quark-ui-rate
  size="1.5rem"
  :value="rateValue"
  space="0.5rem"
  color="#ddd"
  active-color="linear-gradient(to right, #a8f, #8af)"
  @input="handleInput"
  @change="handleChange"
></quark-ui-rate>

fix console warning

By default, Vue will try to resolve any non-native HTML tag as a registered Vue component before falling back to rendering it as custom element. This will cause Vue to emit a "failed to resolve component" warning during development. Your should tell Vue to ignore element with name "quark-ui-rate" or just ignore any element prefixed with "quark-". Please refer to Vue's official document for resolution and detailed explanation.

React

in your function component:

import { useState } from "react";

export default function App() {
  const [value, setValue] = useState(4.7);
  const handleInput = (event) => {
    setValue(event.detail);
  };
  const handleChange = (event) => {
    console.log("value changed", event.detail);
  };
  return (
    <div>
      <quark-ui-rate
        size="1.5rem"
        value={value}
        space="0.5rem"
        color="#ddd"
        activeColor="linear-gradient(to right, #a8f, #8af)"
        onInput={handleInput}
        onChange={handleChange}
      ></quark-ui-rate>
    </div>
  );
}

Vanilla JS

document.addEventListener("DOMContentLoaded", () => {
  const rate = document.getElementById("rate");
  rate.addEventListener("input", (event) => {
    rate.setAttribute("value", event.detail);
  });
  rate.addEventListener("change", (event) => {
    console.log("value changed", event.detail);
  });
});

Then use it as a normal web component in your .html files:

<quark-ui-rate
  id="rate"
  size="1.5rem"
  value="4.7"
  space="0.5rem"
  color="#ddd"
  activeColor="linear-gradient(to right, #a8f, #8af)"
></quark-ui-rate>

Examples above show a 4.7 stars rating out of 5 (top rating stars' count can be customized by the component property count, which is default to 5).

demo

API

Attributes

AttributeDescriptionTypeDefault
valuecurrent ratingnumber0
counticon countnumber5
sizeicon size, unit default to 'px'number | string20px
spacespace between iconsnumber | string4px
iconicon's urlstring
colordefault color of iconstring#F0F3F5
activeColoractive color of iconstring
allowHalfis half select allowedbooleanfalse
readonlyis readonlybooleanfalse
disabledis disabledbooleanfalse

Events

EventDescriptionParameters
inputcalled when new rating value given{ detail: number }
changecalled when rating value changed{ detail: number }
0.8.0

11 months ago

0.7.1

11 months ago

0.7.0

11 months ago

0.6.0

11 months ago

0.5.6

11 months ago

0.5.5

11 months ago

0.5.4

11 months ago

0.5.3

11 months ago

0.5.2

12 months ago

0.5.0

12 months ago

0.4.1

12 months ago

0.4.0

12 months ago

0.2.4

12 months ago

0.2.3

12 months ago

0.2.2

12 months ago

0.2.1

12 months ago

0.2.0

12 months ago

0.1.2

12 months ago

0.1.1

12 months ago

0.1.0

12 months ago

0.0.5

12 months ago

0.0.4

12 months ago

0.0.3

12 months ago

0.0.2

12 months ago

0.0.1

12 months ago