Skip to content

Commit

Permalink
feat(junipero): allow to dissociate field title parsing from options
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasAuger committed Feb 22, 2022
1 parent 973b9c6 commit 01cc0ed
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/junipero/lib/SelectField/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ const SelectField = forwardRef(({
onChange = () => {},
onFocus = () => {},
onToggle = () => {},
dissociateFieldParsing = false,
parseTitle = val => val?.toString?.(),
parseFieldTitle = val => val?.toString?.(),
parseValue = val => val,
validate = val => !required || typeof val !== 'undefined',
...rest
Expand Down Expand Up @@ -297,7 +299,12 @@ const SelectField = forwardRef(({
placeholder={placeholder}
label={label}
empty={!state.value}
value={parseTitle(state.value || '')}
value={
(dissociateFieldParsing
? parseFieldTitle
: parseTitle
)(state.value || '')
}
valid={state.valid}
focused={state.focused}
onMouseDown={onMouseDown_}
Expand Down Expand Up @@ -390,7 +397,9 @@ SelectField.propTypes = {
onFocus: PropTypes.func,
onToggle: PropTypes.func,
opened: PropTypes.bool,
dissociateFieldParsing: PropTypes.bool,
parseTitle: PropTypes.func,
parseFieldTitle: PropTypes.func,
parseValue: PropTypes.func,
placeholder: PropTypes.oneOfType([
PropTypes.string,
Expand Down
10 changes: 10 additions & 0 deletions packages/junipero/lib/SelectField/index.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,13 @@ export const alwaysOpened = () => (
onChange={action('change')}
/>
);

export const withParsedFieldTitle = () => (
<SelectField
options={options}
placeholder="Choose one item"
dissociateFieldParsing={true}
parseFieldTitle={o => o && `Custom title: ${o}`}
onChange={action('change')}
/>
);
22 changes: 22 additions & 0 deletions packages/junipero/lib/SelectField/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@ describe('<SelectField />', () => {
expect(ref.current.internalValue).toBe('Two');
});

it('should dissociate field title parsing from options parsing', () => {
const ref = createRef();
const component = mount(
<SelectField
ref={ref}
value="One"
options={options}
dissociateFieldParsing={true}
parseFieldTitle={o => o && `Custom parsed title: ${o}`}
parseTitle={o => o}
/>
);
expect(ref.current.internalValue).toBe('One');
expect(component.find('.value').text()).toBe(
'Custom parsed title: One'
);
component.find('.base').simulate('focus');
expect(ref.current.opened).toBe(true);
expect(component.find('.dropdown-item').at(0).find('a').text())
.toBe('One');
});

it('should close menu when disabled prop changes', () => {
const ref = createRef();
const component = mount(<SelectField ref={ref} options={options} />);
Expand Down

0 comments on commit 01cc0ed

Please sign in to comment.