Skip to content

Commit 7dd617e

Browse files
authored
feat: hideTextField in ColorPicker component (#73)
1 parent d6124d1 commit 7dd617e

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

src/components/ColorPicker.jsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const ColorPicker = ({
5555
onOpen,
5656
doPopup,
5757
disableAlpha,
58+
hideTextfield,
5859
}) => {
5960
const refPicker = React.useRef(null);
6061
const [open, setOpen] = React.useState(openAtStart);
@@ -117,6 +118,17 @@ const ColorPicker = ({
117118
);
118119
}
119120

121+
let textField = null;
122+
if (!hideTextfield) {
123+
textField = disableTextfield ? (
124+
<div role="button" data-testid="colorpicker-noinput" onClick={handleClick}>
125+
{color.raw}
126+
</div>
127+
) : (
128+
<TextField color="primary" value={raw} onChange={handleChange} data-testid="colorpicker-input" />
129+
);
130+
}
131+
120132
return (
121133
<StyledRoot ref={refPicker}>
122134
<ColorButton
@@ -125,13 +137,7 @@ const ColorPicker = ({
125137
color={color}
126138
onClick={handleClick}
127139
/>
128-
{disableTextfield ? (
129-
<div role="button" data-testid="colorpicker-noinput" onClick={handleClick}>
130-
{color.raw}
131-
</div>
132-
) : (
133-
<TextField color="primary" value={raw} onChange={handleChange} data-testid="colorpicker-input" />
134-
)}
140+
{textField}
135141
{box}
136142
</StyledRoot>
137143
);
@@ -151,6 +157,7 @@ ColorPicker.propTypes = {
151157
Don't use alpha
152158
*/
153159
disableAlpha: PropTypes.bool,
160+
hideTextfield: PropTypes.bool,
154161
};
155162

156163
ColorPicker.defaultProps = {
@@ -163,6 +170,7 @@ ColorPicker.defaultProps = {
163170
openAtStart: false,
164171
doPopup: undefined,
165172
disableAlpha: false,
173+
hideTextfield: false,
166174
};
167175

168176
export default uncontrolled(ColorPicker);

stories/ColorPicker.stories.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,12 @@ export const Localization = () => {
127127
Localization.story = {
128128
parameters: { defaultValue: 'red', palette: paletteObj, deferred: true },
129129
};
130+
131+
export const HideTextfield = () => (
132+
<div style={style}>
133+
<ColorPicker defaultValue="red" hideTextfield />
134+
</div>
135+
);
136+
HideTextfield.story = {
137+
parameters: { defaultValue: 'red', hideTextfield: true },
138+
};

test/ColorPicker.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,9 @@ test('ColorPicker open', async () => {
137137
fireEvent.click(buttons[6]);
138138
expect(onChange).toHaveBeenCalledTimes(1);
139139
});
140+
141+
test('ColorPicker hideTextfield', async () => {
142+
const { queryAllByTestId } = render(<ColorPicker value="red" hideTextfield />);
143+
expect(await queryAllByTestId('colorpicker-input')).toEqual([]);
144+
expect(await queryAllByTestId('colorpicker-noinput')).toEqual([]);
145+
});

0 commit comments

Comments
 (0)