This repository has been archived by the owner on Aug 17, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 fix the visibility toggle in event form (#750)
- Loading branch information
Showing
6 changed files
with
50 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,34 @@ | ||
import React, { Component } from 'react' | ||
import React, { useState, useCallback } from 'react' | ||
import PropTypes from 'prop-types' | ||
|
||
import './toggle.css' | ||
|
||
class Toggle extends Component { | ||
state = { | ||
checked: this.props.checked, | ||
} | ||
|
||
getValue = checked => { | ||
const { truthy, falsy } = this.props | ||
if (checked && truthy) { | ||
return truthy | ||
} | ||
if (!checked && falsy) { | ||
return falsy | ||
} | ||
return checked | ||
} | ||
|
||
getChecked = value => { | ||
const { truthy, falsy } = this.props | ||
const { checked } = this.state | ||
if (!truthy && !falsy) { | ||
return checked | ||
} | ||
if (value === truthy) { | ||
return true | ||
} | ||
if (value === falsy) { | ||
return false | ||
} | ||
return value | ||
} | ||
|
||
handleChange = e => { | ||
const { checked } = e.target | ||
const { onChange } = this.props | ||
this.setState( | ||
() => ({ checked }), | ||
() => onChange(this.getValue(checked)), | ||
) | ||
} | ||
|
||
render() { | ||
const { name, value, ...rest } = this.props | ||
const checked = this.getChecked(value) | ||
|
||
return ( | ||
<label className="toggle" htmlFor={name}> | ||
<input | ||
id={name} | ||
name={name} | ||
type="checkbox" | ||
{...rest} | ||
checked={checked} | ||
onChange={this.handleChange} | ||
/> | ||
<span className="toggle-item" /> | ||
</label> | ||
) | ||
} | ||
import styles from './toggle.module.css' | ||
|
||
const Toggle = ({ name, checked, onChange, ...rest }) => { | ||
const [isChecked, setChecked] = useState(checked) | ||
const handleChange = useCallback( | ||
e => { | ||
setChecked(e.target.checked) | ||
onChange(e.target.checked) | ||
}, | ||
[onChange], | ||
) | ||
|
||
return ( | ||
<label className={styles.toggle} htmlFor={name}> | ||
<input id={name} type="checkbox" {...rest} checked={isChecked} onChange={handleChange} /> | ||
<span className={styles.item} /> | ||
</label> | ||
) | ||
} | ||
|
||
Toggle.propTypes = { | ||
name: PropTypes.string.isRequired, | ||
onChange: PropTypes.func.isRequired, | ||
truthy: PropTypes.string, | ||
falsy: PropTypes.string, | ||
checked: PropTypes.bool, | ||
value: PropTypes.any, | ||
onChange: PropTypes.func.isRequired, | ||
} | ||
|
||
Toggle.defaultProps = { | ||
truthy: undefined, | ||
falsy: undefined, | ||
checked: false, | ||
value: undefined, | ||
} | ||
|
||
export default Toggle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters