diff --git a/.vscode/settings.json b/.vscode/settings.json index f89ccdc..1ba3dd5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,8 +1,8 @@ { "workbench.colorCustomizations": { - "statusBar.background": "#93e6fc", + "statusBar.background": "#ffef5b", "statusBar.foreground": "#15202b", - "statusBarItem.hoverBackground": "#61dbfb" + "statusBarItem.hoverBackground": "#ffea28" }, - "peacock.color": "#61dafb" + "peacock.color": "#ffea28" } \ No newline at end of file diff --git a/README.md b/README.md index e5503a5..9f986f5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # react-pill-switcher -> Made with create-react-library +> A simple yet beautifull switcher to transition between two or more options. Made for React. [![NPM](https://img.shields.io/npm/v/react-pill-switcher.svg)](https://www.npmjs.com/package/react-pill-switcher) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) @@ -28,10 +28,14 @@ Coming soon... ## Component Props |Prop |Type |Description | |-------------------|----------------------------------------------------|--------------------------------------------------| -|Name |`String` |The name for the input element | -|Options |`Array` of `Strings` or `Objects {label, icon}` |A list of options | -|onChange |`event` |OnChange event that returns the input value | -|activeColor |`string` |Color code for the background of the pill | +|name* |`str` |The name for the input element | +|options* |`[str]` or `[{label: str, icon: obj, value: any}]` |A list of options | +|onChange |`event` |callback event that returns the input value | +|activeBg |`color` |Color code for the background of the pill | +|labelColor |`color` |Color code for the background of the active label | +|activeColor |`color` |Color code for the active label | +|on |`any` |When changed, the compoent is rerendered (used to prevent visual bugs) | +|isFull |`boolean` |If true, the component will fill the containers width| ## License diff --git a/example/src/App.js b/example/src/App.js index fa237fe..10c0de9 100644 --- a/example/src/App.js +++ b/example/src/App.js @@ -1,26 +1,55 @@ -import React from 'react' +import React, { useState } from 'react' import { PillSwitcher } from 'react-pill-switcher' import 'react-pill-switcher/dist/index.css' const App = () => { + const [val1, setVal1] = useState('-'); + const [val2, setVal2] = useState('-'); + const [val3, setVal3] = useState('-'); + return

Simple

- + setVal1(e)} /> +

Selected: {val1}

-

-

With Icons

- +

With Icons and values

+ + icon: , + value: 'light' }, { label: 'Dark Stuff', - icon: + icon: , + value: 'dark' } - ]} /> + ]} + onChange={(e) => setVal2(e)} + /> +

Selected: {val2}

+ +
+

With Custom Colors

+ setVal3(e)} + /> +

Selected: {val3}

+ +
+

Full width

+
} diff --git a/package.json b/package.json index 4c29263..158a8d4 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "react-pill-switcher", - "version": "1.0.3", - "description": "Made with create-react-library", + "version": "1.0.4", + "description": "A simple yet beautifull switcher to transition between two or more options. Made for React.", "author": "lalodoble", "license": "MIT", "homepage": "https://github.com/lalodoble/react-pill-switcher", diff --git a/src/index.js b/src/index.js index 536eecd..b272ffe 100644 --- a/src/index.js +++ b/src/index.js @@ -6,32 +6,40 @@ import classNames from 'classnames' // return
miamam Component: {text}
// } - -export function PillSwitcher({ name, options, onChange, activeColor }) { +export function PillSwitcher({ name, options, onChange, activeColor = null, activeBg = null, labelColor = null, on = null, isFull = false }) { // const name = props.name; // const options = props.options; const defaultRef = useRef(); - const [defaultValue, setDefaultValue] = useState(typeof options[0] === 'object' ? options[0].label : options[0]) - const [value, setValue] = useState(typeof options[0] === 'object' ? options[0].label : options[0]) + const [defaultValue, setDefaultValue] = useState(typeof options[0] === 'object' ? options[0].value || options[0].label : options[0]) + const [value, setValue] = useState(typeof options[0] === 'object' ? options[0].value || options[0].label : options[0]) const [sizes, setSizes] = useState() + let labelStyles = { + color: labelColor + } + + let activeLabelStyles = { + color: activeColor + } + useEffect(() => { if (defaultRef.current) { - setSizes({ - width: defaultRef.current.clientWidth, - height: defaultRef.current.clientHeight, - left: defaultRef.current.offsetLeft, - top: defaultRef.current.offsetTop, - transition: 'none', - }) + setTimeout(() => { + setSizes({ + width: defaultRef.current.clientWidth, + height: defaultRef.current.clientHeight, + left: defaultRef.current.offsetLeft, + top: defaultRef.current.offsetTop, + transition: 'none', + }) + }, 200); } - }, [defaultRef.current]) + }, [defaultRef.current, on]) function onChangeHandle(e) { const val = e.target.value setValue(val) onChange && onChange(val) - } function onClick(e) { @@ -44,21 +52,26 @@ export function PillSwitcher({ name, options, onChange, activeColor }) { }) } - return
+ return
{options.map((opt, i) => { const id = name + '-' + i const isObject = typeof opt === 'object' ? true : false - const actualOption = isObject ? opt.label : opt + const actualOption = isObject ? opt.value || opt.label : opt // console.log(actualOption + ' - ' + value) return (
-