-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RadioSelectList] 實作 RadioSelectList, RadioSelectOption #340
Merged
benny0642
merged 6 commits into
project/radio_select_list
from
feature/radio-select-list
May 5, 2022
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d595eaf
feat: implement RadioSelectList and RadioSelectOption
benny0642 984dedc
feat: add props for RadioSelectList
benny0642 53f3719
fix: fix props name
benny0642 f7a5b09
docs: update CHANGELOG
benny0642 ba3c3c4
test(parseSelectOptions): add unit test for RadioSelectOption
benny0642 ca5074d
docs: update comment for typeSymbol
benny0642 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { valueType } from './RadioSelectOption'; | ||
import SelectList from './SelectList'; | ||
|
||
export default function RadioSelectList({ | ||
value, | ||
defaultValue, | ||
onChange, | ||
title, | ||
desc, | ||
...otherProps | ||
}) { | ||
return ( | ||
<SelectList | ||
value={value} | ||
defaultValue={defaultValue} | ||
onChange={onChange} | ||
title={title} | ||
desc={desc} | ||
{...otherProps} | ||
// default props for RadioSelectList | ||
multiple={false} | ||
showCheckAll={false} | ||
checkAllLabel={null} | ||
minCheck={undefined} | ||
/> | ||
); | ||
} | ||
|
||
RadioSelectList.propTypes = { | ||
value: valueType, | ||
defaultValue: valueType, | ||
onChange: PropTypes.func, | ||
title: PropTypes.string, | ||
desc: PropTypes.node, | ||
}; | ||
|
||
RadioSelectList.defaultProps = { | ||
value: undefined, | ||
defaultValue: undefined, | ||
onChange: () => {}, | ||
title: undefined, | ||
desc: undefined, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { | ||
Radio, | ||
ListRow, | ||
} from '@ichef/gypcrete'; | ||
|
||
export const valueType = PropTypes.oneOfType([ | ||
PropTypes.string, | ||
PropTypes.number, | ||
PropTypes.bool, | ||
]); | ||
|
||
export const TYPE_SYMBOL = Symbol('RadioSelectOption'); | ||
|
||
function RadioSelectOption({ | ||
label, | ||
desc, | ||
value, | ||
avatar, | ||
readOnly, | ||
checked, | ||
onChange, | ||
...checkboxProps | ||
}) { | ||
const handleRadioChange = (event) => { | ||
onChange(value, event.target.checked); | ||
}; | ||
|
||
return ( | ||
<ListRow> | ||
<Radio | ||
checked={checked} | ||
disabled={readOnly} | ||
basic={label} | ||
aside={desc} | ||
avatar={avatar} | ||
onChange={handleRadioChange} | ||
{...checkboxProps} | ||
/> | ||
</ListRow> | ||
); | ||
} | ||
|
||
RadioSelectOption.propTypes = { | ||
label: PropTypes.node.isRequired, | ||
desc: PropTypes.node, | ||
value: valueType, | ||
avatar: PropTypes.node, | ||
readOnly: PropTypes.bool, | ||
// Set by <SelectList> | ||
checked: PropTypes.bool, | ||
onChange: PropTypes.func, | ||
}; | ||
|
||
RadioSelectOption.defaultProps = { | ||
desc: null, | ||
value: null, | ||
avatar: null, | ||
readOnly: false, | ||
checked: false, | ||
onChange: () => {}, | ||
}; | ||
|
||
RadioSelectOption.typeSymbol = TYPE_SYMBOL; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (optional) 也許可以補個需要 typeSymbol 的原因,讓讀者知道可去看 SelectOption comment |
||
|
||
export default RadioSelectOption; |
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
52 changes: 52 additions & 0 deletions
52
packages/storybook/examples/form/RadioSelectList.stories.js
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React from 'react'; | ||
import { action } from '@storybook/addon-actions'; | ||
|
||
import RadioSelectList from '@ichef/gypcrete-form/src/RadioSelectList'; | ||
import RadioSelectOption from '@ichef/gypcrete-form/src/RadioSelectOption'; | ||
|
||
export default { | ||
title: '@ichef/gypcrete-form|RadioSelectList', | ||
component: RadioSelectList, | ||
subcomponents: { RadioSelectOption }, | ||
}; | ||
|
||
export const singleUncontrolled = () => ( | ||
<RadioSelectList defaultValue="1" onChange={action('change')}> | ||
<RadioSelectOption label="Option A" value="1" /> | ||
<RadioSelectOption label="Option B" value="2" /> | ||
<RadioSelectOption label="Option C" value="3" /> | ||
</RadioSelectList> | ||
); | ||
singleUncontrolled.story = { | ||
name: 'Single-value (uncontrolled)', | ||
}; | ||
|
||
export const singleControlled = () => ( | ||
<RadioSelectList value="1" onChange={action('change')}> | ||
<RadioSelectOption label="Option A" value="1" /> | ||
<RadioSelectOption label="Option B" value="2" /> | ||
<RadioSelectOption label="Option C" value="3" /> | ||
</RadioSelectList> | ||
); | ||
singleControlled.story = { | ||
name: 'Single-value (controlled)', | ||
parameters: { | ||
docs: { | ||
storyDescription: 'Observe its onChange() should be firing with user-clicked option', | ||
}, | ||
}, | ||
}; | ||
|
||
export const multipleWithReadOnly = () => ( | ||
<RadioSelectList | ||
value="1" | ||
onChange={action('change')} | ||
> | ||
<RadioSelectOption label="Option A" value="1" readOnly /> | ||
<RadioSelectOption label="Option B" value="2" /> | ||
<RadioSelectOption label="Option C" value="3" /> | ||
</RadioSelectList> | ||
); | ||
multipleWithReadOnly.story = { | ||
name: 'With Read-only options', | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
透過這些 props 寫死 RadioSelectList 是一個單選的 SelectList