From 91ecdaa5dd1a65ff0ae944896945c0fe4bc23574 Mon Sep 17 00:00:00 2001 From: haishanh Date: Mon, 5 Nov 2018 18:32:16 +0800 Subject: update: convert more components to function ones includes Input, Config, SideBard. also removed react-redux Provider --- src/components/ToggleSwitch.js | 112 ++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 70 deletions(-) (limited to 'src/components/ToggleSwitch.js') diff --git a/src/components/ToggleSwitch.js b/src/components/ToggleSwitch.js index cdd4726..7303984 100644 --- a/src/components/ToggleSwitch.js +++ b/src/components/ToggleSwitch.js @@ -1,77 +1,49 @@ -import React, { Component } from 'react'; +import React, { useRef } from 'react'; import PropTypes from 'prop-types'; import s0 from 'c/ToggleSwitch.module.scss'; -class ToggleSwitch extends Component { - static propTypes = { - options: PropTypes.array, - value: PropTypes.string, - name: PropTypes.string, - onChange: PropTypes.func - }; - - static defaultProps = { - options: [ - { - label: 'Global', - value: 'Global' - }, - { - label: 'Rule', - value: 'Rule' - }, - { - label: 'Direct', - value: 'Direct' - } - ], - value: 'Rule', - name: 'rand0' - }; - - // handleRadioOnChange = ev => { - // ev.preventDefault(); - // const value = ev.target.value; - // if (this.state.value === value) return; - // this.setState({ value }); - // }; - - render() { - const { options, name, value, onChange } = this.props; - const w = (100 / options.length).toPrecision(3); - return ( -
-
- {options.map((o, idx) => { - if (value === o.value) this.idx = idx; - const id = `${name}-${o.label}`; - let className = idx === 0 ? '' : 'border-left'; - return ( - - ); - })} - -
+function ToggleSwitch2({ options, value, name, onChange }) { + const idxRef = useRef(null); + const w = (100 / options.length).toPrecision(3); + return ( +
+
+ {options.map((o, idx) => { + if (value === o.value) idxRef.current = idx; + const id = `${name}-${o.label}`; + let className = idx === 0 ? '' : 'border-left'; + return ( + + ); + })} +
- ); - } +
+ ); } -export default ToggleSwitch; +ToggleSwitch2.propTypes = { + options: PropTypes.array, + value: PropTypes.string, + name: PropTypes.string, + onChange: PropTypes.func +}; + +export default React.memo(ToggleSwitch2); -- cgit v1.3.1